java.lang.RuntimeException:com.android.builder.dexing.DexArchiveMergerException:无法在Android Studio 3.0中合并dex


77

这是我的应用程序级别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'
}

编译项目时出现的完整错误是:

错误:任务':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

从Android API 21(5.0)开始,multidex支持不需要使用minSdkVersion 21及更高版本的项目中安装的库。
xRed

1
好,那很奇怪。我期待有关的更多详细信息:app:transformDexArchiveWithExternalLibsDexMergerForDebug FAILED,抱怨类重复或类似问题。我真的不知道该告诉您什么。我知道您已经多次清理了该项目,但是我建议您再次清理它,尝试构建,然后查看该构建的Gradle Console输出是否为您提供了Gradle不满意的更多详细信息。
CommonsWare

2
@CommonsWare,非常奇怪,我进行了两次清理和重建,终于成功了。编译成功!
kumail

4
这是胡说八道,“此问题是由无法再现的问题或简单的印刷错误引起的” –第一,该问题仍然可以再现,第二,这不是简单的印刷错误!
Frank R.

1
@FrankR。同意 投票重新开放。我猜这是因为大多数答案都是清理和重建,但是在这种情况下,这似乎是一个有效的答案。
codeMagic

Answers:


150

通过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. 任务完成后,请Rebuild ProjectBuild菜单中按按钮。
  3. 从菜单 File -> Invalidate cashes / 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?
阿尔瓦罗·阿圭罗

2
@ÁlvaroAgüero,一个.dex文件可以有65,536个方法(引用),因此,如果引用数量超过65,536,则必须使用multidex。有关更多详细信息,请在官方网站上查看。
Abhishek Aryan

1
Clean Project and Rebuild为我解决了此错误。
塔伦(Tarun)

66

我正在使用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'
        }
    }
}

并清理项目。


3
对我不起作用,我认为内部有重复项,但是我找不到找出重复项的出路。
Artist404

@ Artist404可能您的项目或子模块中也包含了multidex库。删除multidex库依赖性。
Rakesh Yadav

问题仍然存在。我也做过 但没有解决....>了java.lang.RuntimeException:com.android.builder.dexing.DexArchiveMergerException:错误而合并DEX档案
吉安Swaroop Awasthi

添加完之后,我收到“多个dex文件定义Landroid / support / v4 / os / ResultReceiver”错误。你能帮我吗
rahul.sapkal23 '19

1
如果您添加了multiDexEnabled true,但尚未解决,请尝试Clean Project
Cobain
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.