无法构建apk:方法引用的数量不能超过64K

时间:2016-04-15 05:17:59

标签: android gradle apk

我一直在尝试为我的应用程序构建apk文件,但是,我收到错误:方法引用的数量不能超过64K。

以下是错误,

错误:.dex文件中的方法引用数不能超过64K。 了解如何在https://developer.android.com/tools/building/multidex.html

解决此问题

错误:任务':app:transformClassesWithDexForDebug'执行失败。

  

com.android.build.api.transform.TransformException:com.android.ide.common.process.ProcessException:java.util.concurrent.ExecutionException:com.android.ide.common.process.ProcessException:org.gradle .process.internal.ExecException:进程'命令'C:\ Program Files \ Java \ jdk1.7.0_15 \ bin \ java.exe''以非零退出值2结束

这是我的gradle文件,

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
    applicationId "nikhilraghavendra.hopper"
    minSdkVersion 21
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        shrinkResources true
        minifyEnabled true
        useProguard true
        proguardFiles getDefaultProguardFile('proguard-android.txt'),
                'proguard-rules.pro'
    }
}
packagingOptions {
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE-FIREBASE.txt'
    exclude 'META-INF/NOTICE'
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.google.android.gms:play-services-identity:8.4.0'
compile 'com.firebase:firebase-client-android:2.3.1'
compile 'com.android.support:cardview-v7:23.2.1'
compile 'com.google.android.gms:play-services:8.4.0'
}

我想构建apk文件并在没有任何问题的情况下部署它,我该怎么做?

更新

我也试过以下

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

dexOptions {
    maxProcessCount = 4 // this is the default value
}

dataBinding{
    enabled = true
}

defaultConfig {
    applicationId "nikhilraghavendra.hopper"
    minSdkVersion 21
    targetSdkVersion 23
    resConfigs "en", "fr"
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        shrinkResources true
        minifyEnabled true
        useProguard true
        proguardFiles getDefaultProguardFile('proguard-android.txt'),
                'proguard-rules.pro'
    }
    debug {
        minifyEnabled true
        useProguard false
    }
}
packagingOptions {
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE-FIREBASE.txt'
    exclude 'META-INF/NOTICE'
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.google.android.gms:play-services-identity:8.4.0'
compile 'com.firebase:firebase-client-android:2.3.1'
compile 'com.android.support:cardview-v7:23.2.1'
compile 'com.google.android.gms:play-services:8.4.0'
}

这产生了消息:

错误:任务':app:transformClassesWithNewClassShrinkerForDebug'的执行失败。

  

收缩时发现警告,请使用-dontwarn或-ignorewarnings来抑制它们。

我如何处理这个并构建一个合适的apk?请帮助。

3 个答案:

答案 0 :(得分:64)

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"

    defaultConfig {
        applicationId "com.try.app"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }

这里multiDexEnabled true应该为你做游戏

更新:支持最新的Android版本
1.如果您的minSdkVersion设置为 21 或更高,您需要做的就是在模块级build.gradle文件中将multiDexEnabled设置为true,如上所示。
2.但是,如果您的minSdkVersion设置为 20 或更低,那么您必须使用multidex支持库以及上述更改,如下所示:

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

除了上面添加的支持库之外,您还需要按照Link中的说明对Application类进行更改。

最佳实践:
1.使用proguard删除所有未使用的代码 2.避免在项目中添加不必要的依赖项 3.如果需要有限数量的方法或任何开源库的类别,那么建议只克隆项目中的那些,因为它不仅可以让你完全控制它们,而且还允许程序对它们采取行动而你不会。你的代码中有任何未使用的方法。

来源:Android Developers - Configure Apps with 64K Method count

答案 1 :(得分:30)

无法构建apk:当app .dex文件有超过65,536种方法时,方法引用的数量不能超过64K。

超过65K方法,这意味着方法计数超过65,536种方法 有不同的方法来解决它:

<强> 1。减少项目中的依赖项 将依赖项放在gradle中是必需的

我从其他开发者那里发现的大多数问题都是使用Google Play服务项目。

 compile 'com.google.android.gms:play‐services:8.4.0'

因此,最好的方法是只选择您真正想要的Google Play服务中的某些依赖项 用它。

compile 'com.google.android.gms:play‐services‐location:8.4.0'
compile 'com.google.android.gms:play‐services‐maps:8.4.0'
compile 'com.google.android.gms:play‐services‐ads:8.4.0'

<强> 2。将最低SDK版本设置为21或更高

为什么将最低SDK更改为21时有效?这是因为Android 5.0或更高版本都使用ART (Android Runtime)支持MultiDex而不是Dalvik。因此ART可以支持65,536以上 方法

第3。使用Proguard减少无用的方法

<强> 4。使用MultiDex

但这些是MultiDex库的限制

•如果.dex文件太大,应用程序启动时可能会出现ANR。

•应将最低SDK定义为14或更高版本。

•如果分配内存超出限制,MulitDex会使用更多内存并在应用运行时崩溃。

•构建项目时需要更多的构建时间

答案 2 :(得分:4)

将它放在defaultConfig

multiDexEnabled true