Android Studio 0.4 Duplicate files copied in APK META-INF/LICENSE.txt

Solution 1:

Putting the dependecies at the top and the packageOptions at the end worked for me.

apply plugin: 'android'. 

Here is my full build.gradle at the app folder.

dependencies {
    compile 'com.android.support:support-v4:+'
    compile files('libs/apache-mime4j-0.6.jar')
    compile files('libs/httpmime-4.0.jar')
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 10
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-    rules.txt'
    }


    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }
}

EDIT: Almost all OS licence include the obligation to "include a copy of the licence" into your project. So this means, that you have to include a copy of all OS licences you use into you projects. By "excluding" them in gradle, you violate the licences.

Excluding them from the project might not be the best option. Thank you R.S. for the info.

Solution 2:

Attention!! Possible OpenSource license violation.

With excluding license.txt files as proposed above you may violate some opensource licenses as it is a common point in opensource licences to agree to add it to your source. Better check your opensource licences.

Update: Until there is a better solution, use

packagingOptions {
   pickFirst  'META-INF/license.txt'
}

like this you at least fulfill a part of the license obligation

Solution 3:

just add

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

in build.gradle

Solution 4:

You can fix it by adding the following code to project/app/build.gradle:

android {
    // Fixed build error : Duplicate files copied in APK META-INF/xxx
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE.txt'
    }
}

Solution 5:

I was facing the same problem as per new version of gradle, Below build.gradle text format work for me :

There are two jackson jars in my libs folder.

android {
         compileSdkVersion 21
         buildToolsVersion "21.1.2"

         defaultConfig {
            applicationId "com.omtlab.myapplication"
            minSdkVersion 14
            targetSdkVersion 21
            versionCode 1
            versionName "1.0"
         }
         buildTypes {
             release {
                 minifyEnabled false
                 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
         }
         packagingOptions {
            exclude 'libs/jackson-core-asl-1.9.13.jar'
            exclude 'libs/jackson-mapper-asl-1.9.13.jar'
            exclude 'META-INF/ASL2.0'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/NOTICE'
         }
}

dependencies {
    //compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile files('libs/jackson-core-asl-1.9.13.jar')
    compile files('libs/jackson-mapper-asl-1.9.13.jar')
}