Unable to merge dex

400,313

Solution 1

I had the same problem when I update from com.google.android.gms:play-services:11.2.2 to com.google.android.gms:play-services:11.4.0. This solved it for me:

  1. clean
  2. rebuild

Solution 2

I tried all the above and none of them helps. finally, I find this work for me:

app/build.gradle:

android {
    defaultConfig {
       multiDexEnabled true
    }
}

Solution 3

Pay attention to Warnings!

Sometimes you only need to eliminate warnings and the error will be disappeared automatically. See below special case:


I had these two dependencies in my module-level build.gradle file:

implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'

and Studio had warned (in addition to dex merging problem):

All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes). Found versions 27.0.2, 21.0.3. Examples include com.android.support:animated-vector-drawable:27.0.2 and com.android.support:support-v4:21.0.3

So I explicitly determined the version of com.android.support:support-v4 (see here for details) and both problems (the warning and the one related to dex merging) solved:

implementation 'com.android.support:support-v4:27.0.2'  // Added this line (according to above warning message)
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'

See below comments for other similar situations.

Solution 4

In my case, Unfortunately, neither Michel's nor Suragch's solutions worked for me.

So I solved this issue by doing the following:

In gradle:3.0 the compile configuration is now deprecated and should be replaced by implementation or api. For more information you can read here You can read the official docs at Gradle Build Tool

The compile configuration still exists but should not be used as it will not offer the guarantees that the api and implementation configurations provide.

it's better to use implementation or api rather compile

just replace compile with implementation, debugCompile with debugImplementation, testCompile with testImplementation and androidtestcompile with androidTestImplementation

For example: Instead of this

compile 'com.android.support:appcompat-v7:26.0.2'
compile 'com.android.support:support-v4:26.1.0'
compile 'com.github.bumptech.glide:glide:4.0.0'

use like this

implementation 'com.android.support:appcompat-v7:26.0.2'
implementation 'com.android.support:support-v4:26.1.0'
implementation 'com.github.bumptech.glide:glide:4.0.0'

After that

  • Delete the .gradle folder inside your project ( Note that, in order to see .gradle, you need to switch to the "Project" view in the navigator on the top left )
  • Delete all the build folders and the gradle cache.
  • From the Build menu, press the Clean Project button.
  • After task completed, press the Rebuild Project button from the Build menu.

Hope it will helps !

Solution 5

  1. Delete the .gradle directory.

  2. Run your app again.

Notes

  • The .gradle directory is in your project's root folder. (You may have to show hidden files first.)
  • I have to do this every time I update a dependency module using Android 3.0. (More recent releases of Android Studio 3 seem to have resolved the problem.)
Share:
400,313
Parad0X
Author by

Parad0X

Updated on January 14, 2021

Comments

  • Parad0X
    Parad0X over 3 years

    I have Android Studio Beta. I created a new project with compile my old modules but when I tried launching the app it did not launch with the message:

    Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
    

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

    But I don't know how to solve this error. I googled this for hours but with no success.

    My project gradle:

        // Top-level build file where you can add configuration options common to all sub-projects/modules.
    
    buildscript {
        repositories {
            jcenter()
            google()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.0.0-beta6'
            classpath "io.realm:realm-gradle-plugin:3.7.1"
            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
    }
    

    My app gradle:

        apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 26
        buildToolsVersion "26.0.1"
        defaultConfig {
            applicationId "parad0x.sk.onlyforyou"
            minSdkVersion 21
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            multiDexEnabled true
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
            debug {
            }
        }
        compileOptions {
            targetCompatibility 1.7
            sourceCompatibility 1.7
        }
        packagingOptions {
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/NOTICE'
        }
        lintOptions {
            checkReleaseBuilds false
        }
        productFlavors {
        }
    }
    
    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        //noinspection GradleCompatible
        compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
        compile project(path: ':loginregisterview')
    
    
    }
    

    And my module gradle:

        apply plugin: 'com.android.library'
    apply plugin: 'realm-android'
    
    android {
        compileSdkVersion 26
        buildToolsVersion "26.0.1"
    
        defaultConfig {
            minSdkVersion 19
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
    
            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.0.2'
        compile 'com.android.support:support-v4:26.1.0'
        compile 'com.github.bumptech.glide:glide:4.0.0'
        testCompile 'junit:junit:4.12'
        compile project(path: ':parser')
    
    }
    

    My second module:

         apply plugin: 'com.android.library'
    apply plugin: 'realm-android'
    
    android {
        compileSdkVersion 26
        buildToolsVersion "26.0.1"
        defaultConfig {
            minSdkVersion 14
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    
        }
    
        realm {
            syncEnabled = true
        }
        useLibrary 'org.apache.http.legacy'
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        productFlavors {
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        androidTestCompile 'junit:junit:4.12'
        //  compile 'com.android.support:appcompat-v7:23.1.0'
    
        //   compile 'com.fasterxml.jackson.core:jackson-core:2.9.0'
     //   compile 'com.fasterxml.jackson.core:jackson-annotations:2.9.0'
     //   compile 'com.fasterxml.jackson.core:jackson-databind:2.9.0'
        compile 'com.google.code.gson:gson:2.6.2'
    }
    

    ____________finding_________

    When I did not import the second module (parser) the app did not crash on dex but when the module was not imported app did not work. :D :D

  • Vasili Fedotov
    Vasili Fedotov over 6 years
    this helped me when I also did a Invalidate Cache and restart
  • Brendon Whateley
    Brendon Whateley over 6 years
    Thanks. I think the problem was that most answers are not accounting for including JAR files from other sources that you cannot rebuild. multiDex seems to allow the otherwise incompatible code to be combined.
  • IgorGanapolsky
    IgorGanapolsky over 6 years
    Did OP mention Play Services?
  • Jonathan
    Jonathan over 6 years
    The approved answer did. If this issue is surfacing without that scenario then I don't know!
  • itzhar
    itzhar over 6 years
    You only delay the real problem with that solution. probably ver 11.4.0 contains less methods. See my answer below. you will got this error in the next dependence you'll add I guess
  • Yuri Brigance
    Yuri Brigance over 6 years
    This solves my issue, where multiDexEnabled true alone did not
  • Shayno
    Shayno over 6 years
    Thanks, all I had to do was change compile to implementation and it built successfully, didn't have to complete the other steps. The build started failing after I updated the Facebook SDK
  • issamux
    issamux over 6 years
    same isse with com.google.android.gms:play-services-location:11.6.0 ... this is not a valid solution
  • Alex
    Alex over 6 years
    Worked for me - insights into why?
  • durgeshtrivedi
    durgeshtrivedi over 6 years
    I have the same issue I was using playServiceVersion as 11.4.0 implementation "com.google.firebase:firebase-core:11.4.0" change it to implementation "com.google.firebase:firebase-core:11.6.2" fixed my issue . So please verify the version dependency for different lib
  • NoWar
    NoWar over 6 years
    How to know it? Don't compile/implement two versions of same dependencies.
  • global_warming
    global_warming over 6 years
    'com.google.firebase:firebase-messaging:10.0.1' with different version 'com.google.firebase:firebase-messaging:11.0.2' or a .jar file in libs with firebase-messaging**-**12.3.2.jar
  • hamou92
    hamou92 over 6 years
    This solution worked for me, but in addition, I had to explicitly enable multidex for API <= 21 developer.android.com/studio/build/multidex.html
  • angryITguy
    angryITguy over 6 years
    This solution worked for me when I had a compatibility problem using google analytics libraries. In AS view the android project navigator pane as "Project", you should see "External Libraries", if you expand the external libraries, you'll see all the jars and modules used to compile the project. It should help you identify incompatible module combinations.
  • Phil
    Phil over 6 years
    Thanks so much, this helped me a lot!
  • callOfCode
    callOfCode over 6 years
    So glad to find this answer. Thanks!
  • Gary Bak
    Gary Bak over 6 years
    The folder to delete should be .gradle and not ./gradle
  • Zon
    Zon over 6 years
    Also testImplementation instead of testCompile.
  • King of Masses
    King of Masses over 6 years
    I already mentioned it in my answer. anyways thanks for saying
  • Chris Schaller
    Chris Schaller over 6 years
    This should probably be a comment to the answer from @Mansour The duplicate references will make it fail. This response on its own does not offer new insight on this issue. OP has already accepted an answer on this thread so make sure your answer explains why it is different to the others and is of a similar high quality with links to reference material and in-depth explanation. Some of the other answers to this question are really good examples of well structured and referenced posts.
  • lsrom
    lsrom over 6 years
    Thank you, this fixed the issue for me. It was originally caused by adding eu.inloop:androidviewmodel:1.3.4
  • snodnipper
    snodnipper over 6 years
    +1 - I had the support lib, rxjava and a 3rd party dependency (13k lines). My eyes nearly popped out when I saw the method count.
  • Stoycho Andreev
    Stoycho Andreev over 6 years
    Yep, it seems that Kotlin introduction could break the build if the annotations are not excluded.
  • hanzolo
    hanzolo over 6 years
    I was in disbelief when this fixed my issue.. i had tried updating everything, removing all compile keywords, updating a few of the suspect ilb versions.. when i finally did this, it worked..
  • Amin Keshavarzian
    Amin Keshavarzian over 6 years
    Exactly, I just wish the error could have been more precise
  • Naveen Kumar M
    Naveen Kumar M over 6 years
    Thanks. This was the fix for me. I struggle 3 days to identify my problem.
  • Johnny
    Johnny over 6 years
    Thanks a lot. I had the same issue. I was using v26, updated to sdk 27 and solved the problem.
  • Nabin Bhandari
    Nabin Bhandari over 6 years
    Why would implementation work and compile won't? They should have made it backward compatible. Anyways thanks a lot!
  • Sumit Saxena
    Sumit Saxena over 6 years
    If above code is not work in case of "compileSdkVersion 26", try this.. go to - build.gradle(Module:app) - add " multiDexEnabled true " in defaultConfig category and last step Go to File | Settings | Build, Execution, Deployment | Instant Run and try to Enable/Disable
  • Michael Richardson
    Michael Richardson over 6 years
    The only answer that actually worked for me. Thanks!
  • KTCO
    KTCO over 6 years
    Try upgrading to Kotlin plugin version 1.2.30 instead (just released). Now Instant Run can be enabled again too!
  • alashow
    alashow over 6 years
    This should be the correct answer. My builds stopped working after adding one library (airbnb/epoxy) which had a dependency to support:design:26.1.0, and I didn't have that dependency yet. My others support libraries version was 27.1.0. After adding support:design dependency with 27.1.0 version, my issue was resolved.
  • sg7
    sg7 over 6 years
    Could you elaborate?
  • Ante
    Ante over 6 years
    tried all other solutions, in the end only this helped :/
  • Nithinjith
    Nithinjith over 6 years
    Thank you. You save my day.
  • hopia
    hopia over 6 years
    This solved my issue. I was building an internal android library project and under its libs folder, I had two jar files, one of which is just a backup of the other one but with a different filename.
  • vinidog
    vinidog over 6 years
    Error:Execution failed for task ':app:processDebugGoogleServices'. > Please fix the version conflict either by updating the version of the google-services plugin (information about the latest version is available at bintray.com/android/android-tools/…) or updating the version of com.google.android.gms to 11.4.0.
  • Alyoshak
    Alyoshak about 6 years
    Where is the "gradle cache"?
  • Bipin Bharti
    Bipin Bharti about 6 years
    @Ali Gurelli Thank you so much i tried many solutions but only this work only.
  • John
    John about 6 years
    DEspite being more relevant than the accepted answer I still get "Unable to merge dex" message
  • Elsunhoty
    Elsunhoty about 6 years
    Thanx But For me I use Version details.useVersion '15.0.0'
  • Dinuka Salwathura
    Dinuka Salwathura about 6 years
    removing compile fileTree(include: ['*.jar'], dir: 'libs') fixed for me too.. thanks (y)
  • Eben Watts
    Eben Watts about 6 years
    I also had the same problem but I realized that solve my problem at that moment but in the future updates, things might actually change.
  • R World
    R World about 6 years
    Thanks for solution. Same issue happened to me . google tap and pay was 10.0.0 and rest all libs are on latest version
  • mindeh
    mindeh about 6 years
    Also needed to add multiDexEnabled true for all modules
  • panda
    panda almost 6 years
    The most useful way for this problem. One I want to mention here is that, you can use 'gradlew app:transformDexArchiveWithExternalLibsDexMergerForDebug --stacktrace' cmd for more details. I save me, found I have to enable multidex. Hope helpful.
  • Sankalp
    Sankalp almost 6 years
    Worked Smoothly Thanks
  • Atul
    Atul almost 6 years
    Didn't work for me. With this change I got different error: Execution failed for task ':app:transformClassesWithMultidexlistForDebug
  • Aleksandr A
    Aleksandr A over 5 years
    I think this should be first check before adding multiDexEnabled true, and trying other answers. Thanks!
  • sud007
    sud007 over 5 years
    Yes for 5 hours i was dwelling with totally different answers. But somehow I realized it was Multidex issue and landed here. I was true. Thanks man
  • Danger
    Danger over 5 years
    Thanks, It is the coolest answer. It should be on top of the search. Worked for me.
  • Dwhitz
    Dwhitz about 5 years
    Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.
  • Ben-Hur Batista
    Ben-Hur Batista about 5 years
    I need to tell, you saved my morning!
  • Bay
    Bay almost 5 years
    There is no Instant Run in the path called "Build, Execution, Deployment" your answer has no meaning.
  • Rasel
    Rasel almost 5 years
    @Bay in Android studio 3.5 Instant Run is not available.My answer updated.
  • Daniel
    Daniel over 3 years
    @DINA, android.enableD8 = false is deprecated.