【问题标题】:I can't build my app because of .dex file error. (Android)由于 .dex 文件错误,我无法构建我的应用程序。 (安卓)
【发布时间】:2016-11-15 07:35:38
【问题描述】:

我这里有个小问题,如果我想在 Android Studio 中构建一个 APK 我收到此错误消息:

Error:The number of method references in a .dex file cannot exceed 64K.

通过https://developer.android.com/tools/building/multidex.html了解如何解决此问题

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 '/usr/local/java/jdk1.8.0_77/bin/java'' finished with non-zero exit value 2

我在 StackOverflow 上找到了一个答案,说要在我的 Gradle 文件中编译这个依赖项:

compile 'com.android.support:multidex:1.0.0'

但这没有用。

这是我的 Gradle 文件:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.google.android.gms:play-services:9.0.2'
    compile 'com.android.support:multidex:1.0.0'
}

【问题讨论】:

标签: android android-gradle-plugin android-multidex


【解决方案1】:

您需要在您的应用程序中启用多索引,通过将build.gradle 中的multiDexEnabled 标志设置为true 来完成。请记住,您的最低 SDK 版本也必须为 14 或更高版本。

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"

    defaultConfig {
        ...
        minSdkVersion 14
        targetSdkVersion 21
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

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

然后,在 Manifest 中配置您的 Application 元素:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.multidex.myapplication">
    <application
        ...
        android:name="android.support.multidex.MultiDexApplication">
        ...
    </application>
</manifest>

【讨论】:

    【解决方案2】:

    在你的 android { // } build.gradle 文件中试试这个:

    dexOptions {
        javaMaxHeapSize "4g"
        incremental true
        preDexLibraries = false
    }
    

    然后在你的依赖中,添加这个:

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

    更新

    如果您在项目中扩展应用程序,请切换到扩展此:

    public class MyApp extends MultiDexApplication{
    
       //inside here, override
    
        @Override
        protected void attachBaseContext(Context base) {
           super.attachBaseContext(base);
           MultiDex.install(this);
        }
    }
    

    最后,在AndroidManifest文件中设置你的应用名称为

    <application 
       android:name=".MyApp">
    

    这应该可以让您摆脱这个问题;祝你好运!

    【讨论】:

    • 清单仍然需要更新才能使用 MultidexApplication 类。简单地添加 gradle 依赖并不能解决问题
    • 你不需要扩展那个类,你可以直接在Manifest中使用。作为参考,您的分机 doesn't do anything extra
    • 这种情况适用于您已经扩展了应用程序类的情况,但是是的,如果您没有客户应用程序类,则不需要
    猜你喜欢
    • 2018-06-21
    • 2020-01-09
    • 2022-10-14
    • 1970-01-01
    • 2016-06-08
    • 2019-05-30
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多