com.android.builder.dexing.DexArchiveMergerException: 无法合并 dex - Android Studio 3.0 稳定

com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex - Android Studio 3.0 stable

我做了:

错误是:

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'

我对此很满意,none 我找到的答案很有效。终于找到了一个解决方案——在这里分享它,虽然我不能明确地告诉你如何找到有问题的依赖——也许你必须做一些试验和错误。

在我的 build.gradle(模块:应用程序)中,我添加了这个排除条款:

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

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

android {
    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
}

除了把 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'

在我的例子中,我将所有 com.android.support: 库更改为 27.1.0 并且它有效

我知道 update.I 在我的项目中遇到同样的问题已经太晚了。

可能的原因

  1. 如果您在项目中添加了模块,并且该模块具有支持库或任何 google 与您的应用版本不同的播放服务库。
  2. 如果您在项目中使用任何开源库,并且该库在内部使用您也在项目中使用的任何库。

解决方案

  • 如果您的项目中是情况 1,则更新您的库版本并使其在您的项目和模块中相同。
  • 使用下面的命令检查你的依赖关系树,看看是否有任何不匹配的依赖关系。

    ./gradlew :app:dependencies
    
  • 您可以从任何依赖项中排除特定模块,如下所示。

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

  • 您也可以删除特定模块。

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

我认为这是由于 Android Studio 的最新版本(当时)。 试了好久问题都没了

  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
}

我在创建Flutter Project的时候遇到了这个问题。我所做的是,打开 MyProject/android/app/build.gradle 文件并添加 multiDexEnabled true 在 defaultConfig 标签内,如下所示:

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
    }

当同样的问题出现时,我的 build.gradle 已经有以下内容:

android {
  ...

  defaultConfig {
   ...
  multiDexEnabled true 
   }
}

...

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

我清理了项目,然后 运行 再次构建,问题消失了。