com.android.builder.dexing.DexArchiveMergerException: 合并dex文件失败 - Android Studio 3.0稳定版

39

我做了以下工作:

  • 在“设置”->“Android SDK”->“SDK 工具”中,勾选并安装了 Google Play 服务的 v.46 版本
  • 删除了文件夹 /.gradle
  • 执行 "Clean Project"
  • 执行 "Rebuild Project"

错误为:

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

项目 build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'

        classpath 'com.google.gms:google-services:3.1.0'
    }
}

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

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

应用程序的build.gradle文件

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.asanquran.mnaum.quranasaanurdutarjuma"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 3
        versionName "1.3"
        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.+'


    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.android.gms:play-services-ads:11.4.2'
    compile 'com.github.barteksc:android-pdf-viewer:2.3.0'
    compile 'org.apache.commons:commons-io:1.3.2'
    compile 'com.google.firebase:firebase-ads:11.4.2'
    compile 'com.google.firebase:firebase-messaging:11.4.2'
    compile 'com.google.firebase:firebase-storage:11.4.2'
    apply plugin: 'com.google.gms.google-services'
    testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'

我已经尝试过了,但是它仍然给出了一长串错误列表。 - Nauman Shafique
展示你的错误 - Sudheesh R
不要在你的依赖中使用apply plugin: 'com.google.gms.google-services'。请查看我的编辑以获取你的问题的答案。 - Sudheesh R
抱歉,我还没有找到任何解决方案。 - Nauman Shafique
1
尝试使用以下命令编译 'com.android.support:appcompat-v7:26.1.0' - Fathy
显示剩余4条评论
9个回答

38
  1. 前往<project>/app/并打开build.gradle文件
  2. defaultConfigdependencies部分添加以下行
android {
  ...

  defaultConfig {
    ...
    multiDexEnabled true // ADD THIS LINE
  }
}

...

dependencies {
  ...
  implementation 'com.android.support:multidex:1.0.3'  // ADD THIS LINE
}

6
只需要将 multiDexEnabled 设为 true 就可以了,谢谢。 - Kvlknctk
出现了react-native-pdf的错误。这个解决方案对我有效。 - Dami
谢谢,我更新了最小SDK版本,然后遇到了这个错误。对我有用。再次感谢。 - Pulkit Prajapat

21

我知道现在更新有点晚了。我的项目也遇到了同样的问题。

可能的原因

  1. 如果你在项目中添加了模块,并且该模块具有支持库或任何 Google Play 服务库,而这些库的版本与你的应用程序不同。
  2. 如果你正在项目中使用任何开源库,而该库在内部使用了你也在项目中使用的任何库。

解决方法

  • 如果是第1种情况,请更新库版本并使其在项目和模块中相同。
  • 使用以下命令检查你的依赖树,看看是否存在依赖项不匹配的情况。

./gradlew :app:dependencies
  • 你可以像下面这样从任何依赖项中排除特定的模块。

  • implementation('com.google.android.ads.consent:consent-library:1.0.4') {
      transitive = true
      exclude group: "com.android.support"
    } 
    
  • 在上面的例子中,它将从consent-library 依赖项中排除 com.android.support 组。

  • 你也可以删除特定的模块。

  •  compile ('junit:junit:4.12'){
      exclude group: 'org.hamcrest', module:'hamcrest-core'
      }
    
  • 在上面的例子中,它将从org.hamcrest排除hamcrest-core


  • 2
    太好了!这是第一原因。谢谢! - Mike Keskinov

    13

    我遇到了同样的问题,在我的build.gradle中添加sourceCompatibilitytargetCompatibility帮助了我:

    android {
        compileOptions {
            sourceCompatibility 1.8
            targetCompatibility 1.8
        }
    }
    

    4

    我在这个问题上遇到了困难,找到的答案都没有用。最终我找到了一个解决方案--尽管我不能确定如何找到哪个依赖项是有问题的--也许你需要进行一些试验和错误。

    在我的build.gradle(模块:app)中,我添加了这个排除子句:

        compile ('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2')
            { exclude module: 'support-v4' }
    

    3

    我按照图片中的提示进行操作,唯一不同的是将11.0.4更改为11.8.0。

    compile 'com.google.android.gms:play-services-base:11.8.0'
    compile 'com.google.android.gms:play-services:11.8.0'
    

    Unable to merge dex


    3
    当同样的问题出现时,我的 build.gradle 已经有以下内容:
    android {
      ...
    
      defaultConfig {
       ...
      multiDexEnabled true 
       }
    }
    
    ...
    
    dependencies {
    ...
    implementation 'com.android.support:multidex:1.0.3'  
    }
    

    我清理了项目,然后重新构建,问题就消失了。


    请问您是如何清理它的? - helloworld331
    从Android Studio中,在菜单中,我猜在构建下面,你会找到“清理”。@helloworld331 - Fahima Mokhtari
    谢谢@fahima,我想我已经找到了一种使用cli的方法,它在android文件夹中,命令是./gradlew clean。但是我很感激你的评论,让我知道了另一种方法 :) - helloworld331

    2

    我认为这是由于Android Studio的最新版本(当时)引起的。 我很久没用它了,然后问题就解决了。


    2
    在创建Flutter项目时,我遇到了这个问题。我的解决方法是打开MyProject/android/app/build.gradle文件,并在defaultConfig标签内添加以下内容:multiDexEnabledtrue
    defaultConfig {
            // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
            applicationId "Your Application ID Here"
            minSdkVersion 16
            targetSdkVersion 28
            versionCode flutterVersionCode.toInteger()
            versionName flutterVersionName
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            multiDexEnabled true
        }
    

    0
    在我的情况下,我将所有的 com.android.support: 库更改为 27.1.0,然后它就可以工作了。

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