Android - Unable to merge dex with Google Data Library

523 Views Asked by At

I have this android project that worked well. My idea was that I was going to use an already-existing Google Sheets as my database for the app. I searched and searched until I found an example. So I added

  1. gdata-client-1.0.jar
  2. gdata-client-meta-1.0.jar
  3. gdata-core-1.0.jar
  4. gdata-spreadsheet-3.0.jar
  5. gdata-spreadsheet-meta-3.0.jar
  6. guava-11.0.2.jar

Then the error came up:

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

java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

So I tried using

multiDexEnabled true

which worked well (successful gradle) until I ran the project.

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

java.io.IOException: Can't write [E:\AndroidStudioProjects\Corporate Outcome Request System (CORS)\app\build\intermediates\multi-dex\debug\componentClasses.jar] (Can't read [C:\Users\hp.gradle\caches\modules-2\files-2.1\com.google.guava\guava-jdk5\17.0\463f8378feba44df7ba7cd9272d01837dad62b36\guava-jdk5-17.0.jar(;;;;;;**.class)] (Duplicate zip entry [guava-jdk5-17.0.jar:com/google/common/annotations/Beta.class]))

This is my build.gradle now:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.YearZero.CORS"
        minSdkVersion 15
        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'
        }
    }
    configurations.all {
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:26.1.0'
    compile 'com.google.android.gms:play-services-auth:11.8.0'
    compile 'pub.devrel:easypermissions:0.3.0'
    compile 'com.android.support:multidex:1.0.2'
    compile('com.google.api-client:google-api-client-android:1.23.0') {
        exclude group: 'org.apache.httpcomponents'
    }
    compile('com.google.apis:google-api-services-sheets:v4-rev496-1.23.0') {
        exclude group: 'org.apache.httpcomponents'
    }
    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'
}

There were also instances where I got transformClassesWithMultidexlistForDebug and transformClassesWithDexlistForDebug :P

2

There are 2 best solutions below

6
martinomburajr On

Here's what I have found regarding your type of error. I am giving examples similar to your case.

Solution

Try place the following exclude group: 'com.google.guava' in either the com.google.api-client:google-api-client-android:1.23.0 or com.google.apis:google-api-services-sheets:v4-rev496-1.23.0 and see what happens.

In some cases:

  1. There could be some conflict behind your dependencies and gradle may not know how to resolve them. For instance if your app's build.gradle contain different versions of the same dependency e.g one dependency uses play-services:11.6.0 and another play-services:11.6.1 gradle may not know how to merge them. Another example could be you have 'com.android.support:appcompat-v7:26.1.0 but your design library may be using an older version e.g 'com.android.support:design:25.0.0'
  2. In some cases if you are using an emulator/device that contains an older version of your app and you change your dependencies on your app's build.gradle that create the conflict mentioned in (step 1). The simple solution here is to uninstall on your device and reinstall
0
Abhinav Gupta On

Try this once :-

android {
      defaultConfig {
        multiDexEnabled true
        }
   }

may be it will help you..