Android Studio 3.0 Execution failed for task: unable to merge dex

127,685

Solution 1

For Cordova based project, run cordova clean android before build again, as @mkimmet suggested.

Solution 2

This error happens when you add an external library which may not be compatible with your compileSdkVersion .

Be careful when you are adding an external library.

I spent 2 days on this problem and finally it got solved following these steps.

  • Make sure all your support libraries are same as compileSdkVersion of your build.gradle(Module:app) in my case it is 26. enter image description here

  • In your defaultConfig category type multiDexEnabled true. This is the important part. multiDexEnabled True image

  • Go to File | Settings | Build, Execution, Deployment | Instant Run and try to Enable/Disable Instant Run to hot swap... and click okay Enable Instant Run to Hot swap... Image

  • Sync Your project.

  • Lastly, Go to Build | click on Rebuild Project.

  • Note: Rebuild Project first cleans and then builds the project.

Solution 3

Try to add this in gradle

    android {
      defaultConfig {
        multiDexEnabled true
        }
   }

Solution 4

Resolution:

Refer to this link: As there are various options to shut the warning off depending on the minSdkVersion, it is set below 20:

 android {
     defaultConfig {
         ...
         minSdkVersion 15 
         targetSdkVersion 26
         multiDexEnabled true
     }
     ... }

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

If you have a minSdkVersion greater than 20 in your build.gradle set use the following to shut down the warning:

  android {
      defaultConfig {
          ...
          minSdkVersion 21 
          targetSdkVersion 26
          multiDexEnabled true
      }
      ... }

Update dependencies as follows:

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

Again the only difference is the keywords in dependencies:

minSdkVersion below 20: use compile

minSdkVersion above 20: use implementation

  1. I hope this was helpful, please upvote if it solved your issue, Thank you for your time.
  2. Also for more info, on why this occurs, please read the first paragraph in the link, it will explain thoroughly why? and what does this warning mean.

Solution 5

Simply try doing a "Build -> Clean Project". That solved the problem for me.

Share:
127,685
Anguraj
Author by

Anguraj

Updated on December 14, 2020

Comments

  • Anguraj
    Anguraj over 3 years

    android studio was getting build error while build execution with following:

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

    My app:build.gradle file:

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 26
        buildToolsVersion '26.0.2'
        defaultConfig {
            applicationId "com.pdroid.foodieschoice"
            minSdkVersion 16
            targetSdkVersion 25
            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.constraint:constraint-layout:1.0.2'
        compile 'com.firebaseui:firebase-ui-auth:2.3.0'
    
        testCompile 'junit:junit:4.12'
    }
    configurations.all {
        resolutionStrategy {
            force 'com.android.support:appcompat-v7:26.0.1'
            force 'com.android.support:support-compat:26.0.1'
            force 'com.android.support:support-core-ui:26.0.1'
            force 'com.android.support:support-annotations:26.0.1'
            force 'com.android.support:recyclerview-v7:26.0.1'
        }
    }
    
    apply plugin: 'com.google.gms.google-services'
    

    any solutions

    Edit: i have done with sample through firebase github site and solved

  • Vineet Jain
    Vineet Jain over 6 years
    Hope you have cleared or invalidated your cache and restarted Android Studio ? For me Android Studio 3 seems buggy as compared to the previous versions.
  • 68060
    68060 over 6 years
    anyone ever fix this? cannot get past it!
  • Ishan Fernando
    Ishan Fernando over 6 years
    Did you tried to invalidate cache in android studio? @Anguraj
  • OneCricketeer
    OneCricketeer over 6 years
    Not the only step to enable multidex. Also not needed in newer API levels
  • Sumit Saxena
    Sumit Saxena over 6 years
    it is helpful me , Thank you.
  • Niamatullah Bakhshi
    Niamatullah Bakhshi over 6 years
    glad to hear that. Happy Androiding.
  • Paulo Linhares - Packapps
    Paulo Linhares - Packapps about 6 years
    Only that worked for me. I did not need to add force = true
  • mkimmet
    mkimmet about 6 years
    Thanks this worked for me in an Ionic/Cordova project by running cordova clean android while I also had cordova-android-support-gradle-release plugin installed.
  • tonejac
    tonejac about 6 years
    Love it when something simple fixes things!! Thanks!
  • Pradeep Sheoran
    Pradeep Sheoran almost 6 years
    its a dependency put it into gradle
  • pamekar
    pamekar over 5 years
    Someone please mark this answer right. It solves the problem.
  • Michael Auderer
    Michael Auderer over 5 years
    This fixed it for me when working with React Native.
  • Niamatullah Bakhshi
    Niamatullah Bakhshi over 5 years
    @YajliMaclo great!
  • Mr Special
    Mr Special over 4 years
    I changed minSdkVersion, it's enough
  • ssz
    ssz almost 4 years
    For me, installing the 'cordova-plugin-androidx' and 'cordova-plugin-androidx-adapter' sovled the problem
  • gautamsinh mori
    gautamsinh mori almost 4 years
    Thanks, it's the easiest solution for this issue, I spent my 3 days to solve this issue and your answer solves my all problem. Once again thank you my bro!!!
  • Atif
    Atif over 3 years
    Thanks. Helped me on Android Studio 4.1. Saved my day.
  • AndroLogiciels
    AndroLogiciels about 2 years
    For me, just add the line multiDexEnabled=true in defaultConfig