81

This is my app level gradle file:

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion '27.0.0'
    useLibrary 'org.apache.http.legacy'
    defaultConfig {
        multiDexEnabled true
        applicationId 'com.example.test'
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    productFlavors {
    }
    dexOptions {
        incremental true
    }
}

dependencies {
    compile 'com.google.android.gms:play-services:10.0.1'
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
    compile 'commons-io:commons-io:1.3.2'
    compile 'com.mcxiaoke.volley:library:1.0.19'
    compile 'com.facebook.stetho:stetho:1.5.0'
    compile 'com.github.PhilJay:MPAndroidChart:v3.0.2'
}

The complete error I get when compiling the project is:

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

java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

The complete error is as follows:

    Executing tasks: [:app:assembleDebug]

AGPBI: {"kind":"warning","text":"The `android.dexOptions.incremental` property is deprecated and it has no effect on the build process.","sources":[{},{}]}
Configuration 'compile' in project ':app' is deprecated. Use 'implementation' instead.
:app:buildInfoDebugLoader
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:checkDebugManifest UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:prepareLintJar UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:createDebugCompatibleScreenManifests UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:splitsDiscoveryTaskDebug UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources
:app:javaPreCompileDebug UP-TO-DATE
:app:compileDebugJavaWithJavac UP-TO-DATE
:app:mergeDebugShaders UP-TO-DATE
:app:compileDebugShaders UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:packageInstantRunResourcesDebug UP-TO-DATE
:app:checkManifestChangesDebug
:app:extractTryWithResourcesSupportJarDebug UP-TO-DATE
:app:transformClassesWithStackFramesFixerForDebug UP-TO-DATE
:app:transformClassesWithDesugarForDebug UP-TO-DATE
:app:transformClassesWithExtractJarsForDebug UP-TO-DATE
:app:transformClassesWithInstantRunVerifierForDebug UP-TO-DATE
:app:transformClassesWithDependencyCheckerForDebug UP-TO-DATE
:app:compileDebugNdk NO-SOURCE
:app:mergeDebugJniLibFolders UP-TO-DATE
:app:transformNativeLibsWithMergeJniLibsForDebug UP-TO-DATE
:app:processDebugJavaRes NO-SOURCE
:app:transformResourcesWithMergeJavaResForDebug UP-TO-DATE
:app:transformNativeLibsAndResourcesWithJavaResourcesVerifierForDebug UP-TO-DATE
:app:transformClassesWithInstantRunForDebug UP-TO-DATE
:app:transformClassesEnhancedWithInstantReloadDexForDebug UP-TO-DATE
:app:incrementalDebugTasks UP-TO-DATE
:app:preColdswapDebug
:app:fastDeployDebugExtractor UP-TO-DATE
:app:generateDebugInstantRunAppInfo UP-TO-DATE
:app:transformClassesWithInstantRunSlicerForDebug UP-TO-DATE
:app:transformClassesWithDexBuilderForDebug UP-TO-DATE
:app:transformDexArchiveWithExternalLibsDexMergerForDebug FAILED
:app:buildInfoGeneratorDebug

FAILURE: Build failed with an exception.

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

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s

39 actionable tasks: 5 executed, 34 up-to-date
Karthikeyan
  • 196
  • 2
  • 10
kumail
  • 1,331
  • 4
  • 13
  • 19
  • Since Android API 21 (5.0), [multidex](https://developer.android.com/studio/build/multidex.html) support doesn't need the library installed in the projects using `minSdkVersion 21` and above. – xRed Nov 08 '17 at 19:14
  • I added it just to see if it made a difference – kumail Nov 08 '17 at 19:17
  • Oh nevermind, didn't noticed your project supports api 16 so you can't really remove it. – xRed Nov 08 '17 at 19:21
  • I suggest that you edit your question and provide the complete output of the Gradle console, not just that one message. The real source of your problem should lie elsewhere in the output. – CommonsWare Nov 08 '17 at 19:22
  • @CommonsWare thanks, I just updated it with the full output – kumail Nov 08 '17 at 19:27
  • 2
    OK, that's strange. I was expecting more details around the `:app:transformDexArchiveWithExternalLibsDexMergerForDebug FAILED`, complaining about duplicate classes or similar problems. I don't really know what to tell you with that output. I know you have cleaned the project several times, but I recommend that you clean it once more, try the build, and see if that build's Gradle Console output gives you more details of exactly what Gradle doesn't like. – CommonsWare Nov 08 '17 at 19:31
  • 2
    @CommonsWare, super weird, I did a clean and rebuild two times and it finally worked. Compiled successfully! – kumail Nov 08 '17 at 20:23
  • 5
    It is nonsense, "This question was caused by a problem that can no longer be reproduced or a simple typographical error" - 1st, the problem can still be reproduced, 2nd it is not a simple typo! – Frank R. Jan 03 '18 at 12:26
  • 2
    @FrankR. agreed. Voted to Reopen. I'm guessing it is because most answers are to clean and rebuild but that seems like a valid answer in this case. – codeMagic Jan 30 '18 at 01:49

2 Answers2

156

Enable Multidex through build.gradle of your app module

multiDexEnabled true

Same as below -

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.xx.xxx"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        multiDexEnabled true //Add this
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

Then follow below steps -

  1. From the Build menu -> press the Clean Project button.
  2. When task completed, press the Rebuild Project button from the Build menu.
  3. From menu File -> Invalidate cashes / Restart

compile is now deprecated so it's better to use implementation or api

Ashish
  • 6,791
  • 3
  • 26
  • 48
Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
  • 4
    I've tried this many times, still get the same error – kumail Nov 08 '17 at 19:18
  • Hopefully either you extending your Application by `MultiDexApplication` or You've entry for `MultiDexApplication` in `AndoridManifest` file. – Abhishek Aryan Nov 08 '17 at 19:22
  • This problem only occurs in Android Studio 3.0, the program compiles fine on older versions of studio – kumail Nov 08 '17 at 19:28
  • 1
    this will resolve the issue, it's coming because of conflict ```configurations.all { resolutionStrategy.dependencySubstitution { substitute module('org.apache.commons:commons-io:1.3.2') with module('commons-io:commons-io:1.3.2') } }``` – Abdul Rizwan Jan 12 '18 at 12:25
  • @AbdulRizwan I am getting this *Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'. > java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex* because of conflict like *com.android.dex.DexException: Multiple dex files define Lorg/apache/commons/io/output/DeferredFileOutputStream* how to solve this ? where to use *configurations.all* inside *build.gradle* ? can you please guide ? – Jay Rathod Jan 24 '18 at 10:37
  • 1
    Works for me, but what is multiDexEnabled? – Álvaro Agüero Jun 19 '19 at 19:30
  • 2
    @ÁlvaroAgüero, A single `.dex` file can have 65,536 methods(references) so if the number of references exceeds 65,536, you've to go with multidex. For more details check on [official](https://developer.android.com/studio/build/multidex) site. – Abhishek Aryan Jun 19 '19 at 19:44
  • 1
    Clean Project and Rebuild resolved this error for me. – Tarun Nov 13 '19 at 11:50
67

I am using Android Studio 3.0 and was facing the same problem. I add this to my gradle:

multiDexEnabled true

And it worked!

Example

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.1'
    defaultConfig {
        applicationId "com.xx.xxx"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        multiDexEnabled true //Add this
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

And clean the project.

Rakesh Yadav
  • 1,966
  • 2
  • 21
  • 35
  • 4
    Not working for me, I think there is duplicate includes internally, but I dont find a way out which tells whats the duplicate. – Artist404 Mar 12 '18 at 05:35
  • @Artist404 May be you have included multidex library as well in your project or submodule. Remove multidex library dependency. – Rakesh Yadav Mar 12 '18 at 07:27
  • 1
    Still problem persists. i have done the same. but not resolved....>java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives – Gyan Swaroop Awasthi Jul 25 '18 at 14:02
  • 1
    after adding this, I am getting "Multiple dex files define Landroid/support/v4/os/ResultReceiver" error. can you please help me – rahul.sapkal23 Jun 20 '19 at 06:54
  • @rahul.sapkal23 please check your support libraries and its dependencies. – Rakesh Yadav Jun 21 '19 at 23:59
  • 1
    If you have added the ```multiDexEnabled true```, but not resolved, just try ```Clean Project``` – Cobain Oct 21 '19 at 14:22
  • Could not find method multiDexEnabled() for arguments [true] on object of type org.gradle.api.internal.plugins.DefaultExtraPropertiesExtension. – Luca C. Jun 20 '22 at 15:05