Duplicated files copied in APK when including both Joda Time and Common IO libraries in Android project
Asked Answered
G

1

11

I have an Android project (Gradle) where I need to include both Joda Time and Commons IO libraries. This is my Gradle file:

apply plugin: 'android-library'
apply plugin: 'android-test'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.10.+'
        classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.+'
    }
}

dependencies {
    repositories {
        mavenCentral()
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots/'
        }
    }

    androidTestCompile 'com.google.guava:guava:14.0.1',
            'com.squareup.dagger:dagger:1.1.0',
            'org.hamcrest:hamcrest-integration:1.1',
            'org.hamcrest:hamcrest-core:1.1',
            'org.hamcrest:hamcrest-library:1.1'

    androidTestCompile('junit:junit:4.11') {
        exclude module: 'hamcrest-core'
    }
    androidTestCompile 'org.mockito:mockito-all:1.9.5'

    androidTestCompile('org.robolectric:robolectric:2.3') {
        exclude module: 'classworlds'
        exclude module: 'maven-artifact'
        exclude module: 'maven-artifact-manager'
        exclude module: 'maven-error-diagnostics'
        exclude module: 'maven-model'
        exclude module: 'maven-plugin-registry'
        exclude module: 'maven-profile'
        exclude module: 'maven-project'
        exclude module: 'maven-settings'
        exclude module: 'nekohtml'
        exclude module: 'plexus-container-default'
        exclude module: 'plexus-interpolation'
        exclude module: 'plexus-utils'
        exclude module: 'wagon-file'
        exclude module: 'wagon-http-lightweight'
        exclude module: 'wagon-http-shared'
        exclude module: 'wagon-provider-api'
    }
    androidTestCompile 'com.squareup:fest-android:1.0.+'

    compile files('./libs/joda-time-2.4.jar')
    compile files('./libs/commons-io-2.1.jar')
    compile files('./libs/jackson-annotations-2.0.2.jar');
    compile files('./libs/jackson-core-2.0.2.jar');
    compile files('./libs/jackson-databind-2.0.2.jar');
    compile files('./libs/scribe-1.2.3.jar');
}

android {
    packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
    }
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        versionName "1.0"
        versionCode 1
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src/main/java']
            res.srcDirs = ['res']
        }

        androidTest {
            java.srcDirs = ['src/test/java']
        }
    }

    lintOptions {
        abortOnError false
    }
}

The problem is that I can't compile the app, the following error appears:

Error:Gradle: Execution failed for task ':App:packageDebug'.
> Duplicate files copied in APK META-INF/LICENSE.txt
    File 1: <Path>/unspecified/libs/commons-io-2.1.jar
    File 2: <Path>/unspecified/libs/commons-io-2.1.jar

As you can see, I've already put the exclude 'META-INF/LICENSE.txt' line in my build.gradle, but the problem persists. If I remove the commons-io library dependency, all compiles perfectly.

Thank you,

Gertudegerty answered 11/8, 2014 at 21:29 Comment(1)
Have you found a solution to this? I am experiencing this issue as well. EDIT Found it! See answerWoeful
W
15

You need to exclude LICENSE.txt and NOTICE.txt as such:

android {
    //code

    packagingOptions {
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE.txt'
    }
}

dependencies {
    compile 'joda-time:joda-time:2.3'
    compile 'commons-io:commons-io:2.4'
}

This worked for me with the same error. Notice the versions as well.

Woeful answered 15/8, 2014 at 19:45 Comment(1)
Although this works, I think the license require these files to be included into the final APK. is there a way to rename them?Fasciate

© 2022 - 2024 — McMap. All rights reserved.