Android Studio: Duplicate files copied in APK META-INF/DEPENDENCIES when compile
Asked Answered
T

6

43

I exported my project from Eclipse and imported to Android Studio using the instructions in this link: http://developer.android.com/sdk/installing/migrate.html

When I build, I have an error:

Duplicate files copied in APK META-INF/DEPENDENCIES

After searching, I found a solution: add

packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
}

into build.gradle. And it works!

But I don't understand why I had this error and why I've had to apply that fix. Can anybody explain?

Trantham answered 16/1, 2015 at 4:39 Comment(3)
I just updated to Android Studio 2.2 and got this error. It was fixed by changing 'META-INF/DEPENDENCIES.txt' to 'META-INF/DEPENDENCIES'.Barton
Same for me after Android Studio 2.2 but instead with "META-INF/LICENSE"Cactus
When I add this line, it just hangs for eternity when building the project?Digestif
L
55

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.

Levity answered 16/1, 2015 at 17:34 Comment(3)
Thanks for your help. <br/> But i don't know why there were two META-INF/DEPENDENCIES files in my build. The message shows duplicate files is in the same location. <br/> ` Execution failed for task ':packageDebug'. > Duplicate files copied in APK META-INF/DEPENDENCIES File 1: D:\Study\Android\AndroidStudio\MyProject\libs\apache-mime4j-core-0.7.2.jar File 2: D:\Study\Android\AndroidStudio\MyProject\libs\apache-mime4j-core-0.7.2.jar ` Did Android Studio auto generate it?Trantham
That's actually a good question -- I don't know. I've seen that before but have never tried to get to the bottom of it.Levity
@ScottBarta thank you for being humble and admire that you don't know I liked that too much broNoach
C
67

While Scott Barta's answer is correct, is lacks a simple and common solution: just add

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

to your build.gradle to ignore those duplicates.

Corundum answered 1/12, 2015 at 15:29 Comment(5)
I was getting this problem in Android Studio 2.2.2 (11/2016) and could not get it to build. I had just upgraded to AnddroidStudio 2.2.2 and adding these changes fixed it and Gradle finally built and I was able to run my project. I have dependencies on Firebase and Admobs. ThanksCorpuscle
pickFirst also works instead of exclude. Also, if you have other similar errors, add those too. For example, I had to add pickFirst'META-INF/plexus/components.xml'Flinger
I prefer the pickFirst solution as it would appear you can't use wildcards with excludeTumbleweed
There is now also a merge function in packagingOptions, that merges multiple files into one, which is probably better for LICENSE files, than excluding them, or picking only one.Infringement
I'm still facing the same issue none of the solutions worked,here's the error: Cause: duplicate entry: META-INF/services/javax.annotation.processing.ProcessorLectureship
L
55

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.

Levity answered 16/1, 2015 at 17:34 Comment(3)
Thanks for your help. <br/> But i don't know why there were two META-INF/DEPENDENCIES files in my build. The message shows duplicate files is in the same location. <br/> ` Execution failed for task ':packageDebug'. > Duplicate files copied in APK META-INF/DEPENDENCIES File 1: D:\Study\Android\AndroidStudio\MyProject\libs\apache-mime4j-core-0.7.2.jar File 2: D:\Study\Android\AndroidStudio\MyProject\libs\apache-mime4j-core-0.7.2.jar ` Did Android Studio auto generate it?Trantham
That's actually a good question -- I don't know. I've seen that before but have never tried to get to the bottom of it.Levity
@ScottBarta thank you for being humble and admire that you don't know I liked that too much broNoach
D
12

The simplest solution is to add

 packagingOptions {
    pickFirst  'META-INF/*'
}

to your build.gradle in android section

Distance answered 21/4, 2017 at 4:48 Comment(1)
Great! I guess you can also do pickFirst 'META-INF/**' for subfolders, although usually there are no subfolders there.Gourd
V
6

The easiest way I've found to resolve this problem is to use a wildcard, so you don't find yourself having to manually declare each file in conflict.

packagingOptions {
    pickFirst  '**'
}
Vibraphone answered 20/12, 2017 at 9:51 Comment(0)
Q
0

In case that anyone having these problem while uploading new .apk to Google Play Store, after updatng Android Studio ;

click V1 Jar Signature not Full Apk Signature while Generating new Apk with old Keystore

V1 Jar Signature

enter image description here

Quincuncial answered 10/8, 2017 at 12:20 Comment(0)
S
-2

dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support:design:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'com.google.android.gms:play-services-ads:10.2.1' implementation 'com.android.support:support-annotations:25.0.1' testImplementation 'junit:junit:4.12'

**// select only one in two line below**  implementation ‘package’    //implementation project(‘:package’)

}

// good luck

Secondly answered 6/11, 2018 at 17:5 Comment(1)
Hi and welcome to Stack Overflow. This is not an appropriate answer. You should provide sufficient context as to how to apply this answer, and also ensure your code is indented appropriately.Bastardy

© 2022 - 2024 — McMap. All rights reserved.