Home » Android » android – Duplicate Entry Google Gson

android – Duplicate Entry Google Gson

Posted by: admin June 15, 2020 Leave a comment

Questions:

My error:

 Error:Execution failed for task ':app:packageAllDebugClassesForMultiDex'.
 > java.util.zip.ZipException: duplicate entry: com/google/gson/annotations/Expose.class

I’m trying to use Stripe and integrate it with retrofit. I have the Stripe lib build.gradle file and the app build.gradle file.

I dont see whats causing this error and I need the dependency in both build.gradle files because both Stripe and Retrofit use it.

app build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.weaverprojects.stripe2"
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':stripe')
    //compile 'com.android.support:support-v4:18.0.+'

    compile 'com.google.code.gson:gson:2.3'
    compile 'org.parceler:parceler:0.2.13'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.squareup:otto:1.3.6'
    compile 'com.squareup.okhttp:okhttp:2.3.0'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.3.0'
}

Stripe build.gradle:

apply plugin: 'com.android.library'

dependencies {
//    compile 'com.stripe:stripe-java:1.15.1'
//    compile 'com.google.code.gson:gson:2.2.4'
    compile files('libs/gson-2.2.4.jar')
    compile files('libs/stripe-java-1.15.1.jar')
}

android {
    compileSdkVersion 21
    buildToolsVersion '23.0.1'

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 21
        multiDexEnabled = true
    }
}

I do have the Stripe and GSON jar in the libs folder so I tried changing:

compile 'com.google.code.gson:gson:2.3'

to

compile files('../stripe/libs/gson-2.2.4.jar')

in the app’s build.gradle.

What am I doing wrong?

Thanks in advance.

How to&Answers:

The root of the issue is that you’re mixing a dependency on a jar via compile files('libs/gson-2.2.4.jar') and a maven artifact via compile 'com.google.code.gson:gson:2.3'.

When you reference the same maven artifact in separate parts of your project, Gradle is able to intelligently figure out that it shouldn’t include both. But, Gradle is unable to figure out that the jar you’re referencing is the same as the maven artifact you’re referencing.

Solution

In Stripes build.gradle, change the lib reference to compile 'com.google.code.gson:gson:2.3', and delete gson-2.2.4.jar from your project entirely.

Answer:

Either remove the line compile 'com.google.code.gson:gson:2.3'

or

Delete the gson jar from your lib folder. because you have included the library twice once in build file and in libs folder.