I get: Error:Execution failed for task ':app:transformClassesWithDexForDebug'. when making Google maps App

I get: Error:Execution failed for task ':app:transformClassesWithDexForDebug'. when making Google maps App

我按照 link 中的说明进行操作:https://developers.google.com/maps/documentation/android-api/start 使用 Google 地图 API 制作了一个简单的 android 应用程序,但我总是遇到此错误下面是我 运行 我 phone 上的应用:

Error:Execution failed for task ':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

清除并查看是否仍然存在错误,

1.go 到您的 build.gradle 文件。 添加 multiDexEnabled true

 defaultConfig {
    multiDexEnabled true
}

2.in 你的依赖添加 compile 'com.android.support:multidex:1.0.1'

dependencies {
 compile 'com.android.support:multidex:1.0.1'
}

3.inside 你的应用标签在 menifest 添加 android:name="android.support.multidex.MultiDexApplication"

<application
        android:name="android.support.multidex.MultiDexApplication"
    ....

4.use 在您启动时使用此覆盖方法 activity

 @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

检查您是否依赖于整个 Google Play 服务,而不是仅仅依赖于地图组件。来自 the documentation

If the number of method references in your app exceeds the 65K limit, your app may fail to compile. You may be able to mitigate this problem when compiling your app by specifying only the specific Google Play services APIs your app uses, instead of all of them. For information on how to do this, see Selectively compiling APIs into your executable.

例如(使用最新的 Play 服务版本),在您的 build.gradle

中更改此设置
dependencies {
    compile 'com.google.android.gms:play-services:10.0.1'
}

对此

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

如果您添加其他 Play 服务模块,您需要将它们单独添加到您的 build.gradle