简体   繁体   English

DexIndexOverflowException使用Google Maps API

[英]DexIndexOverflowException With Google Maps API

I want to make an application that uses the Google Maps API, I started with a simple application that opens a google map activity. 我想创建一个使用Google Maps API的应用程序,我开始使用一个简单的应用程序打开谷歌地图活动。 (I download google play services from the SDK Manager). (我从SDK Manager下载google play服务)。 I think that it's a very basic program but there is an error when I try to run my application. 我认为这是一个非常基本的程序,但是当我尝试运行我的应用程序时出错。

Here's the code of my program, and also the error generated by Android Studio. 这是我的程序代码,以及Android Studio生成的错误。

The error: 错误:

Error:Execution failed for task ':app:transformClassesWithDexForDebug'. 错误:任务':app:transformClassesWithDexForDebug'的执行失败。 > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536 > com.android.build.api.transform.TransformException:com.android.ide.common.process.ProcessException:java.util.concurrent.ExecutionException:com.android.dex.DexIndexOverflowException:方法ID不在[0,0xffff]中:65536

The code: 代码:

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>

This is a common error when you import all of Google Play Services, if you do, it's easy to hit the 65k method limit. 当您导入所有Google Play服务时,这是一个常见错误,如果您这样做,则很容易达到65k方法限制。

If you have this in your build.gradle: 如果你在build.gradle中有这个:

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

Replace it with this: 替换为:

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

For more info see the documentation . 有关详细信息,请参阅文档

add internet permission in manifest file 在清单文件中添加Internet权限

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

also make sure you turn on internet and location on your phone 还要确保您在手机上打开互联网和位置

Yes after adding Internet permission try to add multiDexEnabled true to your app build.gradle file. 是的,在添加Internet权限后尝试将multiDexEnabled true添加到您的app build.gradle文件中。

defaultConfig {
    multiDexEnabled true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM