'Why are gradle build packagingOptions needed?

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?



Solution 1:[1]

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

Solution 2:[2]

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"
    // )
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JBirdVegas
Solution 2