24

In my gradle file, I have the following:

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

According to the documentation:

/**
 * Adds an excluded paths.
 * @param path the path, as packaged in the APK
 */

What does this mean? Could someone give me a real life example of why these exclusions would need to be made?

Mahozad
  • 18,032
  • 13
  • 118
  • 133
Daiwik Daarun
  • 3,804
  • 7
  • 33
  • 60
  • 10
    If 2+ libraries contain those files, you'll get a build conflict. Since you don't need those files in your APK, the typical solution is to exclude them. – CommonsWare Feb 15 '16 at 19:29
  • 1
    http://stackoverflow.com/questions/27977396/android-studio-duplicate-files-copied-in-apk-meta-inf-dependencies-when-compile – Doug Stevenson Feb 15 '16 at 19:35
  • Is there a difference between setting this under `android{ buildTypes{ packagingOptions{...} } }` vs `android{ packagingOptions{...} }`? – Daiwik Daarun Feb 15 '16 at 19:49
  • 1
    the `packagingOptions` closure is not available on the `buildTypes` block. Your creating a new `buildType` by placing it there – JBirdVegas Feb 16 '16 at 09:17

2 Answers2

22

If you were to change the extension of a few aar files to zip and open them eventually you will have two aar files with files that with the same path.

SomeDependency-A.aar
-META-INF/LICENSE
...

SomeDependency-B.aar
-META-INF/LICENSE
...

When the aar dependencies are merged it fails because it tries to add the file LICENSE and it already exists.

We resolve this by excluding the duplicated files

android {
    packagingOptions {
        exclude 'META-INF/LICENSE'
    }
}
JBirdVegas
  • 10,855
  • 2
  • 44
  • 50
3

For Kotlin DSL (build.gradle.kts) and Android Gradle Plugin (AGP) version 7.0.0 and higher the exclude method is deprecated in favor of the resources.excludes property:

android {
  // ...
  packagingOptions {
    resources.excludes += "META-INF/LICENSE*"
    resources.excludes += "META-INF/NOTICE.txt"
    // OR
    // resources.excludes += setOf(
    //   "META-INF/LICENSE*",
    //   "META-INF/NOTICE.txt"
    // )
  }
}
Mahozad
  • 18,032
  • 13
  • 118
  • 133
  • 1
    And further than that, with version 8.0.0 "packagingOptions" is deprecated in favour of "packaging". – Markers Jul 24 '23 at 11:32