Android Studio: Duplicate files copied in APK META-INF/DEPENDENCIES when compile
In Android Gradle builds, you're not permitted to include the same file with the same path more than once in the output. In your build, there were two META-INF/DEPENDENCIES
files coming from different places. Since you don't need this file at all in your application, the simplest thing to do is to tell the build system to ignore it altogether, which is what this exclude
directive does.
There's also a pickFirst
directive to tell the build system to keep one of the copies; there's a tiny amount of detail on that in Android Gradle plugin 0.7.0: "duplicate files during packaging of APK".
Android builds in Gradle are rather strict about duplicate files, which can make life difficult. There's a similar problem if you include the same Java class more than once, where you get the "Multiple dex files define" error (see Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat) for a typical example).
Other build systems are more lenient. It's typical in Java that if you include the same class more than once in a classpath, for example, the first copy it sees is the one that's used; duplicates after that are ignored. This is in most cases easier to deal with, but it has a couple problems. The biggest one is that there can be subtle errors if multiple different versions of a file creep into the build without you knowing -- it can be difficult to figure out what's going on. When you do figure it out, you can usually solve it by juggling the order in which things are included to make sure the one you want makes it to the final output, but in very complex builds, this can be difficult to achieve, and it can happen that doing seemingly unrelated things like including new libraries in your project can upset the ordering and lead to a lot of woe.
For that reason, Gradle has the philosophy of not relying on ordering of things to determine "winners" in the game of resolving duplicates, and it forces the developer to make all dependencies explicit. Android's implementation of its build system on top of Gradle follows that philosophy.
Android Gradle Duplicate files copied in APK META-INF/license.txt
Write below lines in your app level gradle file
android {
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'
}
}
How do I resolve Duplicate files copied in APK META-INF/*
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.
Duplicate Files copied in APK META-INF when Gradle building
I had to add the following lines to packagingOptions {}
exclude 'META-INF/maven/commons-io/commons-io/pom.xml'
exclude 'META-INF/maven/commons-io/commons-io/pom.properties'
My problem was pretty foolish; when including exclude 'META-INF/maven/commons-io/commons-io/pom.xml'
I got an error that looked very similar to the first one, but was in fact different. adding exclude 'META-INF/maven/commons-io/commons-io/pom.properties'
finally got everything working. Still not sure why exactly I have to take these steps though.
Android Gradle Duplicate files copied in APK META-INF
add another to your packagingOptions
exclude 'META-INF/spring.schemas'
EDIT: keeeeep adding to it! looks like you've got a boatload of duplicates
Whenever you see Exception: Duplicate files copied in APK BLAH/blah.blah
add another exclude 'BLAH/blah.blah'
Android Studio 0.4 Duplicate files copied in APK META-INF/LICENSE.txt
Putting the dependecies at the top and the packageOptions at the end worked for me.
apply plugin: 'android'.
Here is my full build.gradle at the app folder.
dependencies {
compile 'com.android.support:support-v4:+'
compile files('libs/apache-mime4j-0.6.jar')
compile files('libs/httpmime-4.0.jar')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
defaultConfig {
minSdkVersion 7
targetSdkVersion 10
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard- rules.txt'
}
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'
}
}
EDIT: Almost all OS licence include the obligation to "include a copy of the licence" into your project. So this means, that you have to include a copy of all OS licences you use into you projects. By "excluding" them in gradle, you violate the licences.
Excluding them from the project might not be the best option.
Thank you R.S. for the info.
Android Duplicate files copied in APK META-INF/services/
I just had almost exactly the same problem.
adding this code, just like TML said, solved it for me:
android {
packagingOptions {
exclude 'META-INF/services/com.fasterxml.jackson.databind.Module'
}
}
Related Topics
How to Show and Hide Actionbar with Appcompat V.7
Getting Permission Denial Exception
Proguard with Ormlite on Android
How to Use the Gradle Build System for Android with Eclipse
Android Ndk Build, Method Could Not Be Resolved
Why the View Keeps Flashing When Using Jetpack Navigation with Compose
App-Release-Unsigned.Apk Is Not Signed
Android Studio: New Project VS New Module
Android: Capturing the Return of an Activity
Android Room - Select Query with Like
Get Image from the Gallery and Show in Imageview
Continually Running Background Service
Continuous Speech Recognition Android
Java.Lang.Noclassdeffounderror: Com.Google.Android.Gms.Internal.Zzmp
How to Pause Frame Animation Using Animationdrawable
Initiate a Phone Call on Android with Special Character #
Android Phone Orientation Overview Including Compass
Android Preferences: How to Load the Default Values When the User Hasn't Used the Preferences-Screen