Mayank Thirani
Mayank Thirani

Reputation: 21

java.util.zip.ZipException: duplicate entry: javax/inject/Inject.class

I have seen similar responses to the below question. Sorry but i could not fixed it out trying multiple things so I am asking it again. Whenever I am trying to build my app, the gradle build throws me the below error stating ...due to duplicate entry of javax/Inject class. This only occurred when I am trying to put compile 'org.glassfish.jersey.containers:jersey-container-jdk-http:2.9' in my build.gradle file; This package is necessary for javax.ws.rs.* services.

I tried running the gradlew clean command in my root directory, it says that build is successful but when I am building my application again it started throwing the error again. Can you please help on the below error:

*Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: javax/inject/Inject.class*

Below is my app gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.usfca.studentrecordsverifycation"
        minSdkVersion 17
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        // Enabling multidex support.
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
repositories {
    maven { url 'http://repo1.maven.org/maven2' }
    maven { url 'http://maven.restlet.com' }
    maven { url 'https://maven.java.net/content/repositories/releases/' }
    maven { url 'http://download.java.net/maven/2/' }
    maven { url 'http://download.java.net/maven/1' }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:design:23.3.0'
    compile 'com.android.support:multidex:1.0.1'
    compile 'log4j:log4j:1.2.17'
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.madgag:sc-light-jdk15on:1.47.0.3'
    compile 'com.cloudinary:cloudinary-http44:1.4.5'
    compile 'org.glassfish.jersey.containers:jersey-container-jdk-http:2.9'
    compile 'commons-lang:commons-lang:2.6'
}

Below is my Project gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Upvotes: 1

Views: 2552

Answers (2)

Felipe Lima
Felipe Lima

Reputation: 10750

This error means that the class javax.inject.Inject is being included twice in your apk when the dexer runs. That is, this exact same class is coming from 2 different dependencies. One of them is probably org.glassfish.jersey.containers:jersey-container-jdk-http:2.9 because it happens after you added it. If you CMD+O (jump to class) on Android Studio, you should be able to see the 2 versions of this class and see where they are coming from. Make sure you select the checkbox that allows it to show classes included from your dependencies. Once you open them, hover over the file tab and you'll see a tooltip with the artifact name. That is gonna be the dependency that's including that class. Hope it makes sense.

Upvotes: 0

ᴛʜᴇᴘᴀᴛᴇʟ
ᴛʜᴇᴘᴀᴛᴇʟ

Reputation: 4656

Ok so I created a sample project with all those dependencies and I got a different error.

So if you are still getting the ZipException error, you have some jars in your lib folder that we are not aware of?

Here's the error I got:

Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE

If you are getting a similar error, all you need to do is add the following to your build.gradle:

android {
    defaultConfig {
        ...
    }

    buildTypes {
        ....
    }

    packagingOptions {
        exclude 'META-INF/LICENSE'
    }
}

If this doesn't make any difference, do you have any jar libraries in your project? If so, we need to see the list.

EDIT:

I can't know for sure if it will work because I am not getting the same error. In your dependencies, change to this:

compile ('org.glassfish.jersey.containers:jersey-container-jdk-http:2.9') {
    exclude group: 'javax.inject', module: 'javax.inject'
}

Upvotes: 1

Related Questions