Java.lang.RuntimeException: Com.android.builder.dexing.DexArchiveMergerException: 无法在Android Studio 3.0中合并dex。

81

这是我的应用级别Gradle文件:

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion '27.0.0'
    useLibrary 'org.apache.http.legacy'
    defaultConfig {
        multiDexEnabled true
        applicationId 'com.example.test'
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    productFlavors {
    }
    dexOptions {
        incremental true
    }
}

dependencies {
    compile 'com.google.android.gms:play-services:10.0.1'
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
    compile 'commons-io:commons-io:1.3.2'
    compile 'com.mcxiaoke.volley:library:1.0.19'
    compile 'com.facebook.stetho:stetho:1.5.0'
    compile 'com.github.PhilJay:MPAndroidChart:v3.0.2'
}

编译项目时我收到的完整错误提示为:

Error:执行任务“:app:transformDexArchiveWithExternalLibsDexMergerForDebug”失败。

java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: 无法合并dex

完整错误提示如下:

    Executing tasks: [:app:assembleDebug]

AGPBI: {"kind":"warning","text":"The `android.dexOptions.incremental` property is deprecated and it has no effect on the build process.","sources":[{},{}]}
Configuration 'compile' in project ':app' is deprecated. Use 'implementation' instead.
:app:buildInfoDebugLoader
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:checkDebugManifest UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:prepareLintJar UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:createDebugCompatibleScreenManifests UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:splitsDiscoveryTaskDebug UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources
:app:javaPreCompileDebug UP-TO-DATE
:app:compileDebugJavaWithJavac UP-TO-DATE
:app:mergeDebugShaders UP-TO-DATE
:app:compileDebugShaders UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:packageInstantRunResourcesDebug UP-TO-DATE
:app:checkManifestChangesDebug
:app:extractTryWithResourcesSupportJarDebug UP-TO-DATE
:app:transformClassesWithStackFramesFixerForDebug UP-TO-DATE
:app:transformClassesWithDesugarForDebug UP-TO-DATE
:app:transformClassesWithExtractJarsForDebug UP-TO-DATE
:app:transformClassesWithInstantRunVerifierForDebug UP-TO-DATE
:app:transformClassesWithDependencyCheckerForDebug UP-TO-DATE
:app:compileDebugNdk NO-SOURCE
:app:mergeDebugJniLibFolders UP-TO-DATE
:app:transformNativeLibsWithMergeJniLibsForDebug UP-TO-DATE
:app:processDebugJavaRes NO-SOURCE
:app:transformResourcesWithMergeJavaResForDebug UP-TO-DATE
:app:transformNativeLibsAndResourcesWithJavaResourcesVerifierForDebug UP-TO-DATE
:app:transformClassesWithInstantRunForDebug UP-TO-DATE
:app:transformClassesEnhancedWithInstantReloadDexForDebug UP-TO-DATE
:app:incrementalDebugTasks UP-TO-DATE
:app:preColdswapDebug
:app:fastDeployDebugExtractor UP-TO-DATE
:app:generateDebugInstantRunAppInfo UP-TO-DATE
:app:transformClassesWithInstantRunSlicerForDebug UP-TO-DATE
:app:transformClassesWithDexBuilderForDebug UP-TO-DATE
:app:transformDexArchiveWithExternalLibsDexMergerForDebug FAILED
:app:buildInfoGeneratorDebug

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s

39 actionable tasks: 5 executed, 34 up-to-date

哦,算了,我没注意到你的项目支持API 16,所以你不能真正删除它。 - xRed
2
好的,这很奇怪。我原本期望在 :app:transformDexArchiveWithExternalLibsDexMergerForDebug FAILED 周围会有更多关于重复类或类似问题的详细信息。对于那个输出,我真的不知道该告诉你什么。我知道你已经多次清理了项目,但我建议你再清理一次,尝试构建,并查看该构建的 Gradle 控制台输出是否提供了更多关于 Gradle 不喜欢的内容的详细信息。 - CommonsWare
2
@CommonsWare,太奇怪了,我进行了两次清理和重建,最终它成功了。编译成功! - kumail
5
这是无稽之谈,“这个问题是由一个无法再现或简单的排版错误引起的”- 首先,这个问题仍然能够再现;第二,它不是一个简单的排版错误! - Frank R.
2
@FrankR. 同意。投票重新开放。我猜这是因为大多数答案都是清理和重建,但在这种情况下似乎是一个有效的答案。 - codeMagic
显示剩余4条评论
2个回答

156

通过你的应用模块的build.gradle启用Multidex

multiDexEnabled true

与下方相同 -
android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.xx.xxx"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        multiDexEnabled true //Add this
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

接下来按照以下步骤操作 -

  1. Build 菜单中 -> 点击 Clean Project 按钮。
  2. 任务完成后,从 Build 菜单中点击 Rebuild Project 按钮。
  3. 从菜单 File -> Invalidate caches / Restart

compile已经现在已被弃用,因此最好使用implementationapi


4
我尝试了很多次,仍然得到相同的错误。 - kumail
1
这将解决问题,它是由于冲突引起的configurations.all { resolutionStrategy.dependencySubstitution { substitute module('org.apache.commons:commons-io:1.3.2') with module('commons-io:commons-io:1.3.2') } } - Abdul Rizwan
1
可以运行,但是multiDexEnabled是什么? - Álvaro Agüero
2
@ÁlvaroAgüero,一个.dex文件最多可以有65,536个方法(引用),因此如果引用数量超过65,536,则必须使用multidex。有关更多详细信息,请查看官方网站。 - Abhishek Aryan
1
清理项目并重新构建解决了这个错误。 - Tarun
显示剩余3条评论

67

我正在使用 Android Studio 3.0 并且遇到了同样的问题。我将以下内容添加到我的 gradle 文件中:

multiDexEnabled true

它起作用了!

例子

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.1'
    defaultConfig {
        applicationId "com.xx.xxx"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        multiDexEnabled true //Add this
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

并清理项目。


4
对我来说不起作用,我认为内部存在重复的包含,但是我找不到一种方法来告诉我哪些是重复的。 - Artist404
1
问题仍然存在。我已经尝试了相同的方法,但没有解决...>java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: 合并dex归档时出错。 - Gyan Swaroop Awasthi
1
添加了这个之后,我遇到了“Multiple dex files define Landroid/support/v4/os/ResultReceiver”错误。你能帮我解决一下吗? - rahul.sapkal23
1
如果您已经添加了multiDexEnabled true,但是没有解决问题,请尝试使用Clean Project - Cobain
在类型为 org.gradle.api.internal.plugins.DefaultExtraPropertiesExtension 的对象中找不到参数为 [true] 的 multiDexEnabled() 方法。 - Luca C.
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接