Parad0X
Parad0X

Reputation: 3664

Unable to merge dex

I have Android Studio Beta. I created a new project with compile my old modules but when I tried launching the app it did not launch with the message:

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

com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

But I don't know how to solve this error. I googled this for hours but with no success.

My project gradle:

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

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-beta6'
        classpath "io.realm:realm-gradle-plugin:3.7.1"
        classpath 'com.google.gms:google-services:3.1.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
}

My app gradle:

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "parad0x.sk.onlyforyou"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
        }
    }
    compileOptions {
        targetCompatibility 1.7
        sourceCompatibility 1.7
    }
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }
    lintOptions {
        checkReleaseBuilds false
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    //noinspection GradleCompatible
    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
    compile project(path: ':loginregisterview')


}

And my module gradle:

    apply plugin: 'com.android.library'
apply plugin: 'realm-android'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 26
        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:appcompat-v7:26.0.2'
    compile 'com.android.support:support-v4:26.1.0'
    compile 'com.github.bumptech.glide:glide:4.0.0'
    testCompile 'junit:junit:4.12'
    compile project(path: ':parser')

}

My second module:

     apply plugin: 'com.android.library'
apply plugin: 'realm-android'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    realm {
        syncEnabled = true
    }
    useLibrary 'org.apache.http.legacy'

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

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile 'junit:junit:4.12'
    //  compile 'com.android.support:appcompat-v7:23.1.0'

    //   compile 'com.fasterxml.jackson.core:jackson-core:2.9.0'
 //   compile 'com.fasterxml.jackson.core:jackson-annotations:2.9.0'
 //   compile 'com.fasterxml.jackson.core:jackson-databind:2.9.0'
    compile 'com.google.code.gson:gson:2.6.2'
}

____________finding_________

When I did not import the second module (parser) the app did not crash on dex but when the module was not imported app did not work. :D :D

Upvotes: 326

Views: 413292

Answers (30)

Zohidjon Akbarov
Zohidjon Akbarov

Reputation: 3223

I had the same problem when I updated from com.google.android.gms:play-services:11.2.2 to com.google.android.gms:play-services:11.4.0. This solved it for me:

  1. clean
  2. rebuild

And also pay attention if you use more libs add multiDexEnabled true

Upvotes: 301

Sergio
Sergio

Reputation: 30595

Along with Unable to merge dex I had an error java.lang.OutOfMemoryError: Java heap space when building a project.

In the gradle.properties file I had this commented line:

# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

Uncommenting this line and increasing the heap memory to 3g solved the problem:

org.gradle.jvmargs=-Xmx3g -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

Upvotes: 0

DINA TAKLIT
DINA TAKLIT

Reputation: 8388

if(1. Try to clean and rebuild work ) then good

else if (2. Try to remove gradle work ) then good

else-> 3. Try to add in grade.properties

android.enableD8 = false

Edit 2021: This 3rd option is deprecated now, use the other options

else-> 4. Add multiDexEnabled true to your build.gradle

android {
    compileSdkVersion 26
    defaultConfig {
      ...
        minSdkVersion 15
        targetSdkVersion 26
        multiDexEnabled true
     ...
    }
}

and add the dependency

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

It may the first one works for u and so on but it really depends on the nature of your problem for me for example

I got the error once I have added this library

implementation 'com.jjoe64:graphview:4.2.2'

and later I discovered that I have to check that and I have to add the same version of the support libraries. So I have to try another version

compile 'com.jjoe64:graphview:4.2.1'

and it fixes the problem. So pay attention for that.

Upvotes: 17

raVen
raVen

Reputation: 73

Go Unity >> Edit Project settings>>player, look for Minify section and then choose the Proguard option

Upvotes: 0

Sanjay Mali
Sanjay Mali

Reputation: 546

Just add this line in app/build.gradle

defaultConfig {
       multiDexEnabled true 
    }

Upvotes: 0

itzhar
itzhar

Reputation: 13031

I tried all the above and none of them helps. finally, I find this work for me:

app/build.gradle:

android {
    defaultConfig {
       multiDexEnabled true
    }
}

Upvotes: 359

Mushfiqur Rahman
Mushfiqur Rahman

Reputation: 306

In my case the following works

implementation 'com.google.android.gms:play-services-ads:17.2.0'
implementation 'com.google.firebase:firebase-core:16.0.9'
implementation 'com.google.firebase:firebase-analytics:16.5.0'
implementation 'com.google.firebase:firebase-messaging:18.0.0'

In my app module. The problem was they firebase-analytics latest library, it is not working properly with the other latest libraries.

Upvotes: 0

Rasel
Rasel

Reputation: 5734

I had same problem. I just enabled Instant Run(It was disabled) then my project worked. You can find it in-

File->Settings-> Build,Execution,Deployment->Instant Run

In Android studio 3.5 Instant Run have been removed. Please see here for reference

Upvotes: 1

Casey Schneider
Casey Schneider

Reputation: 1002

This hasn't been shared here yet so hopefully it can help someone.

Following this link solved the same issue for me.

To begin, I'll note that I did not have to set multiDexEnabled to true.

First I set dependencies in my pubspec.yaml to

dependencies:
  flutter:
    sdk: flutter
  cloud_firestore: ^0.8.2 

and ran flutter packages get in my IDE's terminal.

Also I had to change the minimum target SDK version:

  1. Open android/app/build.gradle, then find the line that says minSdkVersion 16.
  2. Change that line to minSdkVersion 21.
  3. Save the file.

This alone may fix your problem; however I had to also do the following because some of my dependency versions were mismatched.

I had to open android/app/build.gradle, then add the following line as the last line in the file: apply plugin: 'com.google.gms.google-services'

Next, I had to open android/build.gradle, then inside the buildscript tag, add a new dependency:

buildscript {
   repositories {
       // ...
   }

   dependencies {
       // ...
       classpath 'com.google.gms:google-services:3.2.1'   // new
   }
}

After this my app finally ran on the android emulator.

The link has a more complete walkthrough if you get stuck.

Also, to note, I did not have to set multiDexEnabled to true.

Upvotes: 0

abhi.nalavade
abhi.nalavade

Reputation: 196

Just add below in build.gradle,

defaultConfig { multiDexEnabled true }

Upvotes: 0

kaushal
kaushal

Reputation: 911

add this in your app/build.gradle. It will work

implementation 'com.android.support:design:27.1.0'

Upvotes: 0

Mir-Ismaili
Mir-Ismaili

Reputation: 16928

Pay attention to Warnings!

Sometimes you only need to eliminate warnings and the error will be disappeared automatically. See below special case:


I had these two dependencies in my module-level build.gradle file:

implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'

and Studio had warned (in addition to dex merging problem):

All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes). Found versions 27.0.2, 21.0.3. Examples include com.android.support:animated-vector-drawable:27.0.2 and com.android.support:support-v4:21.0.3

So I explicitly determined the version of com.android.support:support-v4 (see here for details) and both problems (the warning and the one related to dex merging) solved:

implementation 'com.android.support:support-v4:27.0.2'  // Added this line (according to above warning message)
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'

See below comments for other similar situations.

Upvotes: 61

Aditya Kumar
Aditya Kumar

Reputation: 33

This works for me-

Add code at last in platforms\android\build.gradle

configurations.all { 
 resolutionStrategy{
    force 'com.android.support:support-v4:26.0.0'
    }

    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
    def requested = details.requested
    if (requested.group == 'com.android.support') {
       if (!requested.name.startsWith("multidex")) {
      details.useVersion '26.0.0'
     }
   }
  }
}

Upvotes: 0

Suraj Vaishnav
Suraj Vaishnav

Reputation: 8305

In my case a library makes this problem, library was successfully added to project but when i run my app it shows me this error. So if this happens to you too, you can go to github and check issues or raise new issue. If you do not find any solution regarding the library i suggest you to replace it.

Upvotes: 1

kokoko
kokoko

Reputation: 3964

I tried every other solution, but no one worked for me. At the end, i solved it by using same dependency version by editing build.gradle. I think this problem occurres when adding a library into gradle which uses different dependency version of support or google libraries.

Add following code to your build gradle file. Then clean and rebuild project.

ps: that was old solution for me so you should use updated version of following libraries.

configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
    def requested = details.requested
    if (requested.group == 'com.android.support') {
        if (!requested.name.startsWith("multidex")) {
            details.useVersion '26.1.0'
        }
    } else if (requested.group == "com.google.android.gms") {
        details.useVersion '11.8.0'
        } else if (requested.group == "com.google.firebase") {
            details.useVersion '11.8.0'
          }
      }
}

Upvotes: 23

Ramesh Krishnan
Ramesh Krishnan

Reputation: 81

If you are using Firebsae in your plugin, this issue will come due to play services version issue, try this `cordov

cordova.system.library.4=com.google.gms:google-services:3.3.0
    cordova.system.library.5=com.google.android.gms:play-services-tagmanager:+
    cordova.system.library.6=com.google.firebase:firebase-core:16.0.1
    cordova.system.library.7=com.google.firebase:firebase-messaging:17.1.0
    cordova.system.library.8=com.google.firebase:firebase-crash:16.0.1
    cordova.system.library.9=com.google.firebase:firebase-config:16.0.0
    cordova.system.library.10=com.google.firebase:firebase-perf:16.0.0 

Change your project.properties like this, then this issue will be resolved Reference

Upvotes: 0

DontPanic
DontPanic

Reputation: 2406

This may not be your problem, but I got this error when I accidentally included two identical (but differently named) libraries in the dependencies{} section of the project.

Upvotes: 2

Jon
Jon

Reputation: 840

I agree with Chris-Jr. If you are using Firebase to embed your AdMob ads (or even if you are not) the play-services-analytics includes the play-services-ads even though you don't add that as a dependency. Google have obviously made a mistake in their 11.4.0 roll-out as the analytics is including version 10.0.1 of ads, not 11.4.0 (the mouse over hint in the gradle shows this).

I manually added compile 'com.google.android.gms:play-services-ads:11.4.0' at the top which worked, but only after I disabled Instant Run: http://stackoverflow.com/a/35169716/530047

So its either regress to 10.0.1 or add the ads and disable Instant Run. That's what I found if it helps any.

Upvotes: 3

android developer
android developer

Reputation: 116322

Just to add to the above solutions:

Make sure that you don't have duplicate dependencies pointing to different versions of them, in multiple places (or even in the same file).

Upvotes: 12

Philippe Grain
Philippe Grain

Reputation: 61

One of the possibilities is: the presence of the same library but with different versions in the dependencies.

I had this problem with the following lines in gradle file:

  • compile fileTree(include: ['*.jar'], dir: 'libs')
  • compile 'com.google.code.gson:gson:2.8.2'

The gson library was in my libs directory but with a previous version. I deleted the gson-2.3.1.jar from the libs directory and everything is back to normal.

Upvotes: 6

Rodrigo Salomao
Rodrigo Salomao

Reputation: 153

Change you buildToolsVersion to some other version and sync, change it back and sync again. It will work again.

Upvotes: 0

asish
asish

Reputation: 4807

Make sure all same source library have same version number. In my case I was using different google api versions. I was using 11.6.0 for all google libraries but 10.4.0 for google place api. After updating google place api to 11.6.0, problem fixed.

Please note that my multidex is also enabled for the project.

Upvotes: 0

Oleg Korban
Oleg Korban

Reputation: 269

Check if you have debugCompile and releaseCompile in build.gradle. Change it to debugImplementation and releaseImplementation. This was how I have solved it, gradle should pass through this successfully marked instructions as deprecated, but suddenly it stucked exactly on this two.

Upvotes: 0

Ifta
Ifta

Reputation: 1595

For our project, we accidentally added same jar two times with different name. Removing one of them solved the issue.

Upvotes: 2

Eben Watts
Eben Watts

Reputation: 141

android {
    defaultConfig {
        ...
        minSdkVersion 15 
        targetSdkVersion 26
        multiDexEnabled true
    }
    ...
}

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

Upvotes: 1

Taha Ali
Taha Ali

Reputation: 487

[ UNABLE TO MERGE DEX SOLVED ] After hours of stack overflowing I resolved the " UNABLE TO MERGE DEX ERROR "

  1. Update all the com.android.support lines in your gradle to v27.1.0

Cause - Android has updated it support libraries to v27.1.0, so you have to change all the android support lines in your gradle file to 27.1.0 from 26.1.0

  1. Make sure that the repositories section includes a maven section with the "https://maven.google.com" endpoint. For example:

    allprojects { repositories { jcenter() maven { url "https://maven.google.com" } } }

Cause :- Android cannot update the support libraries in the SDK manager and now it uses maven.google.com to update , so you have to include this to use 27.1.0 support libraries

After Change Version: 1. Clean Project 2. Rebuild Project

Upvotes: 5

Nikul Vadher
Nikul Vadher

Reputation: 29

check dependencies version if they are not same then make it same it will solve your problem

compile 'com.android.support:appcompat-v7:27.0.2'
compile 'com.android.support:recyclerview-v7:27.0.2'
compile 'com.android.support:cardview-v7:27.0.2'

Upvotes: 0

akhil as
akhil as

Reputation: 21

I also had the problem.

I was able to solve by changing compileSdkVersion and targetSdkVersion to latest version.

Upvotes: 2

Suragch
Suragch

Reputation: 511558

  1. Delete the .gradle directory.

  2. Run your app again.

Notes

  • The .gradle directory is in your project's root folder. (You may have to show hidden files first.)
  • I have to do this every time I update a dependency module using Android 3.0. (More recent releases of Android Studio 3 seem to have resolved the problem.)

Upvotes: 32

Ashwin H
Ashwin H

Reputation: 723

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.xyz.name"
        minSdkVersion 14
        targetSdkVersion 27
        versionCode 7
        versionName "1.6"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'com.android.volley:volley:1.0.0'
    implementation 'com.wang.avi:library:2.1.3'
    implementation 'com.android.support:design:27.1.0'
    implementation 'com.android.support:support-v4:27.1.0'
    implementation 'de.hdodenhof:circleimageview:2.1.0'
    implementation 'com.github.bumptech.glide:glide:3.7.0'
    implementation 'com.theartofdev.edmodo:android-image-cropper:2.6.0'
    implementation 'com.loopj.android:android-async-http:1.4.9'
    implementation 'com.google.firebase:firebase-messaging:11.8.0'
    implementation 'com.felipecsl.asymmetricgridview:library:2.0.1'
    implementation 'com.android.support:recyclerview-v7:27.1.0'
    implementation 'com.github.darsh2:MultipleImageSelect:3474549'
    implementation 'it.sephiroth.android.library.horizontallistview:hlistview:1.2.2'
    implementation 'com.android.support:multidex:1.0.1'
}

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

Note: update your all support library to 27.1.0 like above and remove duplicates

Upvotes: 0

Related Questions