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

Nau*_*que 23 merge android gradle dex android-studio-3.0

我做了:

  • 在"设置" - >"Android SDK" - >"SDK工具"中,检查并安装了Google Play服务v.46
  • 删除了文件夹/.gradle
  • "清洁工程"
  • "重建项目

错误是:

Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
Run Code Online (Sandbox Code Playgroud)

项目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
}
Run Code Online (Sandbox Code Playgroud)

App 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'
Run Code Online (Sandbox Code Playgroud)

请帮助我现在该怎么办

请不要标记它重复我已经尝试了几乎所有的解决方案

Ilk*_*Cat 25

  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
}
Run Code Online (Sandbox Code Playgroud)

  • 只有 multiDexEnabled true 就足够了,谢谢。 (4认同)

Vat*_*tel 17

我知道更新已经太晚了.我的项目也有同样的问题.

可能的原因

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

解决方案

  • 如果项目中是案例1,则更新库版本并在项目和模块中使其相同.
  • 使用以下命令检查依赖关系树,并查看依赖关系是否存在任何不匹配.

    ./gradlew :app:dependencies
    
    Run Code Online (Sandbox Code Playgroud)
  • 您可以从任何依赖项中排除特定模块,如下所示.

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

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

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

  • 大!这就是原因1。谢谢! (2认同)

Phi*_*bim 10

我得到了同样的问题,增加sourceCompatibilitytargetCompatibility对我的build.gradle帮助我:

android {
    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
}
Run Code Online (Sandbox Code Playgroud)


Nau*_*que 2

我认为这是由于 Android Studio 的最新版本(当时)造成的。我尝试了很长时间之后问题就消失了。