53
Error:java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
Error:java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
Error:com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
Error:com.android.dex.DexException: Multiple dex files define Lcom/google/zxing/integration/android/IntentResult;
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
Ankita-AR
  • 780
  • 1
  • 6
  • 11

15 Answers15

56

I have same problem with Android Studio 3.0 beta 4. I found a solution.

1. From the Build menu, press the Clean Project button.

Clean Project

2. After task completed, press the Rebuild Project button from the Build menu.

enter image description here

Y.E.S.
  • 764
  • 5
  • 16
28

For Android Studio 3.0 what I did was to add this to my gradle:

multiDexEnabled true

And it worked!

Example

android {
    compileSdkVersion 25
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.xx.xxx"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 9
        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'
        }
    }
}
Prodigy
  • 2,094
  • 24
  • 30
  • 7
    for me, this does not resolve the problem... I still continue working on resolve it.. – Hpsaturn Nov 01 '17 at 21:41
  • Try these steps: Delete your .gradle, clean and rebuild. Also don't forget add multiDexEnabled. Hope it helps – Prodigy Nov 02 '17 at 00:04
  • If you enable multidex and your minSDK is less than 5.0, then follow the instructions here: https://developer.android.com/studio/build/multidex – Johnny Aug 07 '18 at 22:23
19

So I solved this issue by doing the following:

  • Delete the ./gradle folder inside your project
  • Delete all the build folders and the gradle cache. I ran the following command:

How ?

 cd ~/[your project root folder] && find . -name build -exec rm -rf {} \; && rm -rf $HOME/.gradle/caches/

Assuming your gradle config files are in the $HOME/.gradle folder.

  • Inside Android Studio, went to File > Invalidate caches / Restart... and invalidated the caches and restarted it.
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
ADev
  • 5,259
  • 2
  • 16
  • 29
9

You should be able to get to the cause of this error by inspecting your dependencies with gradle and looking for duplicates like this: ./gradlew -q app:dependencies

In my case, the following error was happening at build time:

Duplicate zip entry [httpcore-4.4.1.jar

and it was resolved by doing this in my build.gradle:

implementation ('me.dlkanth:stetho-volley:1.0') {
    exclude group: 'org.apache.httpcomponents'
}
Sneh Pandya
  • 8,197
  • 7
  • 35
  • 50
IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
  • 1
    In my case 'com.android.volley' was being added twice somehow, I also added the exclude statement, now its working fine. – Harish Rana Oct 27 '17 at 10:28
  • running this script gave me `could not determine java version from 9.0.1`. I updated to Gradle from 4.1 to 4.2.1 using Android Studio 3.0 – androidtitan Feb 17 '18 at 23:54
7

If your minSdkVersion is 21 or higher

   android {
        defaultConfig {
           multiDexEnabled true
        }
    }

if your minSdkVersion is 20 or lower

1) you must add the following library in dependencies

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

2) Create a java class then extend it from Application and override attachBaseContext method.

 public class MyApplication extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

3) Mention the created class inside manifest in application tag.

    <application
        android:name=".MyApplication"
.
.
.
    </application>
Shahzad Afridi
  • 2,058
  • 26
  • 24
  • If above code does not work then check your every library in the gradle by holding cursor on it, if it has available new version then Update it, sync, clean project, rebuild project and run. hopefully, it will work for you. – Shahzad Afridi Nov 13 '17 at 12:16
  • This code works for Android 5.1 and above... for below android version.. there is another way to add multidex support. https://developer.android.com/studio/build/multidex.html – Relsell Dec 02 '17 at 10:57
6

Check your dependencies for latest version usually it is inconsistency with the version of any of your dependencies.

1.Build > Clean Project 2. Rebuild your project

Check the verbose log for the dependency causing the merge issue and update the same. Repeat the same for each dependencies.

Wills
  • 491
  • 8
  • 20
4

I faced same issue in Android Studio 3.0.1, I tried all possible cases nothing worked for me as mentioned above, finally I solved it by

Solution1:

  • Close Android Studio
  • Delete .gradle folder located at C:\Users\YourComputerName\.gradle not from app's .gradle
  • Restart android studio

It will download all the necessary jars and add to your build path

Solution2:

  • Delete duplicate jar from libs folder in app/libs
  • Rerun the app again

Because if there is duplicate jar file which already defined in build.gradle file it causes issue.

This solved the issue for me. It may help others..

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
1

This error can have multiple reasons. No clean and rebuild or anythging like that did the job for me.

For me the problem was the dependency:

compile 'org.jetbrains:annotations-java5:15.0'

I removed it and everything worked fine.

Palm
  • 699
  • 1
  • 6
  • 17
1

Change "compile" to "compileOnly" this is what worked for me.

0

In my case the culprit was SendGrid lib, added this and it got fixed:

compile 'com.github.danysantiago:sendgrid-android:1',{
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
Zohar
  • 1,820
  • 1
  • 18
  • 16
0

I am using Studio 3.0.0 release. In my case there was the only solution: I have removed old JARs from the "libs" folder in my project: it was "ksoap2" package with dependencies. Packages obtained with gradle usually do not conflict with each other, at least if you are using the most popular ones. But an old JAR in libs folder may crush your build if it includes all own dependencies.

Almaz
  • 920
  • 11
  • 13
0

I have tried other comments from removing gradle and bla bla bla, but adding multiDexEnabled true solving my problem, I investigate that my apk has reached Over 64K Method.

enter image description here

Ref: https://developer.android.com/studio/build/multidex.html)

Community
  • 1
  • 1
0

I am using Android Studio 3.0.1 Build #AI-171.4443003, built on November 9, 2017

I delete jar file from libs folder. And that work fine for me

IceCreamVan
  • 192
  • 1
  • 10
0

I am using Android Studio 3.0.1 and was facing the same problem. If others answer doesn't works try this:

Tools -> Android -> Sync Project with Gradle Files

It worked for me Sync Project with Gradle Files

jgodinez
  • 11
  • 2
-1

Sometime it can happens due to same library (jar file) present two times or your project has reached 64k methods limit.

Solution: 1) Remove Databinding if your project use this 2) Remove same type library from lib folder or, delete .gradle file from c//user//your_pc//.gradle 3) apply this in your build.gradle

android {
    defaultConfig {
       multiDexEnabled true
    }
}
Shaon
  • 2,496
  • 26
  • 27