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

Nauman Shafique picture Nauman Shafique · Oct 27, 2017 · Viewed 55.2k times · Source

I made:

  • In "Settings"->"Android SDK"->"SDK Tools" Google Play services is checked and installed v.46
  • Removed folder /.gradle
  • "Clean Project"
  • "Rebuild Project

Error is:

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

Project 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
}

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'

Answer

VP4Android picture VP4Android · Jun 28, 2018

I know it's too late to update.I had same issue on my project.

Possible Reasons

  1. If you have added module in your project and that module has support libraries or any google play services libs which has different version then your app.
  2. If you are using any open source library in your project and that library internally using any of libraries that your are also using in your project.

Solutions

  • If it is case 1 in your project then update your library versions and make it same in your project and module.
  • Check your dependencies tree using below command and see if any mismatch in dependencies.

    ./gradlew :app:dependencies
    
  • You can exclude particular module from any dependencies like below.

    implementation('com.google.android.ads.consent:consent-library:1.0.4') {
      transitive = true
      exclude group: "com.android.support"
    } 
    
  • In above example, It will exclude the com.android.support group from consent-library dependencies.

  • You can also remove particular module as well.

     compile ('junit:junit:4.12'){
      exclude group: 'org.hamcrest', module:'hamcrest-core'
      }
    
  • In above example it will exclude hamcrest-core from org.hamcrest.