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

AndroidAndroid Studio-3.0Build Error

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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 - Android

Try to add this in gradle

    android {
      defaultConfig {
        multiDexEnabled true
        }
   }

Solution 4 - Android

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 - Android

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

Solution 6 - Android

For me, adding

multiDexEnabled true

and

packagingOptions {
        exclude 'META-INF/NOTICE' 
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/notice'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license'
        exclude 'META-INF/license.txt'
    }

into the app level Build.gradle file solved the issue

Solution 7 - Android

Go to your module level build.gradle file and add the following lines to the code

    defaultConfig {
        ...
        minSdkVersion 15 
        targetSdkVersion 28
        multiDexEnabled true
    }
    ...
}

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

That solved the problem easily. Check this documentation

Solution 8 - Android

Use multiDexEnabled true as below.

{
 minSdkVersion 17
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}

and

implementation 'com.android.support:multidex:1.0.3'

this Solution worked for me.

Solution 9 - Android

I tried many solutions as mentioned above including the multiDexEnabled true but none of that worked for me.

Here is the solution which worked for me - copy this code in app\build.gradle file

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support' && requested.name != 'multidex') {
            details.useVersion "${rootProject.ext.supportLibVersion}"
        }
    }
}

Make sure to run gradlew clean before running the code

Solution 10 - Android

Same thing happened to me, figured out that I was in the middle of updating to AS 3.0.1 and after I did the update, cleaned and rebuilt the app, everything was fixed.

Solution 11 - Android

For me what fixed this error was changing one line in my app's gradle dependencies.

from this:

compile('com.google.android.gms:play-services-gcm:+') {
    force = true
}

To this:

compile('com.google.android.gms:play-services-gcm:11.8.0') {
    force = true
}

Solution 12 - Android

Also be sure your app is subclassing MultiDexApplication

import android.support.multidex.MultiDexApplication

class App : MultiDexApplication()

or if not subclassing Application class, add to AndroidManifest.xml

<application
  android:name="android.support.multidex.MultiDexApplication"

Solution 13 - Android

This is what works for me. clean prepare and run.

cordova clean android;
ionic cordova prepare andriod;
ionic cordova run andriod;

hope it helps.

Solution 14 - Android

Are you in between SDK or platform updates? If yes complete those completely and then try to continue. I normally update the individual packages that need to be updated as to taking the entire update which could go up to 2.5 GB sometimes. Doing this complete update sometimes fails. I had a number of SDK updates once I upgraded to Android Studio 3.0 and was getting the above error since all packages had not been updated. Once I updated all packages the above error was gone.

Solution 15 - Android

In my case changing firebase library version from 2.6.0 to 2.4.2 fixed the issue

Solution 16 - Android

For me, the problem was the use of Java 1.8 in a module, but not in the app module. I added this to the app build gradle and worked:

android{
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

Solution 17 - Android

Same problem. I have enabled multidex: defaultConfig { applicationId "xxx.xxx.xxxx" minSdkVersion 24 targetSdkVersion 26 multiDexEnabled true

I have cleared cache, ran gradle clean, rebuild, make, tried to make sure no conflicts in imported libraries (Removed all transitive dependencies) and made all updates. Still:

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

Turns out the build did not like: implementation 'org.slf4j:slf4j-android:1.7.21'

Solution 18 - Android

When the version of Android Studio is 3.0.1, Gradle Version is 4.1 and Android PluginVersion is 3.0.0, it will encounter this problem. Then I downgrade Gradle Version is 3.3, Android Android is zero, there is no such problem.

Solution 19 - Android

I had this when requested SDK version didn't match the dependencies. You can click the line highlighted and fix this clicking the red light bulb. No need to find the actual version, just let the IDE figure it out for you. And add google repo to maven config.

Solution 20 - Android

I was receiving the same error and in my case, the error was resolved when I fixed a build error which was associated with a different build variant than the one I was currently building.

I was building the build variant I was looking at just fine with no errors, but attempting to debug caused a app:transformDexArchiveWithExternalLibsDexMergerForDebug error. Once I switched to build the other build variant, I caught my error in the build process and fixed it. This seemed to resolve my app:transformDexArchiveWithExternalLibsDexMergerForDebug issue for all build variants.

Note that this error wasn't within the referenced external module but within a distinct source set of a build variant which referenced an external module. Hope that's helpful to someone who may be seeing the same case as me!

Solution 21 - Android

I Have Done and fixed this issue by just doing this jobs in my code

Open ->build.gradle Change value from

compile 'com.google.code.gson:gson:2.6.1'

to

compile 'com.google.code.gson:gson:2.8.2'

Solution 22 - Android

To remove this Dex issue just implement one dependency. This issue occur when we are using multiple different service from the same server. Suppose we are using ads and Firestore in a project and both have a repository maven. so we need to call different data with on repository we need dex dependency to implement. The new update Dependency:-

 implementation 'com.android.support:multidex:1.0.3'

I'm sure it will resolve your issue permanent

Solution 23 - Android

1)Please add multiDexEnabled true
2)if you get compilation error then check your app level build.gradle weather same dependencies are implemented with different versions.
3)Remove one if you find same dependencies.

android { compileSdkVersion 28 defaultConfig {

    minSdkVersion 17
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}

hope it works fine :) Happy Coding

Solution 24 - Android

The easiest way to avoid suck kind of error is:

-Change library combilesdkversion as same as your app compilesdkversion

-Change library's supportLibrary version as same as your build.gradle(app)

Solution 25 - Android

I also got the similar error.

> Problem :

enter image description here

> Solution :

Main root cause for this issue ismultiDex is not enabled. So in the Project/android/app/build.gradle, enable the multiDex

For further information refer the documentation: https://developer.android.com/studio/build/multidex#mdex-gradle

enter image description here

Solution 26 - Android

You can always revert to DX for now via this setting in your project's gradle.properties file:

android.enableD8=false

For more info, see https://android-developers.googleblog.com/2018/04/android-studio-switching-to-d8-dexer.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAngurajView Question on Stackoverflow
Solution 1 - Androidsulaiman sudirmanView Answer on Stackoverflow
Solution 2 - AndroidNiamatullah BakhshiView Answer on Stackoverflow
Solution 3 - AndroidIshan FernandoView Answer on Stackoverflow
Solution 4 - AndroidModesto CabreraView Answer on Stackoverflow
Solution 5 - AndroidAnasView Answer on Stackoverflow
Solution 6 - AndroidMd Nakibul HassanView Answer on Stackoverflow
Solution 7 - AndroidMahiView Answer on Stackoverflow
Solution 8 - AndroidSuresh kumarView Answer on Stackoverflow
Solution 9 - AndroidRajView Answer on Stackoverflow
Solution 10 - AndroidTian PretoriusView Answer on Stackoverflow
Solution 11 - AndroidHedeHView Answer on Stackoverflow
Solution 12 - Android1192805View Answer on Stackoverflow
Solution 13 - AndroidSankha KarunasekaraView Answer on Stackoverflow
Solution 14 - AndroidVineet JainView Answer on Stackoverflow
Solution 15 - AndroidPersiawareView Answer on Stackoverflow
Solution 16 - AndroidPelanesView Answer on Stackoverflow
Solution 17 - Androidtrond050666View Answer on Stackoverflow
Solution 18 - AndroidQian ZhangView Answer on Stackoverflow
Solution 19 - AndroidVeeKooView Answer on Stackoverflow
Solution 20 - AndroidThePartyTurtleView Answer on Stackoverflow
Solution 21 - AndroidMahboob Ahmed ToorView Answer on Stackoverflow
Solution 22 - AndroidPradeep SheoranView Answer on Stackoverflow
Solution 23 - Androiduser8363370View Answer on Stackoverflow
Solution 24 - AndroidSarojini2064130View Answer on Stackoverflow
Solution 25 - AndroidShriram NavaratnalingamView Answer on Stackoverflow
Solution 26 - AndroidParesh MangukiyaView Answer on Stackoverflow