42

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?

richsage
  • 26,912
  • 8
  • 58
  • 65
TrungNVT
  • 671
  • 2
  • 6
  • 8

6 Answers6

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.

Jehy
  • 4,729
  • 1
  • 38
  • 55
  • 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. Thanks – raddevus Nov 01 '16 at 21:11
  • `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'` – CorayThan Jan 12 '17 at 00:33
  • I prefer the `pickFirst` solution as it would appear you can't use wildcards with `exclude` – Will Calderwood Aug 17 '17 at 13:25
  • There is now also a `merge` function in [packagingOptions](https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.PackagingOptions.html), that merges multiple files into one, which is probably better for LICENSE files, than excluding them, or picking only one. – gregn3 Oct 01 '17 at 18:57
  • 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.Processor – Sujeet Oct 07 '19 at 12:15
54

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.

Community
  • 1
  • 1
Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • Thanks for your help.
    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.
    ` 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?
    – TrungNVT Jan 19 '15 at 02:38
  • 1
    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. – Scott Barta Jan 19 '15 at 18:01
  • @ScottBarta thank you for being humble and admire that you don't know I liked that too much bro – Moustafa EL-Saghier Feb 22 '22 at 14:45
12

The simplest solution is to add

 packagingOptions {
    pickFirst  'META-INF/*'
}

to your build.gradle in android section

Jorge Arimany
  • 5,814
  • 2
  • 28
  • 23
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  '**'
}
Mapsy
  • 4,192
  • 1
  • 37
  • 43
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

Samir
  • 6,405
  • 5
  • 39
  • 42
-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

  • 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. – cmprogram Nov 06 '18 at 17:11