How do I resolve "Duplicate files copied in APK META-INF/*"

JavaAndroidGradle

Java Problem Overview


I am working at a commercial android application. I am also using some libraries licensed under different license types some of them stating the following:

If the library has a "NOTICE" file with attribution notes, you must include that NOTICE when you distribute

(One of them is licensed under Apache License 2.0 for example).

There is more than one library. When I do the build with gradle or with Android Studio I obtain the following build error:

* 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 META-INF/license.txt

The answers that I found until now on the internet and stackoverflow suggest to remove the license.txt(notice.txt or other files that could interfere like this) from packaging by adding to build.gradle file the following:

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'
}

See for example: https://stackoverflow.com/questions/20827885/android-studio-0-4-duplicate-files-copied-in-apk-meta-inf-license-txt

According to the license of those libraries(Apache License 2.0 for instance), the license and notice files should be included.

My question: How can I add multiple files related to licensing(such as license.txt, notice.txt etc) from gradle into my project in order to be compliant with the licenses(technical detail: licences texts will be concatenated)?

Java Solutions


Solution 1 - Java

There is a solution if you have only one license using the name license.txt (read: all license.txt copies are identical):

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

Otherwise, Google also released a Gradle plugin to manage dependencies licenses. See here. I didn't try it, but it looks like it's capable of aggregating every dependency, and even generating an activity displaying all of those licenses.

Solution 2 - Java

Add following into respective build.gradle file

packagingOptions {
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/MANIFEST.MF'
    }

Solution 3 - Java

I faced the same issue with my application. You need to make sure you have not added any libraries twice. If you have followed the firebase documentation https://firebase.google.com/docs/android/setup

Then you should not add firebase library inside android studio i.e. file->project structure->cloud->firebase

You have to do only one of the both, to use firebase in your android application.

At the end clean and rerun your app.

Solution 4 - Java

As an alternative to Marc Plano-Lesay's answer, you can also merge the files:

packagingOptions {
    merge "META-INF/license.txt"
}

Reference: Gradle API 4.2 Packaging Options

Solution 5 - Java

You can add multiple licence in gradle see this

Solution 6 - Java

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

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

Solution 7 - Java

Surely it will work

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFlowrynView Question on Stackoverflow
Solution 1 - JavaMarc Plano-LesayView Answer on Stackoverflow
Solution 2 - JavaMax DroidView Answer on Stackoverflow
Solution 3 - JavaaMightyView Answer on Stackoverflow
Solution 4 - JavaAaronCView Answer on Stackoverflow
Solution 5 - JavaAkhil JayakumarView Answer on Stackoverflow
Solution 6 - JavaS.sadham hussainView Answer on Stackoverflow
Solution 7 - JavaMahendran CandyView Answer on Stackoverflow