41

When I updated my Android Studio to 3.0 in the stable channel and ran the project, I started getting the below error.

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

I tried cleaning and rebuilding the project, but it didn't work. Any help will be appreciated.

Project level build.gradle

buildscript {
repositories {
    jcenter()
    google()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.0.0'
    classpath 'com.google.gms:google-services:3.1.0'


    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
} 
allprojects {
repositories {
    jcenter()
    google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

App level build.gradle

apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
defaultConfig {
    applicationId "com.med.app"
    minSdkVersion 21
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    resConfigs "auto"
    multiDexEnabled true

}
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.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'

//appcompat libraries
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:design:26.1.0'


//butterknife
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

//picasso
compile 'com.squareup.picasso:picasso:2.5.2'

//material edittext
compile 'com.rengwuxian.materialedittext:library:2.1.4'

// Retrofit & OkHttp & and OkHttpInterceptor & gson
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.squareup.okhttp3:logging-interceptor:3.8.0'

// FirebaseUI for Firebase Auth
compile 'com.firebaseui:firebase-ui-auth:3.1.0'
}
apply plugin: 'com.google.gms.google-services'

I have tried all the answers given but I am unable to solve this error. Please help.

22 Answers22

29

Add an explicit dependency to play-services-auth along with your firebase-ui-auth dependency:

// FirebaseUI for Firebase Auth
    compile 'com.firebaseui:firebase-ui-auth:3.1.0'
    compile 'com.google.android.gms:play-services-auth:11.4.2'

This is because firebase-ui-auth has a transitive dependency to play-services-auth and must be used with the corresponding version of play-services-auth. Please see this explanation.

firebase-ui-auth
|--- com.google.firebase:firebase-auth
|--- com.google.android.gms:play-services-auth

Earlier versions of the Gradle build tool did not include transitive dependencies so now versions can conflict with other play-services versions.

My Issue Explained and Answered (In case anyone wants to know)

When you upgrade to Android Studio 3.0 and update the gradle build tool version to 3.0.0, compiling of dependencies is now done differently than in earlier versions.

I recently encountered the same issue. I was using these dependencies which worked fine through Gradle version 2.3.3:

implementation 'org.apache.httpcomponents:httpmime:4.3.6'
implementation 'org.apache.httpcomponents:httpclient-android:4.3.5.1'

After the upgrade to gradle-build-version 3.0.0, I got the same error. Digint into it, I found that the transitive dependency of httpmime conflicted with the file httpclient-android was including.

Description

Let me explain this in detail. Earlier, while using gradle-tool-version 2.3.3, I was using httpclient-android to fetch and use the class named org.apache.http.entity.ContentType.java Expanding the transitive dependencies of org.apache.httpcomponents:httpmime:4.3.6 showed that it has org.apache.httpcomponents:httpcore:4.3.6 which is the same package I wanted to use. But while compiling or syncing the build, it was excluding org.apache.http.entity.ContentType.java so I needed to add this dependency which includes ContentType.java:

implementation 'org.apache.httpcomponents:httpclient-android:4.3.5.1'

Everything worked fine after that.

Once I upgraded the gradle-build-version to 3.0.0, things changed. It now included all transitive dependencies. So while compiling with the latest Android Studio with gradle-build-tool version 3.0.0, my ContentType.java was being compiled twice. Once from org.apache.httpcomponents:httpcore:4.3.6 (which is an implicit transitive dependency of httpmime) and again from org.apache.httpcomponents:httpclient-android:4.3.5.1 which I was using earlier.

To resolve this issue I had to remove the existing org.apache.httpcomponents:httpclient-android:4.3.5.1 dependency as httpmime would itself fetch the relevant class required for my application.

The solution for my situation: only use required dependencies and remove the httpclient-android

implementation 'org.apache.httpcomponents:httpmime:4.3.6'

Note that this is just the case for me. You'll need to dig into your own dependencies and apply the solution accordingly.

Allen Luce
  • 7,859
  • 3
  • 40
  • 53
Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107
  • Exactly, I was also stuck with similar issue. One of my project dependency and its transitive dependent library had a dependency on 'com.android.volley'. So this new gradle was having having issues while merging dex files. I fixed it by adding "exclude group: 'com.android.volley' in my gradle file. – Harish Rana Oct 27 '17 at 10:40
  • @Bhavesh Patadiya, Please help me here (https://stackoverflow.com/questions/46976279/dexarchivemergerexception-unable-to-merge-dex). I just got stuck and unable to resolve it. – Pardeep Sharma Oct 28 '17 at 10:19
  • Lucky me that your explanation happen to be related with org.apache.httpcomponents:httpclient-android and org.apache.httpcomponents:httpmime . I was getting crazy to find out about it. Thanks! – xarlymg89 Mar 22 '18 at 15:46
  • 4
    `you need to dig into the classes your are having an issue with` But how did you find out which classes you've been having an issue with? Did you run `./gradlew dependencies` and go through all transitive dependencies (marked as `xxx -> yyy` in the output)? – Denys Kniazhev-Support Ukraine Apr 02 '18 at 11:52
14

First of all I enabled multidex as suggested in previous comments.

Then, if the error continues, open the Gradle Console (click on "Show Console Output" icon at left of "Messages" section) and click on the link to recompile with Debug/Info/Stack options. This will show further details about the error.

In my case, the error "Unable to merge dex" was caused by duplicate entries in "com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0".

I manually removed the conflicting library from my project and executed the "Rebuild Project" (forcing to reload the library). This solved the issue.

Pablo Alfonso
  • 2,317
  • 1
  • 18
  • 24
  • 1
    Where exactly did you see duplicate entries? In a build.gradle file? The actual jar in a folder? Thanks – Russ Wheeler Oct 26 '17 at 23:24
  • I'm getting `com.android.dex.DexIndexOverflowException`, despite the fact I have multidex enabled and my project built fine before updating to Android Gradle Plugin 3.0. – AutonomousApps Oct 26 '17 at 23:50
  • I did as mentioned and got the following: Caused by: com.android.dex.DexException: Multiple dex files define Lcom/google/firebase/iid/zzd; I tried removing FirebaseUI library, rebuilding the project and then again copying the dependency. But it's not working. –  Oct 27 '17 at 05:49
  • Eres una pistola – dan_flo10 Oct 27 '17 at 15:32
  • It took a long time to pinpoint and eliminate the duplicate dependencies (which I feel gradle ought to do automatically), but recompiling with stack info (showing me which class was duplicated) was the tool I needed to be able to fix it at all. – Erhannis Nov 17 '17 at 16:36
3

I had this error:

com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

and ended up changing back my gradle in order to fix this issue.

app\build.gradle

android {
compileSdkVersion 25
//buildToolsVersion '26.0.2'
buildToolsVersion '25.0.3'//<< Changed back to old version before my studio 3.0 update
defaultConfig { ....

.\build.gradle

buildscript {
repositories {
    jcenter()
    google()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.3.3' //<< Changed back to old version before my studio 3.0 update
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

It's not ideal as it's back dating, but it's something which worked for me and should get me there until a possible patch is released.

3

Check out dependencies in your build.gradle (app) if you're using 2 (or more) libraries with the same name and different version. For example (in my case):

implementation files('src/main/libs/support-v4-24.1.1.jar')
implementation 'com.android.support:support-v4:27.0.2'

Remove one, then clean and rebuild. Also note that dependencies is outside buildscript.

JeffNhan
  • 363
  • 3
  • 3
2
android {
    defaultConfig {
       multiDexEnabled true
    }
}

add this line to the :gradle file

Marcello B.
  • 4,177
  • 11
  • 45
  • 65
Vaibhav pandey
  • 109
  • 2
  • 6
  • 3
    While this code may answer the question, providing additional context regarding **how** and **why** it solves the problem would improve the answer's long-term value. – Alexander Jan 14 '18 at 09:31
2

Sometimes this error happens, when same .jar library file is present in "libs" folder and at the same time we are trying to get the source code by adding line "compile " in app gradle file.

Any one of this, if we remove then we can overcome this error.

Hope this may helpful.

suhasini
  • 39
  • 4
1

i have same problem i resolved it put:

classpath 'com.google.gms:google-services:3.0.0'

in buildscript->dependencies

build.gradle

in my file i have:

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        classpath 'com.google.gms:google-services:3.0.0'
    }
}
1

Just Change your classpath to :

classpath 'com.android.tools.build:gradle:2.3.3'

and sycn your Gradle.

Hope this will help.

Gaurav
  • 89
  • 1
  • 6
  • 1
    While this may answer the question, it is better to explain the essential parts of the answer and possibly what was the problem with OPs code. – pirho Dec 09 '17 at 21:32
1

"all gsm libraries" Thanks, it helped to solve my problem, but not only the gsm libraries, but all google libraries must have the same version. I had this dexing error, because the com.android.support:recyclerview-v7 had a different version than the com.android.support:appcompat-v7

Android studio shows these lines with red underline in the build.gradle file.

0

I did exact as the hint in below screenshot, 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'

it works fine

Unable to merge dex

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

Seems like this error have many scenarios. In my case was a 1.8 java compile in build.gradle (app):

compileOptions {
    targetCompatibility 1.8
    sourceCompatibility 1.8
}

I removed and error was gone

Felipe Costa
  • 84
  • 1
  • 5
0

I changed the below from 11.6.0 to 11.8.0 and it worked.

compile 'com.google.android.gms:play-services-ads:11.6.0'

implementation 'com.google.android.gms:play-services-ads:11.8.0'
0

I was experiencing the same error with Firebase UI Database instead. Even after enabling multiDex as suggested in the other answers I was still getting the error. Then I came to know that there is a need for the Firebase UI and the Firebase Database to be of same corresponding versions as given on the Firebase UI GitHub repository.

Firebase UI GitHub

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Rohan Lekhwani
  • 470
  • 4
  • 14
0

Add this your gradle: implementation 'com.android.support:multidex:1.0.0'

Clean project and then rebuild.this works

ArunKumar
  • 1
  • 1
0

This might also be too late, But I think I have an answer too. Based on my recent trials, when compiling the application, ensure that you don't have a jar file and an'implementation'('compile' for 3.0.1 > gradle) of the same package in the same project. In my case, I had implementation 'org.jsoup:jsoup:1.11.2' and Jsoup jar in the same project. Rookie mistake but, I learnt.

Granson
  • 128
  • 1
  • 4
0

For those who are still struggling with this more recently and have added components. What caused it for me was adding:

compile 'android.arch.lifecycle:extensions:1.0.0' annotationProcessor 'android.arch.lifecycle:compiler:1.0.0'

What fixed it was updating it to

compile 'android.arch.lifecycle:extensions:1.1.1' annotationProcessor 'android.arch.lifecycle:compiler:1.1.1'

Hope that helps.

SlickDev
  • 607
  • 7
  • 7
0

By add this following code in build.gradle(app module) work for me

android {
      defaultConfig {
          multiDexEnabled true
      }
}

dependencies {
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'com.android.support:support-v4:26.1.0'
}
Arash Hatami
  • 5,297
  • 5
  • 39
  • 59
Yash Jain
  • 415
  • 2
  • 8
  • 17
0

i think it's because differ library depend same child library but the version is different so exclude one library's dependency like:

api (rootProject.ext.dependencies["bindingRecyclerView"]) {
    exclude group: 'com.android.support'
}
like
  • 1
0

In my case i had to do three things:

  1. As i was using firebase, make sure firebase and google play services had the same version.Initially play services had a lower version. Mainly 12.0.1 version helped

  2. Set this in app's level build.gradle

    android {  
        multiDexEnabled true   
    }
    
  3. Again in app's level build.gradle, add

    compileOptions{
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
    
Arash Hatami
  • 5,297
  • 5
  • 39
  • 59
Javac Ds
  • 21
  • 3
0

This link solved the issue for me.

First I set dependencies in my pubspec.yaml to

dependencies:
  flutter:
    sdk: flutter
  cloud_firestore: ^0.8.2 

and ran flutter packages get in my IDE's terminal.

Also I had to change the minimum target SDK version:

  1. Open android/app/build.gradle, then find the line that says minSdkVersion 16.
  2. Change that line to minSdkVersion 21.
  3. Save the file.

Also, I had to open android/app/build.gradle, then add the following line as the last line in the file: apply plugin: 'com.google.gms.google-services'

Next, I had to open android/build.gradle, then inside the buildscript tag, add a new dependency:

buildscript {
   repositories {
       // ...
   }

   dependencies {
       // ...
       classpath 'com.google.gms:google-services:3.2.1'   // new
   }
}

After this my app finally ran on the android emulator.

The link has a more complete walkthrough if you get stuck.

Casey Schneider
  • 885
  • 6
  • 13
0

Strange situation, but the multiDexEnabled didn't help me, but the problem solved after removing the Gradle directory and rebuild the application.

-1

this is my gradle project

android { compileSdkVersion 26 buildToolsVersion "27.0.3"

defaultConfig {
    applicationId "net.mobilegts.candyjump"
    minSdkVersion 14
    targetSdkVersion 23

    ndk {
        moduleName "player_shared"
    }
}
sourceSets {
    main {
        jni.srcDirs = []
    }
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}

}

Midou Dz
  • 1
  • 1
  • this is dependencies – Midou Dz Apr 14 '18 at 09:09
  • dependencies { compile 'com.google.android.gms:play-services:12.0.0' compile files('libs/dagger-1.2.2.jar') compile files('libs/javax.inject-1.jar') compile files('libs/nineoldandroids-2.4.0.jar') compile files('libs/support-v4-19.0.1.jar') } – Midou Dz Apr 14 '18 at 09:09
  • 1
    I think you'd better edit your answer again for others understand your idea clearly – Cyrus Apr 21 '18 at 03:32