329

Using Android Gradle plugin 0.7.0 with the following build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.0'
    }
}

apply plugin: 'android'

repositories {
    maven { url "https://android-rome-feed-reader.googlecode.com/svn/maven2/releases" }
    maven { url "http://dl.bintray.com/populov/maven" }
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion '18.1.1'

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
    }
    buildTypes {
        release {
            runProguard true
            proguardFile getDefaultProguardFile('proguard-android-optimize.txt')
        }
    }
    productFlavors {
        defaultFlavor {
            proguardFile 'proguard-rules.txt'
        }
    }
    sourceSets {
        instrumentTest.setRoot('src/instrumentTest')
    }
}

configurations {
    apt
}

ext.androidAnnotationsVersion = '2.7.1';

dependencies {
    compile 'com.android.support:support-v4:18.0.0'

    compile 'com.viewpagerindicator:library:2.4.1@aar'

    compile 'com.google.code.android-rome-feed-reader:android-rome-feed-reader:1.0.0-r2'
    compile 'org.jdom:jdom:1.1.1-android-fork'

    apt "com.googlecode.androidannotations:androidannotations:${androidAnnotationsVersion}"
    compile "com.googlecode.androidannotations:androidannotations-api:${androidAnnotationsVersion}"

    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.j256.ormlite:ormlite-android:4.47'
    compile 'com.j256.ormlite:ormlite-core:4.47'
    compile 'org.springframework.android:spring-android-core:1.0.1.RELEASE'
    compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
    compile 'com.prolificinteractive:actionbarsherlock:4.3.1@aar'

    compile 'com.google.code.geocoder-java:geocoder-java:0.15'

    compile files('libs/CWAC-Pager.jar')
}

android.applicationVariants.all { variant ->
    aptOutput = file("${project.buildDir}/source/apt_generated/${variant.dirName}")
    println "****************************"
    println "variant: ${variant.name}"
    println "manifest:  ${variant.processResources.manifestFile}"
    println "aptOutput:  ${aptOutput}"
    println "****************************"

    variant.javaCompile.doFirst {
        println "*** compile doFirst ${variant.name}"
        aptOutput.mkdirs()
        variant.javaCompile.options.compilerArgs += [
                '-processorpath', configurations.apt.getAsPath(),
                '-AandroidManifestFile=' + variant.processResources.manifestFile,
                '-s', aptOutput
        ]
    }
}

gives the error Duplicate files copied in APK META-INF/LICENSE.txt:

WARNING: Dependency commons-logging:commons-logging:1.0.4 is ignored for defaultFlavorDebug as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency commons-logging:commons-logging:1.0.4 is ignored for defaultFlavorRelease as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is scheduled to be removed in Gradle 2.0. Please read http://gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html for information on the replacement for dynamic properties.
Deprecated dynamic property: "aptOutput" on "com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated@5705013c", value: "/Users/david/Developer...".
****************************
variant: defaultFlavorDebug
manifest:  /Users/david/Developer/.../build/manifests/defaultFlavor/debug/AndroidManifest.xml
aptOutput:  /Users/david/Developer/.../build/source/apt_generated/defaultFlavor/debug
****************************
Deprecated dynamic property "aptOutput" created in multiple locations.
****************************
variant: defaultFlavorRelease
manifest:  /Users/david/Developer/.../build/manifests/defaultFlavor/release/AndroidManifest.xml
aptOutput:  /Users/david/Developer/.../build/source/apt_generated/defaultFlavor/release
****************************
:TravelGuard:compileDefaultFlavorDebugNdk UP-TO-DATE
:TravelGuard:preBuild UP-TO-DATE
:TravelGuard:preDefaultFlavorDebugBuild UP-TO-DATE
:TravelGuard:preDefaultFlavorReleaseBuild UP-TO-DATE
:TravelGuard:prepareComProlificinteractiveActionbarsherlock431Library UP-TO-DATE
:TravelGuard:prepareComViewpagerindicatorLibrary241Library UP-TO-DATE
:TravelGuard:prepareDefaultFlavorDebugDependencies
:TravelGuard:compileDefaultFlavorDebugAidl UP-TO-DATE
:TravelGuard:compileDefaultFlavorDebugRenderscript UP-TO-DATE
:TravelGuard:generateDefaultFlavorDebugBuildConfig UP-TO-DATE
:TravelGuard:mergeDefaultFlavorDebugAssets UP-TO-DATE
:TravelGuard:mergeDefaultFlavorDebugResources UP-TO-DATE
:TravelGuard:processDefaultFlavorDebugManifest UP-TO-DATE
:TravelGuard:processDefaultFlavorDebugResources UP-TO-DATE
:TravelGuard:generateDefaultFlavorDebugSources UP-TO-DATE
:TravelGuard:compileDefaultFlavorDebugJava
*** compile doFirst defaultFlavorDebug
/Users/david/Developer/.../src/main/java/com/travelguard/service/TravelGuardService.java:53: cannot find symbol
symbol  : class TravelGuardPrefs_
location: class com.travelguard.service.TravelGuardService
    TravelGuardPrefs_ travelGuardPrefs;
    ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/CategoriesActivity.java:14: cannot find symbol
symbol  : class AssistanceFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.AssistanceFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/CategoriesActivity.java:16: cannot find symbol
symbol  : class CategoryFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.CategoryFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/CategoriesActivity.java:17: cannot find symbol
symbol  : class ContactFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.ContactFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/CountriesActivity.java:25: cannot find symbol
symbol  : class TravelGuardPrefs_
location: package com.travelguard.service
import com.travelguard.service.TravelGuardPrefs_;
                              ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/CountriesActivity.java:44: cannot find symbol
symbol  : class TravelGuardPrefs_
location: class com.travelguard.ui.CountriesActivity
    TravelGuardPrefs_ travelGuardPrefs;
    ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/InstructionsActivity.java:18: cannot find symbol
symbol  : class AssistanceFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.AssistanceFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/InstructionsActivity.java:19: cannot find symbol
symbol  : class ContactFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.ContactFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/InstructionsActivity.java:21: cannot find symbol
symbol  : class InstructionsFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.InstructionsFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/MainActivity.java:36: cannot find symbol
symbol  : class TravelGuardPrefs_
location: package com.travelguard.service
import com.travelguard.service.TravelGuardPrefs_;
                              ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/MainActivity.java:40: cannot find symbol
symbol  : class AssistanceFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.AssistanceFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/MainActivity.java:41: cannot find symbol
symbol  : class ContactFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.ContactFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/MainActivity.java:55: cannot find symbol
symbol  : class TravelGuardPrefs_
location: class com.travelguard.ui.MainActivity
    TravelGuardPrefs_ travelGuardPrefs;
    ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/TermsActivity.java:17: cannot find symbol
symbol  : class TravelGuardPrefs_
location: package com.travelguard.service
import com.travelguard.service.TravelGuardPrefs_;
                              ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/TermsActivity.java:23: cannot find symbol
symbol  : class TravelGuardPrefs_
location: class com.travelguard.ui.TermsActivity
    TravelGuardPrefs_ travelGuardPrefs;
    ^
Note: Starting AndroidAnnotations annotation processing
Note: AndroidManifest.xml file found: /Users/david/Developer/.../build/manifests/defaultFlavor/debug/AndroidManifest.xml
Note: Number of files generated by AndroidAnnotations: 22
Note: Generating source file: com.travelguard.service.SmartTravellerService_
Note: Generating source file: com.travelguard.service.SmartTravellerWebservice_
Note: Generating source file: com.travelguard.service.TravelGuardPrefs_
Note: Generating source file: com.travelguard.service.TravelGuardService_
Note: Generating source file: com.travelguard.service.TravelGuardWebservice_
Note: Generating source file: com.travelguard.ui.AdviceActivity_
Note: Generating source file: com.travelguard.ui.CategoriesActivity_
Note: Generating source file: com.travelguard.ui.ContactDetailActivity_
Note: Generating source file: com.travelguard.ui.ContactListActivity_
Note: Generating source file: com.travelguard.ui.ContactTravelGuardActivity_
Note: Generating source file: com.travelguard.ui.CountriesActivity_
Note: Generating source file: com.travelguard.ui.InformationActivity_
Note: Generating source file: com.travelguard.ui.InstructionsActivity_
Note: Generating source file: com.travelguard.ui.MainActivity_
Note: Generating source file: com.travelguard.ui.TermsActivity_
Note: Generating source file: com.travelguard.ui.TravelGuardActivity_
Note: Generating source file: com.travelguard.ui.fragments.AdviceDetailFragment_
Note: Generating source file: com.travelguard.ui.fragments.AdviceFragment_
Note: Generating source file: com.travelguard.ui.fragments.AssistanceFragment_
Note: Generating source file: com.travelguard.ui.fragments.CategoryFragment_
Note: Generating source file: com.travelguard.ui.fragments.ContactFragment_
Note: Generating source file: com.travelguard.ui.fragments.InstructionsFragment_
Note: Time measurements: [Whole Processing = 913 ms], [Generate Sources = 358 ms], [Extract Manifest = 163 ms], [Process Annotations = 158 ms], [Extract Annotations = 120 ms], [Validate Annotations = 82 ms], [Find R Classes = 28 ms],
Note: Time measurements: [Whole Processing = 0 ms],
Note: Time measurements: [Whole Processing = 0 ms],
warning: The following options were not recognized by any processor: '[androidManifestFile]'
Note: /Users/david/Developer/.../src/main/java/com/travelguard/ui/ContactDetailActivity.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: /Users/david/Developer/.../src/main/java/com/travelguard/service/SmartTravellerService.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:TravelGuard:preDexDefaultFlavorDebug UP-TO-DATE
:TravelGuard:dexDefaultFlavorDebug
:TravelGuard:processDefaultFlavorDebugJavaRes UP-TO-DATE
:TravelGuard:validateDebugSigning
:TravelGuard:packageDefaultFlavorDebug
Error: duplicate files during packaging of APK /Users/david/Developer/.../build/apk/TravelGuard-defaultFlavor-debug-unaligned.apk
    Path in archive: META-INF/LICENSE.txt
    Origin 1: /Users/david/.gradle/caches/modules-2/files-2.1/commons-httpclient/commons-httpclient/3.1/964cd74171f427720480efdec40a7c7f6e58426a/commons-httpclient-3.1.jar
    Origin 2: /Users/david/.gradle/caches/modules-2/files-2.1/commons-codec/commons-codec/1.2/397f4731a9f9b6eb1907e224911c77ea3aa27a8b/commons-codec-1.2.jar
:TravelGuard:packageDefaultFlavorDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':TravelGuard:packageDefaultFlavorDebug'.
> Duplicate files copied in APK META-INF/LICENSE.txt
    File 1: /Users/david/.gradle/caches/modules-2/files-2.1/commons-httpclient/commons-httpclient/3.1/964cd74171f427720480efdec40a7c7f6e58426a/commons-httpclient-3.1.jar
    File 2: /Users/david/.gradle/caches/modules-2/files-2.1/commons-httpclient/commons-httpclient/3.1/964cd74171f427720480efdec40a7c7f6e58426a/commons-httpclient-3.1.jar


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

BUILD FAILED

Total time: 21.957 secs

Things were working fine this morning before I updated Android Studio. Any ideas?

EDIT: I was able to temporarily fix it by running commands like

zip -d spring-android-core-1.0.1.RELEASE.jar META-INF/notice.txt

until all the duplicate errors went away.

Jonik
  • 80,077
  • 70
  • 264
  • 372
David Lawson
  • 7,802
  • 4
  • 31
  • 37
  • See also http://stackoverflow.com/questions/20673888/duplicate-files-copied-android-studio-0-4-0 – Scott Barta Dec 19 '13 at 05:59
  • 2
    This seems to be bug https://code.google.com/p/android/issues/detail?id=61573. I don't know why it's only started being troublesome for you in this release. – Scott Barta Dec 19 '13 at 06:02
  • stuck with the same problem. Decided to stay on Android Studio 0.3.7 and gradle plugin 0.6.3 until bug is resolved – mente Dec 19 '13 at 10:26
  • 6
    You should be clear of your wordings: You aren't using gradle 0.7.0 - it is the android gradle plugin. Which uses gradle (with wrapper) in version 1.9.0. Wrong wordings will lead to confusing - besides of the given context. – onigunn Dec 19 '13 at 10:54
  • They just release 0.7.1 which supposedly fixes this: http://tools.android.com/tech-docs/new-build-system – Eric Schlenz Dec 19 '13 at 21:52
  • 1
    Please note the `pickFirst` option available since [Gradle 0.9.1](http://tools.android.com/tech-docs/new-build-system). – JJD Apr 03 '14 at 13:30

20 Answers20

475

As of Android Studio version 0.8.14

You should add:

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

to your build.gradle file.

History:

According to comment 14 in this bug: https://issuetracker.google.com/issues/36982149#comment14 this is a bug in v0.7.0 of the Android Gradle plugin, and is due to be fixed soon in 0.7.1.

Here are the notes from that bug about the addition for 0.7.1:

0.7.1 is out with the fix for this.

The DSL to exclude files is:

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

You can add as many exclude statement as you want. The value is the archive path. No wildcard or glob support yet.

Filenames "LICENSE.txt" and "NOTICE.txt" are case sensitive. Please try out with "license.txt" and "notice.txt" as well.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • 1
    See my temporary fix above – David Lawson Dec 19 '13 at 09:03
  • Version 0.7.1 of the plugin is out now and the above solution works now. – regretoverflow Dec 20 '13 at 06:05
  • 12
    Thanks! I also had to exclude 'META-INF/NOTICE.txt' ;) – Climbatize Dec 21 '13 at 23:12
  • @wolfovercats you add this to your build.gradle, you already have a android{} block, just add the packagingOptions block to it – Climbatize Dec 21 '13 at 23:17
  • So, apparently it's case-sensitive. I had to use META-INF/notice.txt instead to get past this issue. But, thanks for the answer! You saved me a lot of trouble! – praneetloke Dec 24 '13 at 02:20
  • 72
    Day by day I am astonished by how programmers could have been remotely productive before stackoverflow arrived. Thanks for the fix! I had to exclude META-INF/LICENSE and /NOTICE instead. (No txt, even on latest gradle.) – domenukk Jan 01 '14 at 19:09
  • things like this would make you stop using great tools like gradle... pre-SO. Thanks! – albertpeiro Mar 26 '14 at 19:48
  • 3
    What file i should edit and where it can be found? thanks, Eyal. – eyal Mar 30 '14 at 12:25
  • @praneetloke please help me : How to exclude META-INF/notice.txt? – UmAnusorn Apr 03 '14 at 13:07
  • Here's an example build.gradle that includes the solution: https://github.com/OneBusAway/onebusaway-android/blob/master/onebusaway-android/build.gradle You can add multiple files to exclude. – Sean Barbeau Apr 03 '14 at 20:39
  • 2
    @um.anusorn Sean has given you a good example for a gradle file that excludes these files from libraries during packaging. This is what I have in mine: packagingOptions { exclude 'META-INF/LICENSE' exclude 'META-INF/NOTICE' exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/license.txt' exclude 'META-INF/dependenices.txt' exclude 'META-INF/notice.txt' } – praneetloke Apr 04 '14 at 14:54
  • 4
    Still prevalent bug in v0.12 of built tools.. Just saying – Nilzor Aug 07 '14 at 09:54
  • Added the .txt extensions to /LICENSE and /NOTICE. Fixed the issue for me. – Chris Amelinckx Nov 14 '14 at 21:15
  • 1
    @domenukk We had less complicated tools by our side! – basickarl Feb 11 '15 at 06:59
  • 1
    26.04.2015 - Still need that in Android Studio v1.1.0 – Stefan Falk Apr 26 '15 at 17:11
  • Do you mean Android Studio 0.8.14? – Lou Morda Oct 21 '15 at 15:25
  • 1
    Had to do this version 1.4.1 - after adding Spring for Android and GSON to my Gradle file. Would not work unless the 'NOTICE' and 'LICENSE' were lowercase - 'notice.txt', 'license.txt' – DtechNet Nov 08 '15 at 23:31
  • Not worked for me, still shows Warning:Dependency org.apache.httpcomponents:httpclient:4.3 is ignored for debug as it may be conflicting with the internal version provided by Android. In case of problem, please repackage it with jarjar to change the class packages – VVB Feb 08 '16 at 09:19
  • 1
    You can also use [pickFirst](https://stackoverflow.com/a/43534151/1121497). Consider commenting that in your solution. Thanks! – Ferran Maylinch Oct 31 '17 at 09:23
  • a regex to relieve the pain `excludes += ['META-INF/*']` – rahul.taicho Mar 21 '18 at 17:52
  • 2021-05-22: Android 4.x and I think I don't need this anymore. Finally. This has been in my gradle file for 7 years! – Mendhak May 22 '21 at 18:51
  • I know this is old, but wildcards work now (I'm using Gradle 7.4) for example pickFirst 'lib/x86/*.so' – NoLoHo Nov 23 '22 at 10:58
149

In my case I had to include several additional exclusions. It appears it doesn't like Regular expressions which would've made this a nice one-liner.

android {
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/notice.txt'
    }
}
Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119
vincentjames501
  • 2,131
  • 1
  • 13
  • 7
  • I only needed `LICENSE` and `NOTICE` after I added `commons-lang3`. – theblang Feb 18 '14 at 17:18
  • This one helped me (I have AndroidAnnotations + Spring REST template). With having to add such hacks this IntelliJ product dreadfully starts to resemble Eclipse... – javaxian May 15 '14 at 09:37
  • Don't know which one affects, but this is really helpful. – CopperCash Jul 04 '14 at 07:21
  • Not worked for me, still shows Warning:Dependency org.apache.httpcomponents:httpclient:4.3 is ignored for debug as it may be conflicting with the internal version provided by Android. In case of problem, please repackage it with jarjar to change the class packages – VVB Feb 08 '16 at 09:18
46
packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
}
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
user3121011
  • 449
  • 3
  • 12
31

Have a look at Sakiboy's comment!


Outdated answer

From Gradle 0.9.1 the following is supported:

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

More information in the Gradle release notes.

JJD
  • 50,076
  • 60
  • 203
  • 339
  • 3
    This is a better solution as developers have an obligation to include these files when they use open source software. – Louth Oct 30 '14 at 22:38
  • 1
    Nice option, but this comment is totally wrong. Only the licence from the firs library will be included all the rest will be left out. – Igor Čordaš Apr 01 '15 at 14:19
  • 1
    this is useful if 2 libraries are using the same .so files – Fabian Jan 27 '16 at 15:59
  • Can I use that for two JARs that contain the same .class file? I have two JARs included that both contain org/xmlpull/v1/XmlPullParser.class – WillC Apr 18 '17 at 09:02
  • Not sure. Please post a new question on Stackoverflow to get answers. – JJD Apr 26 '17 at 08:42
  • 2
    @PSIXO, you can see the answer below if you want to include ALL the licenses: https://stackoverflow.com/a/47828425/2371425 – Sakiboy Dec 15 '17 at 08:33
12

The same problem when I export the library httclient-4.3.5 in Android Studio 0.8.6 I need include this:

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

The library zip content the next jar:

commons-codec-1.6.jar
commons-logging-1.1.3.jar
fluent-hc-4.3.5.jar
httpclient-4.3.5.jar
httpclient-cache-4.3.5.jar
httpcore-4.3.2.jar
httpmime-4.3.5.jar
rayryeng
  • 102,964
  • 22
  • 184
  • 193
ferdiado
  • 369
  • 3
  • 8
8

This bug still exists in 0.8+/1.10

With Jackson

compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-csv:2.2.2'

I had to include as well as the above suggestion before it would compile

exclude 'META-INF/services/com.fasterxml.jackson.core.JsonFactory'
Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119
6

It's more than one error

Under apply plugin: 'android-library'

add this ::

android {
    packagingOptions {
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }
}

In case of duplicate files it's easy, look inside the JAR under the META-INF dir and see what's causing the error. It could be multiple. In my case Couchbase Lite plugin. As you add more plugins, you will need more exceptions

Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42
Pian0_M4n
  • 2,505
  • 31
  • 35
  • Stupid question: where is that JAR file? – WillC Apr 18 '17 at 08:06
  • In Android Studio, running Gradle with the `--info` flag let me see the log of classes being added to the apk, and I could then search upward to find the log entry for the same class being added from a different JAR. This also showed me the file path to both JAR files. I couldn't find a good way to choose one .class file over the other, so I modified one of the JAR files and removed the duplicate .class – WillC Apr 19 '17 at 03:55
4

The same problem when I used 'org.springframework.android:spring-android-rest-template:2.0.0.M1' in Android Studio 1.0.1. I need include this in build.gradle

android{
...
    packagingOptions{
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
    }
...
}
saneryee
  • 3,239
  • 31
  • 22
3

This works for me:

android {
   packagingOptions {
       exclude 'LICENSE.txt'
   }
}
Sestertius
  • 1,367
  • 1
  • 14
  • 13
3

I think you need to include only these options in build.gradle:

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

p.s same answer from my post in : Error :: duplicate files during packaging of APK

Community
  • 1
  • 1
Gent
  • 6,215
  • 1
  • 37
  • 40
3

If you want to do your part as developer, utilizing open source libraries, you should try including all those open source licenses within your apk. To do this, you can use the merge method in your packagingOptions.

Example:

packagingOptions {
        // This will get include every license and notice regardless of what dir it’s in.
        merge '**/LICENSE.txt'
        merge '**/NOTICE.txt'
        merge '**/notice.txt'
        merge '**/license.txt'
        merge '**/NOTICE'
        merge '**/LICENSE'
        merge '**/notice'
        merge '**/license'
        merge '**/LGPL2.1'
        // This will exclude any README files, regardless of the dir or the file type.
        exclude '**/README.*'
}

This answer is better than using pickFirst because that method only picks the first license it finds and disregards all the rest, kinda rendering it useless in this case.

So in short, use the merge method to include all those licenses from those kickass open source libraries you’ve been using.

More info on Gradle PackagingOptions.

Sakiboy
  • 7,252
  • 7
  • 52
  • 69
2

The problem is that the META-INF folder won't get filtered so multiple entries of NOTICE or LICENSE cause duplicates when building and it is tryed to copy them together.

Dirty Quick Fix:

Open the .jar file in your .gradle/caches/... folder (with a zip compatible tool) and remove or rename the files in the META-INF folder that cause the error (usally NOTICE or LICENSE). (I know thats also in the OP, but for me it was not really clear until I read the google forum)

EDIT:

This was fixed in 0.7.1. Just add the confilcting files to exclude.

android {
    packagingOptions {
        exclude 'META-INF/LICENSE'
    }
}
Patrick
  • 33,984
  • 10
  • 106
  • 126
  • Oh sorry I just understand it now. – UmAnusorn Apr 03 '14 at 15:14
  • Maybe the worst possible answer : altering the jars in the gradle cache may change their checksum, and as a consequence won't work for long ... besides, thoses are not your jars – Riduidel Jun 28 '17 at 07:55
  • Yeah, that's why it says "Dirty Quick Fix" in bold letters. Also the correct solution is just a paragraph away. You do realise that was written at a time where there was no clean solution, but people still needed to compile their apps. – Patrick Jun 28 '17 at 09:52
2

I noticed this commit comment in AOSP, the solution will be to exclude some files using DSL. Probably when 0.7.1 is released.

commit e7669b24c1f23ba457fdee614ef7161b33feee69
Author: Xavier Ducrohet <--->
Date:   Thu Dec 19 10:21:04 2013 -0800

    Add DSL to exclude some files from packaging.

    This only applies to files coming from jar dependencies.
    The DSL is:

    android {
      packagingOptions {
        exclude 'META-INF/LICENSE.txt'
      }
    }
Maxime
  • 2,048
  • 1
  • 27
  • 38
  • yeah and they just wrote the release notes on the build site so hopefully they'll upload the artifact to maven central soon so we can use this! – Matt Wolfe Dec 19 '13 at 22:16
  • What if I don’t want to exclude them, just rename them so they don’t collide? – mirabilos Sep 08 '20 at 21:08
2

Important to know in what file it comes to this error (in you example it is META-INF/LICENSE.txt) , in my case it was in META-INF/LICENSE [without ".txt"], and then in the file META-INF/ASL2.0 so I added to my build.gradle this lines:

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

Very important (!) -> add the name of the file in the same style, that you see it in the error message: the text is case sensitive, and there is a difference between *.txt and *(without "txt").

RonU
  • 5,525
  • 3
  • 16
  • 13
2

Same here with

dependencies {
    compile 'org.apache.oltu.oauth2:org.apache.oltu.oauth2.client:1.0.0'
}

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

I lost like 2 days for that weird error... Why is this still happening in gradle 1.0.0 ? That is very disturbing for newbies... Anyway, thanks for that info i thought it was on my code :)

1

In my case I only need to add to project's build.gradle file:

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

   ...
}
K. Stopa
  • 597
  • 8
  • 9
0

In Android Studio 1.1.0 i needed lower case names:

packagingOptions{
      exclude 'META-INF/license.txt'
      exclude 'META-INF/notice.txt'
}
kandroidj
  • 13,784
  • 5
  • 64
  • 76
0

Files "LICENSE.txt" and "NOTICE.txt" are case sensitive. So for SPring android library I had to add

android {
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/notice.txt'
    }
}
Igor Vuković
  • 742
  • 12
  • 25
0

Removing .txt after LICENSE removed my error :

packagingOptions {
    exclude 'META-INF/LICENSE'
}
eli-k
  • 10,898
  • 11
  • 40
  • 44
JSONParser
  • 1,112
  • 2
  • 15
  • 30
0
  packagingOptions {
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/dependencies.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/LGPL2.1'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/notice.txt'
}
alias26
  • 13
  • 6