Android Studio 3.0执行任务失败:无法合并dex。

88

在构建执行过程中,Android Studio出现了以下构建错误:

Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'. java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex"

我的app:build.gradle文件内容如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.pdroid.foodieschoice"
        minSdkVersion 16
        targetSdkVersion 25
        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.constraint:constraint-layout:1.0.2'
    compile 'com.firebaseui:firebase-ui-auth:2.3.0'

    testCompile 'junit:junit:4.12'
}
configurations.all {
    resolutionStrategy {
        force 'com.android.support:appcompat-v7:26.0.1'
        force 'com.android.support:support-compat:26.0.1'
        force 'com.android.support:support-core-ui:26.0.1'
        force 'com.android.support:support-annotations:26.0.1'
        force 'com.android.support:recyclerview-v7:26.0.1'
    }
}

apply plugin: 'com.google.gms.google-services'

有任何解决方案吗?

编辑:我已经通过 Firebase Github 网站完成了示例并解决了问题。


你需要在你的应用程序中包含MultiDex支持。 - Sudheesh R
请尝试清理并重新构建您的项目。 - Sudheesh R
我建议您编辑您的问题并发布Gradle控制台的整个输出,而不仅仅是那一行。您问题的细节可能在输出的其他地方。 - CommonsWare
那并不是你的Gradle控制台的全部输出。 - CommonsWare
2
尝试禁用即时运行,清理您的项目并添加以下内容: android { defaultConfig { multiDexEnabled true } } - Tara
显示剩余3条评论
24个回答

67

对于基于 Cordova 的项目,在再次构建之前运行 cordova clean android,正如@mkimmet所建议的那样。


1
有人请把这个答案标为正确。它解决了问题。 - pamekar
2
对我来说,安装“cordova-plugin-androidx”和“cordova-plugin-androidx-adapter”解决了这个问题。 - ssz

59

当添加一个可能与您的 compileSdkVersion 不兼容的外部库时,会出现此错误。

在添加 external library 时要小心。

我花了2天时间解决这个问题,最终按照以下步骤解决了:

  • 确保所有支持库与您的 build.gradle(Module:app) 中的 compileSdkVersion 相同,在我的情况下为 26enter image description here

  • 在 defaultConfig 类别中键入 multiDexEnabled true。 这是重要的一步。multiDexEnabled True image

  • 转到 File | Settings | Build, Execution, Deployment | Instant Run 并尝试启用/禁用即时运行以进行热交换... 然后单击 okayEnable Instant Run to Hot swap... Image

  • Sync 您的项目。

  • 最后,转到 Build | 单击 Rebuild Project

  • 注意:Rebuild Project 首先清理项目然后再重新构建。


这对我很有帮助,谢谢。 - Sumit Saxena
很高兴听到这个消息。 祝你在Android开发中愉快。 - Niamatullah Bakhshi
1
@YajliMaclo 很棒! - Niamatullah Bakhshi

51

尝试在Gradle中添加此内容

    android {
      defaultConfig {
        multiDexEnabled true
        }
   }

你尝试过在Android Studio中使缓存失效(invalidate cache)了吗?@Anguraj - Ishan Fernando
不仅是启用multidex的唯一步骤。在较新的API级别中也不需要。 - OneCricketeer

29

分辨率:

请参考此链接:由于minSdkVersion设置为低于20 ,因此有各种选项可以关闭警告:

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

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

如果你在build.gradle中设置的minSdkVersion大于20,请使用以下内容来关闭警告:

如果您的build.gradle文件中的minSdkVersion大于20,则使用以下内容以关闭警告:

  android {
      defaultConfig {
          ...
          minSdkVersion 21 
          targetSdkVersion 26
          multiDexEnabled true
      }
      ... }

按以下方式更新依赖关系:

     dependencies {
        implementation 'com.android.support:multidex:1.0.3'
     }

唯一的区别在于依赖关键词:

minSdkVersion小于20:使用compile

minSdkVersion大于20:使用implementation

  1. 希望这对您有所帮助,请点赞以表示感谢。感谢您抽出宝贵时间。
  2. 此外,如果您想要了解更多信息,可以查看链接中的第一段落详细解释为什么会出现这种情况?这个警告意味着什么。

24

尝试执行"Build -> Clean Project"操作,这对我解决了问题。


5
谢谢,这对我在一个Ionic/Cordova项目中运行cordova clean android很有用,同时我也安装了cordova-android-support-gradle-release插件。 - mkimmet
喜欢这种简单的解决方案!谢谢! - tonejac
当我使用React Native时,这个解决了我的问题。 - Michael Auderer
谢谢,这是解决这个问题最简单的方法,我花了三天时间来解决这个问题,你的答案解决了我所有的问题。再次感谢你,兄弟!!! - gautamsinh mori
谢谢。对我在Android Studio 4.1上的帮助很大,救了我的一天。 - Atif

7

For me, adding

multiDexEnabled true

并且。
packagingOptions {
        exclude 'META-INF/NOTICE' 
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/notice'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license'
        exclude 'META-INF/license.txt'
    }

将代码添加到应用级别的 Build.gradle 文件中解决了该问题。

6

打开您的模块级别的build.gradle文件,将以下代码添加到文件中:

    defaultConfig {
        ...
        minSdkVersion 15 
        targetSdkVersion 28
        multiDexEnabled true
    }
    ...
}

dependencies {
  implementation 'com.android.support:multidex:1.0.3'
}

这样就轻松解决了问题。请查看此文档


4
请使用以下方式将 multiDexEnabled 设置为 true。
{
 minSdkVersion 17
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}

并且
implementation 'com.android.support:multidex:1.0.3'

这个解决方案对我有用。

4

我尝试了上面提到的许多解决方案,包括将multiDexEnabled设置为true,但都没有对我起作用。

以下是对我有效的解决方案 - 将此代码复制到app\build.gradle文件中

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support' && requested.name != 'multidex') {
            details.useVersion "${rootProject.ext.supportLibVersion}"
        }
    }
}

在运行代码之前,请确保先执行gradlew clean操作。


1
我改了minSdkVersion,已经足够。 - Mr Special

2
对我来说,修复这个错误的方法是更改应用程序 gradle 依赖项中的一行。 从这里开始:
compile('com.google.android.gms:play-services-gcm:+') {
    force = true
}

转化为:

compile('com.google.android.gms:play-services-gcm:11.8.0') {
    force = true
}

只有那样对我起作用。我不需要添加force = true。 - Paulo Linhares - Packapps

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