92

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: 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)?

Community
  • 1
  • 1
Flowryn
  • 1,437
  • 1
  • 16
  • 32
  • 2
    From a technical POV, can you not package things so that all the "must include" files of each library is in their own directory? An alternative I've seen with some apps is for you to (manually) combine all the respective licence/notice files into one resource and include/display this (where two or more libraries share the same licence version, you should be able group them, "Library A and Library B are included subject to the following licence:..."). – TripeHound Dec 15 '15 at 12:09
  • @TripeHound this is what I currently do as a workaround, while in developing process I exclude them and when it comes to release: comment all 'excludes' and solve the licenses manually. – Flowryn Dec 23 '15 at 18:12
  • 1
    searching for the answer "packagingOptions - exclude" deserves an upvote – Ahmed Adel Ismail Jan 26 '16 at 15:06

7 Answers7

47

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.

Marc Plano-Lesay
  • 6,808
  • 10
  • 44
  • 75
  • 1
    I have 2 licenses at this moment, one is from Apache 2.0 the other one GPL 3.0. My current workaround is to exclude them while in developing phase and manually include them when we release. All license.txt will be concatenated. Same for notice.txt Anyway I like your approach with pickFirst in case the license is identical! – Flowryn Dec 15 '15 at 11:35
  • 3
    If you ever find a way to automatically concatenate licenses, I'm all ears! – Marc Plano-Lesay Dec 15 '15 at 12:22
  • This is what I am investigating now. First I need to find out what(and how) is doing the gradle task that generated the conflict(for that I asked this question: https://stackoverflow.com/questions/34287701/how-do-i-find-out-a-gradle-tasks-actions-and-configurations-that-are-composing) And then replace it – Flowryn Dec 15 '15 at 12:32
  • @Flowryn how you include the all notice.txt manually,just copy them in one notice.txt? can not modify it which in `jar` file – chinaanihchen Mar 03 '16 at 02:53
  • you mention that the issue is in the libraries being used...in my case, I'm responsible for creating the libraries I'm using..what I could be doing wrong when I am making them? Thanks – Eric May 12 '16 at 08:03
  • @Eric that's a good question. I can't figure out what I meant by that… I'm removing that part until I can remember. Silly brain :-( – Marc Plano-Lesay Jun 08 '16 at 07:33
  • @Flowryn I don't know if you actually found a proper solution, including different licenses with the same file name, but Google released a plugin that might be useful: https://developers.google.com/android/guides/opensource – Marc Plano-Lesay May 03 '18 at 19:43
32

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'
    }
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
Max Droid
  • 924
  • 9
  • 10
4

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.

aMighty
  • 283
  • 4
  • 11
1

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

AaronC
  • 155
  • 2
  • 9
0

You can add multiple licence in gradle see this

Akhil Jayakumar
  • 2,262
  • 14
  • 25
0

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

Surely it will work

packagingOptions {
 exclude 'META-INF/LICENSE.txt'
 exclude 'META-INF/NOTICE.txt'   }
khelwood
  • 55,782
  • 14
  • 81
  • 108
Mahendran Candy
  • 1,114
  • 17
  • 17