无法构建 apk:方法引用数不能超过 64K [英] Unable to build apk: The number of method references cannot exceed 64K

查看:23
本文介绍了无法构建 apk:方法引用数不能超过 64K的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been trying to build the apk file for my app, however, I am getting the error: The number of method references cannot exceed 64K.

这里是错误,

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

Error:The number of method references in a .dex file cannot exceed 64K. Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html

错误:任务:app:transformClassesWithDexForDebug"的执行失败.

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.ide.common.process.ProcessException:org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.7.0_15\bin\java.exe'' 以非零退出值 2 结束

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文件,

This is my gradle file,

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 文件并在没有任何问题的情况下部署它,我该怎么做?

I want to build the apk file and deploy it without any issues, how do I do it?

更新

我也尝试了以下

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"的执行失败.

Error:Execution failed for task ':app:transformClassesWithNewClassShrinkerForDebug'.

在收缩过程中发现的警告,请使用 -dontwarn 或 -ignorewarnings 来抑制它们.

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

我该如何处理并构建合适的 apk?请帮忙.

How do I deal with this and build a proper apk? Please Help.

推荐答案

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 应该为你做游戏

here multiDexEnabled true should do the game for you

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

UPDATE : To support latest Android version
1. If your minSdkVersion is set to 21 or higher, all you need to do is set multiDexEnabled to true in your module-level build.gradle file, as shown above.
2. However, if your minSdkVersion is set to 20 or lower, then you must use the multidex support library along with above changes as follows:

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

除了上面添加的支持库之外,您还需要对您的应用程序类进行更改,如本链接.

Apart from the above addition of support library, you need to make changes to your Application class as mentioned in this Link.

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

BEST PRACTICE:
1. Remove any unused code with proguard.
2. Avoid adding unnecessary dependencies in your project.
3. If limited number methods or classes of any open source library are needed, then its advisable to clone only those in your project as it not only gives you total control on them but also allows proguard to act on them and you don't have any unused methods in your code.

来源:Android 开发人员 - 使用 64K 方法计数配置应用

这篇关于无法构建 apk:方法引用数不能超过 64K的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆