簡體   English   中英

DexIndexOverflowException使用Google Maps API

[英]DexIndexOverflowException With Google Maps API

我想創建一個使用Google Maps API的應用程序,我開始使用一個簡單的應用程序打開谷歌地圖活動。 (我從SDK Manager下載google play服務)。 我認為這是一個非常基本的程序,但是當我嘗試運行我的應用程序時出錯。

這是我的程序代碼,以及Android Studio生成的錯誤。

錯誤:

錯誤:任務':app:transformClassesWithDexForDebug'的執行失敗。 > com.android.build.api.transform.TransformException:com.android.ide.common.process.ProcessException:java.util.concurrent.ExecutionException:com.android.dex.DexIndexOverflowException:方法ID不在[0,0xffff]中:65536

代碼:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {     
private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
<resources>

<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">My API key is here ;) </string>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.amine.bea_mapapp">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="My API key is here ;)" />

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

當您導入所有Google Play服務時,這是一個常見錯誤,如果您這樣做,則很容易達到65k方法限制。

如果你在build.gradle中有這個:

compile 'com.google.android.gms:play-services:10.0.1'

替換為:

compile 'com.google.android.gms:play-services-maps:10.0.1' 

有關詳細信息,請參閱文檔

在清單文件中添加Internet權限

 <uses-permission android:name="android.permission.INTERNET"/>

還要確保您在手機上打開互聯網和位置

是的,在添加Internet權限后嘗試將multiDexEnabled true添加到您的app build.gradle文件中。

defaultConfig {
    multiDexEnabled true
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM