android - 无法构建 apk : The number of method references cannot exceed 64K

标签 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: Process 'command 'C:\Program Files\Java\jdk1.7.0_15\bin\java.exe'' finished with non-zero exit value 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' 执行失败。

Warnings found during shrinking, please use -dontwarn or -ignorewarnings to suppress them.

我该如何处理这个问题并构建一个合适的 apk?请帮忙。

最佳答案

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 中所述.

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

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

关于android - 无法构建 apk : The number of method references cannot exceed 64K,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36638756/

相关文章:

android - 带有 Retrofit gradle 问题的简单 XML

android - 使用调试 key 测试应用内购买

android - 如何防止对 Android APK 文件进行逆向工程以保护代码?

android - 在 Flutter 的登录屏幕中显示循环进度对话框,如何在 Flutter 中实现进度对话框?

android - 带有 ArrayAdapter 的 AlertDialog 不显示项目列表

java - Gradle Groovy项目依赖于Java项目

gradle - 我包含一些库后出现Gradle错误(Shared_preference)

android - 失败 [INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION : Failed to parse/data/app/vmdl1686600827. tmp/base.apk:损坏的 XML 二进制文件]

android - 如何在Android中定义 "inflation"

java - 我怎样才能从通知转到我的 Viewpager 中添加的最后一个 fragment ?