117

After I have updated my Studio from 0.3.7 to 0.4.0, I can't compile my project. I found a solution on stackoverflow: Duplicate files copied (Android Studio 0.4.0)

I updated my project to gradle 0.7.+, but I don't know where I must put the next strings:

android {

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

My logcat: log

Execution failed for task ':Prog:packageDebug'.
> Duplicate files copied in APK META-INF/LICENSE.txt
    File 1: /home/scijoker/AndroidStudioProjects/ProgProject/Prog/libs/httpclient-4.1.1.jar
    File 2: /home/scijoker/AndroidStudioProjects/ProgProject/Prog/libs/httpclient-4.1.1.jar

P.S. Develop in ubuntu 13.04

Community
  • 1
  • 1
a.black13
  • 2,157
  • 4
  • 19
  • 20

13 Answers13

157

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.

Sergio
  • 844
  • 2
  • 9
  • 26
biniam
  • 8,099
  • 9
  • 49
  • 58
  • This one seems to work for me as well. What is all these exclude directives, by the way? Why do we need this? – Andree Dec 06 '14 at 06:56
  • 2
    @Andree The reason we exclude the files is because we do not want the .txt (and other) files to be included in the .apk that will be generated by the build. – biniam Dec 08 '14 at 06:26
  • 4
    exclude is not recognized outside of android{} :/ – An-droid Feb 24 '15 at 10:47
  • For me it is worked with adding exclude 'META-INF/LICENSE' and exclude 'META-INF/NOTICE'. – Dhiraj Powar Jul 01 '15 at 06:23
  • packagingOptions must be within android –  Aug 08 '15 at 13:07
  • 4
    Exclude should be inside android { }, then only it works. – Pavan Pyati Aug 14 '15 at 08:03
  • 2
    ! This Solution violates all Open source licences ! As you are obliged to add the original licence text of each library to your package – R.A Nov 13 '15 at 13:05
  • @R.S I don't really understand which open source licence you are mentioning. Please give me detail so I can modify it. – biniam Nov 18 '15 at 10:26
  • 2
    Hi. Any. 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. – R.A Nov 18 '15 at 12:55
  • 1
    for most licences you need to include license.txt if you are distributing their source codes or module. When compiling the APK no source code is being distributed, executable is being distributed. So it's fine to include it in this case. – over_optimistic Feb 28 '16 at 01:32
  • 2
    R.S, you are aware that the convention on Android is to just have an activity in your app that displays all of the libraries you use and their respective licenses, yes? No license literally says you have to include it as a .txt file, and in fact including it in your APK would accomplish absolutely nothing. – Veselin Romić Mar 03 '16 at 17:40
  • The `packagingOptions` must be in the `android` object. – loretoparisi Sep 21 '16 at 14:08
  • 1
    use `pickFirst` instead of `exclude` to prevent losing the licenses – 4ndro1d Apr 18 '17 at 09:39
51

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

You also can try:

packagingOptions {
   merge 'META-INF/license.txt'
}
R.A
  • 1,813
  • 21
  • 29
23

just add

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

in build.gradle

shankey
  • 343
  • 2
  • 9
16

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'
    }
}
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
AlfredZhong
  • 161
  • 2
  • 3
11

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')
}
Dhiral Pandya
  • 10,311
  • 4
  • 47
  • 47
  • Thank you! I was banging my head against this for ~5 hours today. :) It's the `packagingOptions { exclude 'libs/jackson...' }` that fixed it. – CJBrew Jul 28 '16 at 16:01
8

Adding:

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

worked for me, biniam_Ethiopia's solution is probably the most fail-safe

Jan Wilmans
  • 665
  • 6
  • 10
8

While inserting this code

android{

packagingOptions{
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license.txt'
 }
}

MAKE SURE if in error it is showing

> Duplicate files copied in APK META-INF/LICENSE.txt

then add

 android{

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

}

if in error it is showing

> Duplicate files copied in APK META-INF/LICENSE

then add

 android{

packagingOptions{
    exclude 'META-INF/LICENSE'
 }

}

if in error it is showing

> Duplicate files copied in APK META-INF/license.txt

then add

 android{

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

}

In short text CASE and document FORMAT(.txt) is so important.

(this error exist in Android Studio 1.1.0 also)

josliber
  • 43,891
  • 12
  • 98
  • 133
Shreekant N
  • 858
  • 1
  • 13
  • 28
6

This will help you solve the problem

packagingOptions {
    exclude 'META-INF/ASL2.0'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/DEPENDENCIES'
}
Wajahat
  • 1,593
  • 3
  • 20
  • 47
Suresh Sarak
  • 119
  • 1
  • 1
4
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'
}

Add in build.gradle file and syn project

3

I just add 2:

android{

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

}
josedlujan
  • 5,357
  • 2
  • 27
  • 49
3

This may very well be bad practice, however if you are including multiple large libraries, you may find yourself working through hundreds of these kinds of conflicts.

Listed below is a super-simple fix for such cases:

android { 
    ....
    packagingOptions {
        // Allow the compilation process to choose the dependencies for us.
        pickFirst "**"
    }
}
Mapsy
  • 4,192
  • 1
  • 37
  • 43
2

I had a similar error and solved it without the packingOptions() and exclude function. I was adding two dependencies but one was a sub-group of the first. This caused the error, once I removed one of them I got a clean build. I recommend checking for a similar error within your dependency block.

2

When using java-jwt and jackson-core together use following:

exclude("META-INF/maven/com.fasterxml.jackson.core/jackson-annotations/pom.properties")
exclude("META-INF/maven/com.fasterxml.jackson.core/jackson-annotations/pom.xml")
exclude("META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.properties")
exclude("META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.xml")
exclude("META-INF/maven/com.fasterxml.jackson.core/jackson-databind/pom.properties")
exclude("META-INF/maven/com.fasterxml.jackson.core/jackson-databind/pom.xml")
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112