java - 安卓错误 : Your app has more methods references than can fit in a single dex file

标签 java android android-activity android-studio gradle

Gradle 构建成功,但当我尝试运行我的应用程序时,出现此错误:

Error:Your app has more methods references than can fit in a single dex file. See https://developer.android.com/tools/building/multidex.html

我在我的应用中实现了“使用 Google 帐户登录”功能后得到了这个。

为什么我会收到这个?为了解决这个问题,我不明白这个问题。你们有解决方案吗?

编辑

我按照步骤操作,但是它显示另一个错误:

Copying resources from program jar [/Users/Earthling/AndroidStudioProjects/GoogleTestProject/app/build/intermediates/transforms/jarMerging/debug/jars/1/1f/combined.jar] :app:transformClassesWithDexForDebug FAILED Error:Execution failed for task ':app:transformClassesWithDexForDebug'. com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home/bin/java'' finished with non-zero exit value 3

最佳答案

你必须使用 multidex 因为你的项目超出了一个应用程序中授权的方法的限制(大约 ~56000 或差不多,我不记得了)。请参阅文档中的这句话(您顺便提供的链接):

As the Android platform has continued to grow, so has the size of Android apps. When your application and the libraries it references reach a certain size, you encounter build errors that indicate your app has reached a limit of the Android app build architecture.

为了构建和运行,您有 to configure your build.gradle如下:

android {
    defaultConfig {
        ...
        // enabling multidex
        multiDexEnabled true
    }
}

// add the dependencie
dependencies {
    compile 'com.android.support:multidex:1.0.0'
}

然后,创建您自己的 Application 类,如下所示:

public class MyApplication extends MultiDexApplication {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this); // install multidex
    }
}

最后,在Manifest中添加这个类:

<application
    ...
    android:name=".MyApplication">

或者您可以默认使用 android.multidex.application 而不是像这样创建:

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

关于java - 安卓错误 : Your app has more methods references than can fit in a single dex file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34749193/

相关文章:

java - 可以以编程方式退出简单的等待情况吗?

java - 如何通过英文的 String.format 返回格式化的数字?

java - 工资计算模式

android - 有没有办法在同一工作区内制作项目的副本?

android - Google 制作的应用程序中的 fragment 或 Activity

java - 如何在Activity之间传递ObservableInt?

android - onActivityResult 从未在 TabActivity 中被调用

java - 如何在运行时测试可选模块是否存在

android - 权限名称 C2D_MESSAGE 不唯一出现在两个 C2D_MESSAGE 中

android - 如何使用 PhoneNumberUtils 格式化电话号码?