Android Studio Multiple Dex Error

2.3k views Asked by At

I looked at the related topics but i couldn't find any solution. First time i run the project, the emulator opened, but then it started to give an error. How i can solve this problem? Regards...

My error:

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lorg/apache/http/entity/mime/FormBodyPart;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:502)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
at com.android.dx.command.dexer.Main.run(Main.java:277)
at com.android.dx.command.dexer.Main.main(Main.java:245)
at com.android.dx.command.Main.main(Main.java:106)
Error:Execution failed for task ':userAndroid:dexDebug'.
com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2
Information:BUILD FAILED
Information:Total time: 19.946 secs
Information:1 error
Information:0 warnings
Information:See complete output in console

My build.gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 19
buildToolsVersion '23.0.0 rc2'

defaultConfig {
    applicationId "com.user.android"
    minSdkVersion 14
    targetSdkVersion 19
 //   multiDexEnabled = true

}


/*dexOptions {
    preDexLibraries = false
    incremental true
    javaMaxHeapSize "4g"
}*/

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

  }

dependencies {
compile project(':slidingMenuLibrary')
compile project(':pullToRefreshLibrary')
compile project(':placeActivity')
compile project(':jMC')
compile project(':salesforceSDK')
compile project(':library')
compile files('libs/android-integration-2.3-SNAPSHOT.jar')
compile files('libs/aws-android-sdk-1.4.4-core.jar')
compile files('libs/aws-android-sdk-1.4.4-s3.jar')
compile files('libs/com.radaee.pdfex_view.jar')
compile files('libs/commons-lang3-3.1.jar')
compile files('libs/core-2.3-SNAPSHOT.jar')
compile files('libs/ksoap2-android-assembly-3.2.0-jar-with-dependencies.jar')
compile files('libs/libGoogleAnalyticsServices-v3.01.jar')
compile files('libs/universal-image-loader-1.8.5-with-sources.jar')
compile files('libs/urbanairship-lib-3.0.0.jar')
compile 'com.google.api.client:google-api-client-repackaged-com-google-common-base:1.2.3-alpha'
compile 'com.google.code.gson:gson:2.3.1'
compile 'com.android.support:support-v4:22.2.0'
}

Edit:I have compile 'com.android.support:support-v4:22.2.0' in different files.

build.gradle for my JMC library:

apply plugin: 'com.android.library'
android {
compileSdkVersion 17
buildToolsVersion '23.0.0 rc2'

defaultConfig {
    minSdkVersion 8
    targetSdkVersion 16
}

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

dependencies {
compile files('../libMaster/acra-4.2.3.jar')
compile files('../libMaster/apache-mime4j-0.5.jar')
compile files('../libMaster/commons-io-2.4.jar')
compile files('../libMaster/httpclient-4.0.jar')
compile files('../libMaster/httpcore-4.0-beta3.jar')
compile 'com.google.guava:guava:18.0'
compile files('../libMaster/httpmime-4.0-beta2.jar')
compile 'com.android.support:support-v4:22.2.0'

}

2

There are 2 answers

1
once2go On

Discover this:https://github.com/mihaip/dex-method-counts Tools show you libs that have many methods. Your dependency in graddle so huge, are you really using all of it? If no try to delete unused

1
Adi Tiwari On

Android newer build tools have support for mutidex. you can enable multidex support by editing your build.gradle file like below

  defaultConfig {
        ...
        minSdkVersion 14
        targetSdkVersion 21
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }

Also you have @override this method in your Application class

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