【问题标题】:How can i solve this error? Method ID not in [0, 0xffff]: 65536我该如何解决这个错误?方法 ID 不在 [0, 0xffff] 中:65536
【发布时间】:2017-06-25 14:39:41
【问题描述】:

我使用 MapsActivity 创建了一个新的 Android Studio 项目,如果我尝试在我的 Huawei P8 Lite 设备上启动该应用程序,则会出现此错误:

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

我的 Build.Gradle(模块:应用程序):

apply plugin: 'com.android.application'

    android {
        compileSdkVersion 25
        buildToolsVersion "24.0.2"
        defaultConfig {
            applicationId "com.example.de.maptestdel"
            minSdkVersion 15
            targetSdkVersion 25
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        compile 'com.android.support:appcompat-v7:25.1.0'
        compile 'com.google.android.gms:play-services:10.0.1'
        compile 'com.android.support:design:25.1.0'
        testCompile 'junit:junit:4.12'
    }

除了启用 Multidex 之外,还有其他解决方法吗? 听说激活不太好。

【问题讨论】:

  • 第一个答案非常明确地指出了您的问题。这应该可以解决现在的问题。但是总有可能随着越来越多的功能的添加,您可能会再次遇到问题。基本上,问题是由于每个 dex 文件允许的方法计数受到限制。因此,如果您在以后的某个时间遇到问题,请在谷歌搜索“Android Multidex”一词。 Android 可以选择将您的 APK 拆分为多个 dex 文件

标签: android android-studio android-activity build.gradle android-multidex


【解决方案1】:

使用 Google Play 服务库中的特定/个人 API。你使用了compile 'com.google.android.gms:play-services:10.0.1',这个库方法计数是79958。

参考此链接:https://developers.google.com/android/guides/setup

在 6.5 之前的 Google Play 服务版本中,您必须将整个 API 包编译到您的应用中。在某些情况下,这样做会使您的应用程序中的方法数量(包括框架 API、库方法和您自己的代码)保持在 65,536 个限制以下变得更加困难。

从 6.5 版开始,您可以选择性地编译 Google Play 服务 API 到您的应用中。

例如

对于谷歌地图,使用:

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

方法数:17984

对于谷歌云消息:

com.google.android.gms:play-services-gcm:10.0.1

方法数:15784

因此,使用单独的 API 将减少 apk 方法数。那么,就不需要开启multidex了。

【讨论】:

  • 感谢一百万!省去了我弄乱 multidex 的麻烦。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-19
  • 1970-01-01
  • 2014-09-14
相关资源
最近更新 更多