Android Gradle duplicate files copied exception

1.1k views Asked by At

I have seen many versions of this question where the solution is to simply exclude the META-INF file name, but those are already excluded and the duplicate file, in this case, is not even a file associated with META-INF. When I run ./gradlew --stacktrace it compiles fine, but if I run the application on a device, the app crashes with the duplicate file warning.

apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "24.0.3"
defaultConfig {
    applicationId "rocks.morrisontech.historicsf"
    minSdkVersion 19
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
packagingOptions {
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/license'
}

dependencies {

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:25.1.0'
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.socrata:soda-api-java:0.9.12'
compile 'com.google.code.findbugs:jsr305:2.0.1'
testCompile 'junit:junit:4.12'
// play-services imports
compile 'com.google.android.gms:play-services-maps:10.0.1'

}

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.

    com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK com/sun/jersey/impl/api.properties File1: /Users/Quinn/.gradle/caches/modules-2/files-2.1/com.sun.jersey/jersey-core/1.9.1/cf6c0f8b945081fca5f5eb7417d10d58cefd7035/jersey-core-1.9.1.jar File2: /Users/Quinn/.gradle/caches/modules-2/files-2.1/com.sun.jersey/jersey-bundle/1.9.1/67d37c4e80633a0196b733886441855201742a65/jersey-bundle-1.9.1.jar

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

I have noticed jersey is a dependency of com.socrata:soda-api-java:0.9.12, and when I delete that dependency I have no issues. I have also tried to use the soda-android-api, but that came with similar issues.

1

There are 1 answers

0
Vincent On

I just had the identical problem while trying to build a project using the soda-java library with Android Studio.

It looks like multiple copies of files are trying to be included into the APK. This is causing a problem and not allowing the APK to be built. We have to exclude these files in our app's build.gradle file under the android section as such.

packagingOptions {
    exclude 'META-INF/LICENSE'
    exclude 'com/sun/jersey/impl/api.properties'
    exclude 'com/sun/jersey/impl/spi.properties'
    exclude 'com/sun/jersey/impl/impl.properties'
    exclude 'META-INF/jersey-module-version'
}

Basically you will have to go through each one of these errors and add the below bolded file reported in the error.

com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK com/sun/jersey/impl/api.properties

Currently I am unsure if this is the proper long term solution. But it is letting me get up and running with writing my app. I will have to come up with a more long term and possibly appropriate solution down the road before release.