0

I use Android Studio version 3.0.1. My project showed up the Unable to merge dex.

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

I had tried

method A:

1.clean project
2.rebuild project 

method B:

android {
    defaultConfig {
       multiDexEnabled true
    }
}

But still not working :(

Here are my gradle file setting can anyone help me to check the problem? thx!

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '26.0.2'
    useLibrary 'org.apache.http.legacy'

defaultConfig {
    multiDexEnabled true
    applicationId "edu.berkeley.calhacks.pictureperfect"
    minSdkVersion 21
    targetSdkVersion 16  
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

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


dependencies {

    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:25.1.0'
    compile 'com.google.android.gms:play-services:8.1.0'
    compile 'com.android.support:design:25.1.0'
    compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
    compile "org.apache.httpcomponents:httpmime:4.2.3"
    compile 'com.android.support:multidex:1.0.1'
}

2 Answers2

1

If you are using Google Service then your Project gradle should look like this-

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.google.gms:google-services:3.0.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
}

And for handling the Multidexing and using Google service in App level gradle,

apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion "24.0.2"
    defaultConfig {
        applicationId "com.example.hp_pc.cerv"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    dexOptions {
        preDexLibraries = false
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/DEPENDENCIES'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
//    compile files('libs/httpclient-4.2.3.jar')
//    compile files('libs/httpmime-4.3.jar')
//    compile files('libs/httpcore.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:23.4.0'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.mcxiaoke.volley:library:1.0.+'
    compile 'com.github.pinball83:masked-edittext:1.0.3'
   // compile 'com.google.android.gms:play-services-maps:9.8.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.wdullaer:materialdatetimepicker:2.5.0'
    compile 'com.google.android.gms:play-services:6.5.87'
    compile 'joda-time:joda-time:2.9.4'
    testCompile 'junit:junit:4.12'
}

apply plugin: 'com.google.gms.google-services'

Add these two lines in your AndroidManifest.xml file,

<application
    android:hardwareAccelerated="true"
    android:largeHeap="true"
Fenil Patel
  • 1,528
  • 11
  • 28
1

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

multiDexEnabled true

then add this in your application class:

 @Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

and then clean and synk project. but if you tried all and not working yet then change the targetSdkVersion=26 and compileSdkVersion also set 26. and try with this.

aj0822ArpitJoshi
  • 1,142
  • 1
  • 9
  • 25
  • Apart from adding that, your Application class will also need this `extends MultiDexApplication` – Shark Dec 25 '17 at 10:47
  • if you extend Application then i think you need to add this @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } or if you extend MultiDexApplication then you dont need. if i am wrong then please do correct, may be i can! – aj0822ArpitJoshi Dec 25 '17 at 10:50
  • That's right, `MultiDex.install()` isn't really necessary when extending MultiDexApplication. – Shark Dec 25 '17 at 10:52