Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Unable to merge dex" when using room

I'm trying to add a "room" to my project.

When I try to build a project, I get an error:

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

What I have already done:

  1. Clean/Rebuild project
  2. I added "multiDexEnabled true" in defaultConfig{}. Then I get the error:

    Error:Execution failed for task ':app:transformClassesWithMultidexlistForDebug'. java.io.IOException: Can't write [C:\Users\user1\AndroidStudioProjects\git\mobile\app\build\intermediates\multi-dex\debug\componentClasses.jar] (Can't read [C:\Users\user1.gradle\caches\transforms-1\files-1.1\support-core-utils-26.1.0.aar\a6c34f6784b0b6bc5c2fc7a7815426da\jars\classes.jar(;;;;;;**.class)] (Duplicate zip entry [classes.jar:android/support/v4/content/PermissionChecker$PermissionResult.class]))

If I remove the "room" from my project, it is build without errors.

I'm using Android Studio 3, gradle build tools 3.0.0.

This is my build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'

buildscript {
   repositories {
       mavenCentral()
   }

   dependencies {
       classpath 'me.tatarka:gradle-retrolambda:3.2.5'
   }
}

repositories {
    mavenCentral()
}

android {
   compileSdkVersion 23
   buildToolsVersion '26.0.2'

   defaultConfig {
        applicationId "trsnet.gtp2.com"
        minSdkVersion 17
        targetSdkVersion 23
        multiDexEnabled true
   }

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

   compileOptions {
       sourceCompatibility JavaVersion.VERSION_1_8
       targetCompatibility JavaVersion.VERSION_1_8
   }

}

dependencies {

    compile files('libs/commons-codec-1.9.jar')
    compile files('libs/ksoap2-android-assembly-3.0.0-jar-with-
    dependencies.jar')
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:cardview-v7:23.2.1'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
    compile 'com.android.support.constraint:constraint-layout:+'
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'io.reactivex:rxjava:1.1.6'
    compile 'com.jakewharton.rxbinding:rxbinding:0.4.0'
    implementation 'android.arch.persistence.room:runtime:1.0.0'
    annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
}
like image 551
Krabs Avatar asked Nov 08 '17 17:11

Krabs


1 Answers

I was having this problem too and it took me quite a while to solve, but I finally got it. ROOM uses some of the support-v4 libraries so this is why you are getting the error that there are duplicate zip entries. In my situation, ROOM is using components from an earlier version than what I needed. So what worked for me (found here) is adding the following to the root level of the Gradle file:

configurations {
    all*.exclude group: 'com.android.support', module: 'support-v4'
}

What I found this does is it prevents libraries from including any support-v4 components, but then you have to manually include the necessary components for both ROOM and anything else you might need. If you need to find out exactly which libraries are duplicates then you can follow these instructions to look into each library and it's dependencies.

Slightly unrelated note: As of Gradle 3.0 using the compile configuration is deprecated and should be replace with implementation or api, a nice explanation can be found here.

like image 185
dDan Avatar answered Nov 16 '22 06:11

dDan