39

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'
Jonas Praem
  • 2,296
  • 5
  • 32
  • 53
Nauman Shafique
  • 433
  • 1
  • 6
  • 15

9 Answers9

38
  1. Go to <project>/app/ and open build.gradle file
  2. Add the following line to the defaultConfig and dependencies sections
android {
  ...

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

...

dependencies {
  ...
  implementation 'com.android.support:multidex:1.0.3'  // ADD THIS LINE
}
Ilker Cat
  • 1,862
  • 23
  • 17
21

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.

VP4Android
  • 576
  • 4
  • 17
13

I got the same problem, adding sourceCompatibility and targetCompatibility to my build.gradle helped me:

android {
    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
}
4

I was having a fit with this, and none of the answers I found worked. Finally found a solution -- sharing it here although I can't tell you definitively how to find which is the offending dependency -- maybe you'll have to do some trial and error.

In my build.gradle (Module:app) I added this exclude clause:

    compile ('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2')
        { exclude module: 'support-v4' }
veggiebenz
  • 389
  • 5
  • 12
3

I did exact as the hint in the image except changed 11.0.4 to 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

clemens
  • 16,716
  • 11
  • 50
  • 65
3

When the same issue appeared, my build.gradle already had the following was already there:

android {
  ...

  defaultConfig {
   ...
  multiDexEnabled true 
   }
}

...

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

I cleaned the project, and then run build again, and the issue was gone.

Fahima Mokhtari
  • 1,691
  • 2
  • 16
  • 30
2

I think it was due to Android Studio's latest version (at that time). I tried it after a long time then the issue gone.

Nauman Shafique
  • 433
  • 1
  • 6
  • 15
2

while creating Flutter Project, I encountered this issue. What I did is, opened MyProject/android/app/build.gradle file and added multiDexEnabled true inside defaultConfig tag as given below:

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
    }
Hanny
  • 1,322
  • 15
  • 20
0

in my case i change all com.android.support: libraries to 27.1.0 and it works

Marriage
  • 501
  • 3
  • 15