无法合并dex

324

我有Android Studio Beta。我创建了一个新项目,编译了我的旧模块,但是当我尝试启动应用程序时,它没有启动,并显示以下消息:

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

com.android.builder.dexing.DexArchiveMergerException: 无法合并dex文件

但我不知道该如何解决这个错误。我已经谷歌搜索了几个小时,但是没有成功。

我的项目gradle:

    // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-beta6'
        classpath "io.realm:realm-gradle-plugin:3.7.1"
        classpath 'com.google.gms:google-services:3.1.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        google()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我的应用 gradle 文件:

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "parad0x.sk.onlyforyou"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
        }
    }
    compileOptions {
        targetCompatibility 1.7
        sourceCompatibility 1.7
    }
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }
    lintOptions {
        checkReleaseBuilds false
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    //noinspection GradleCompatible
    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
    compile project(path: ':loginregisterview')


}

我的模块 gradle:

    apply plugin: 'com.android.library'
apply plugin: 'realm-android'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.0.2'
    compile 'com.android.support:support-v4:26.1.0'
    compile 'com.github.bumptech.glide:glide:4.0.0'
    testCompile 'junit:junit:4.12'
    compile project(path: ':parser')

}

我的第二个模块:

     apply plugin: 'com.android.library'
apply plugin: 'realm-android'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    realm {
        syncEnabled = true
    }
    useLibrary 'org.apache.http.legacy'

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile 'junit:junit:4.12'
    //  compile 'com.android.support:appcompat-v7:23.1.0'

    //   compile 'com.fasterxml.jackson.core:jackson-core:2.9.0'
 //   compile 'com.fasterxml.jackson.core:jackson-annotations:2.9.0'
 //   compile 'com.fasterxml.jackson.core:jackson-databind:2.9.0'
    compile 'com.google.code.gson:gson:2.6.2'
}

在没有导入第二个模块(parser)时,应用程序不会在dex上崩溃,但当未导入该模块时,应用程序无法工作。:D :D

____________找到答案____________


6
如果有人在使用Android Studio 3.0稳定版本时遇到同样的问题,我强烈建议您查看 https://dev59.com/f1YN5IYBdhLWcg3wwKdZ#46971548 上的答案,并看看它是否能够帮助您。 - Bhavesh Patadiya
1
我曾经遇到同样的问题,我按照本页上提到的每种方法都尝试了,但都没有帮助到我,所以我去了Gradle并在每个库上按住光标检查它们是否有新版本可用。那些有新版本的我就更新了,同步 Gradle 并运行。这样就解决了问题。 - Shahzad Afridi
63个回答

359

我尝试了上述所有方法,但都没有帮助。最后,我找到了适合我的解决方法:

app/build.gradle:

android {
    defaultConfig {
       multiDexEnabled true
    }
}

11
谢谢。我认为问题在于大多数答案没有考虑到包含来自其他源的无法重建的JAR文件。multiDex似乎允许将本来不兼容的代码组合起来。 - Brendon Whateley
2
这个解决方案对我有效,但是另外,我还需要显式启用API <= 21的multidex https://developer.android.com/studio/build/multidex.html - elghazal-a
2
尽管我的回答比被接受的回答更相关,但我仍然收到“无法合并dex”的消息。 - John
还需要为所有模块添加 multiDexEnabled true - mindeh
16
对我来说没有用。进行这个更改后,我得到了不同的错误提示:执行“:app:transformClassesWithMultidexlistForDebug”任务失败。 - Atul
1
是的,我花了5个小时纠结于完全不同的答案。但不知怎么的,我意识到这是Multidex问题,然后就来到了这里。我是正确的。谢谢你。 - sud007

301

当我从com.google.android.gms:play-services:11.2.2更新到com.google.android.gms:play-services:11.4.0时,我遇到了同样的问题。这对我有用:

  1. 清除
  2. 重新构建

你只是用那个解决方案延迟了真正的问题。可能版本11.4.0包含较少的方法。请看下面我的答案。我猜在你添加下一个依赖时会出现这个错误。 - itzhar
3
这解决了我的问题,仅使用 multiDexEnabled true 是不够的。 - Yuri Brigance
30
与 com.google.android.gms:play-services-location:11.6.0 存在同样的问题...这不是一个有效的解决方案。 - issamux
3
当我在使用谷歌分析库时遇到兼容性问题时,这个解决方案对我很有帮助。在AS中将Android项目导航窗格视图设置为“Project”,您应该会看到“External Libraries”,如果展开外部库,您将看到用于编译项目的所有jars和模块。这应该能帮助您识别不兼容的模块组合。 - angryITguy
3
如果在“compileSdkVersion 26”情况下以上代码无法工作,请尝试以下步骤:进入build.gradle(Module:app)文件,将"multiDexEnabled true"添加到defaultConfig类别中。最后一步,进入File | Settings | Build, Execution, Deployment | Instant Run,并尝试启用/禁用。 - Sumit Saxena
错误:执行任务“:app:processDebugGoogleServices”失败。
请通过更新google-services插件的版本(有关最新版本的信息可在https://bintray.com/android/android-tools/com.google.gms.google-services/上找到)或更新com.google.android.gms的版本至11.4.0来解决版本冲突。
- vinidog

61

注意警告!

有时您只需要消除警告,错误就会自动消失。请参见下面的特殊情况:


我在我的模块级build.gradle文件中有这两个依赖项:

implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'

而且 Studio 已经发出了警告(除了 dex 合并问题之外):

所有的 com.android.support 库必须使用完全相同的版本规范(混合版本可能导致运行时崩溃)。发现版本为 27.0.221.0.3。例如 com.android.support:animated-vector-drawable:27.0.2com.android.support:support-v4:21.0.3

因此,我明确确定了 com.android.support:support-v4 的版本(详见这里),解决了两个问题(警告和与 dex 合并相关的问题)

implementation 'com.android.support:support-v4:27.0.2'  // Added this line (according to above warning message)
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'
请参阅下面的评论以了解其他类似情况。

1
谢谢,这个修复了我的问题。最初是由于添加了eu.inloop:androidviewmodel:1.3.4引起的。 - lsrom
2
确切地说,我希望错误信息能够更加精确。 - Amin Keshavarzian
1
非常感谢。我也遇到了同样的问题。我之前使用的是v26,更新到sdk 27后问题得到了解决。 - Johnny
2
这应该是正确的答案。在添加一个库(airbnb/epoxy)后,我的构建停止工作了,因为它依赖于support:design:26.1.0,而我还没有这个依赖项。我的其他支持库版本是27.1.0。在添加了support:design依赖项并使用27.1.0版本后,我的问题得到了解决。 - alashow

37

很遗憾的是,在我的情况下,Michel和Suragch的解决方法都不能解决我的问题。

所以我通过以下方式解决了这个问题:

gradle:3.0 中,compile 配置已经被弃用,应该使用 implementation 或者 api 替代。详情请参考这里,你也可以看官方文档:Gradle Build Tool

虽然 compile 配置仍然存在,但是不建议使用,因为它提供不了 apiimplementation 提供的保证。

与其使用 compile ,最好使用 implementation 或者 api

只需要将 compile 替换为 implementationdebugCompile 替换为 debugImplementationtestCompile 替换为 testImplementationandroidtestcompile 替换为 androidTestImplementation 即可。

例如:

compile 'com.android.support:appcompat-v7:26.0.2'
compile 'com.android.support:support-v4:26.1.0'
compile 'com.github.bumptech.glide:glide:4.0.0'

像这样使用

implementation 'com.android.support:appcompat-v7:26.0.2'
implementation 'com.android.support:support-v4:26.1.0'
implementation 'com.github.bumptech.glide:glide:4.0.0'

接下来,

  • 删除项目内的 .gradle 文件夹(注意,在左上方导航器中需要切换到“项目”视图才能看到 .gradle)
  • 删除所有构建文件夹和 gradle 缓存。
  • 在 Build 菜单中,点击 Clean Project 按钮。
  • 任务完成后,从 Build 菜单中点击 Rebuild Project 按钮。

希望这有所帮助!


2
谢谢,我所需要做的就是将compile改为implementation,然后它就成功构建了,不必完成其他步骤。在我更新Facebook SDK之后,构建开始失败。 - Shayno
2
为我工作 - 为什么的见解? - Alex
1
应该删除的文件夹是 .gradle 而不是 ./gradle - Gary Bak
1
唯一一个对我实际起作用的答案。谢谢! - Michael Richardson
1
谢谢,这是最棒的答案。应该放在搜索结果的顶部。对我很有用。 - Danger
显示剩余4条评论

32
  1. 删除 .gradle 目录。

  2. 再次运行你的App。

注意

  • .gradle 目录位于项目的根目录中(您可能需要先显示隐藏文件)。
  • 每次使用Android 3.0更新依赖模块后,我都需要这样做。(更近期发布的Android Studio 3似乎已解决了这个问题。)

4
当我执行"Invalidate Cache and Restart"操作时,这个方法对我有所帮助。 - Vasili Fedotov
当我尝试更新所有内容、删除所有编译关键字、更新一些可疑的ilb版本时,我感到非常不可思议,因为这些都没有解决我的问题。但最终当我采用了这种方法时,问题得以解决。 - hanzolo

26

根据Suragch的建议,仅删除.gradle并不足够。此外,我还需要执行Build > Clean Project

请注意,在左上角导航器中切换到“Project”视图,才能看到.gradle文件:

Switch to project view


23

我尝试了其他所有的解决方案,但都没有成功。最后,我通过编辑build.gradle文件使用相同的依赖版本来解决了问题。我认为这个问题是由于将一个使用不同支持库或者Google库的依赖库添加到gradle中造成的。

请在你的build gradle文件中添加以下代码。然后进行cleanrebuild操作。

ps: 对我而言这是一种旧的解决方法,因此您应该使用更新版本的以下库。

configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
    def requested = details.requested
    if (requested.group == 'com.android.support') {
        if (!requested.name.startsWith("multidex")) {
            details.useVersion '26.1.0'
        }
    } else if (requested.group == "com.google.android.gms") {
        details.useVersion '11.8.0'
        } else if (requested.group == "com.google.firebase") {
            details.useVersion '11.8.0'
          }
      }
}

5
尝试了所有其他方法,最终只有这个起了作用 :/ - Ante
@Ali Gurelli非常感谢,我尝试了许多解决方案,但只有这个有效。 - Bipin Bharti
谢谢,但我使用版本 details.useVersion '15.0.0' - Elsunhoty
感谢您的解决方案。我也遇到了同样的问题。Google Tap and Pay版本是10.0.0,其余所有库都是最新版本。 - R World
工作顺利,谢谢。 - Sankalp
我必须告诉你,你救了我的早晨! - Ben-Hur Batista

17

如果(1.尝试清理和重建工作),那么很好

否则,如果(2.尝试删除gradle工作),那么很好

否则-> 3. 尝试在grade.properties中添加

android.enableD8 = false

编辑2021:此第三个选项现在已弃用,请使用其他选项

否则-> 4. 在您的build.gradle文件中将multiDexEnabled设置为true

android {
    compileSdkVersion 26
    defaultConfig {
      ...
        minSdkVersion 15
        targetSdkVersion 26
        multiDexEnabled true
     ...
    }
}

并添加依赖项

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

可能第一个对你有效,依此类推,但它真正取决于你问题的本质。例如,对我来说:

一旦我添加了这个库,我就遇到了错误。

implementation 'com.jjoe64:graphview:4.2.2'

后来我发现我必须检查并添加相同版本的支持库。所以我不得不尝试另一个版本。

compile 'com.jjoe64:graphview:4.2.1'

并且它修复了这个问题。所以请注意这一点。


4
针对这个问题,我要提到最有用的方法之一,就是你可以使用以下命令:'gradlew app:transformDexArchiveWithExternalLibsDexMergerForDebug --stacktrace',以获取更多详细信息。我自己遇到了相同的问题,发现我需要启用multidex。希望能对你有所帮助。 - panda
@DINA,android.enableD8 = false已经过时。 - Daniel

13

我的情况是由于Room库引起的:

compile 'android.arch.persistence.room:runtime:1.0.0-alpha1'

更改为:

compile 'android.arch.persistence.room:runtime:1.0.0'

工作了。


非常感谢,这对我帮助很大! - Phil
太高兴找到了这个答案。谢谢! - callOfCode

12

补充以上解决方案:

确保您不在多个地方(甚至同一文件中)拥有指向它们不同版本的重复依赖项。


在将multiDexEnabled设置为true并尝试其他答案之前,我认为应该首先进行此检查。谢谢! - Aleksandr A

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