601

When I build my app, I get the following error:

Error: Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. More than one file was found with OS independent path 'META-INF/LICENSE'

This is my build.gradle file:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "cn.sz.cyrus.kotlintest"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        javaCompileOptions{
            annotationProcessorOptions{
                includeCompileClasspath = true
            }
        }
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        /*     
          exclude 'META-INF/DEPENDENCIES'
          exclude 'META-INF/NOTICE'
          exclude 'META-INF/LICENSE'
          exclude 'META-INF/LICENSE.txt'
          exclude 'META-INF/NOTICE.txt'
        */
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.1'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
    testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
    compile 'com.github.GrenderG:Toasty:1.2.5'
    compile 'com.orhanobut:logger:1.15'

    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'com.umeng.analytics:analytics:latest.integration'
    compile 'ai.api:libai:1.4.8'
    compile 'ai.api:sdk:2.0.5@aar'
    // api.ai SDK dependencies
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'commons-io:commons-io:2.4'
    compile 'com.android.support:multidex:1.0.1'
}

When I add this code to my build.gradle file:

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

This error would be solved, but another problem will happen. Like this:

java.lang.NoClassDefFoundError: com.squareup.leakcanary.internal.HeapAnalyzerService
at com.squareup.leakcanary.LeakCanary.isInAnalyzerProcess(LeakCanary.java:145)
at cn.sz.cyrus.wemz.TestApplication.onCreate(TestApplication.kt:32)

Who has ideas how to solve this?

Mahozad
  • 18,032
  • 13
  • 118
  • 133
Cyrus
  • 8,995
  • 9
  • 31
  • 58
  • 2
    I faced the same issue when accessing Google API Client Library for Android. Solved the issue by excluding the module HttpClient from gradle dependencies. Refer Google docs for more details https://developers.google.com/api-client-library/java/google-api-java-client/setup search the docs page with "On Android, you will need to explicitly exclude unused dependencies:" – mifthi Sep 28 '19 at 12:31
  • Try change minimum Android version >= 21 in your build.gradle android{} – Jemt tinhwa May 04 '20 at 05:55

35 Answers35

876

You can add this in yourProject/app/build.gradle inside android{}. The exclude function adds the named resource to the list of resources that are not packaged in the APK.

android {      
      packagingOptions {
        exclude("META-INF/DEPENDENCIES")
        exclude("META-INF/LICENSE")
        exclude("META-INF/LICENSE.txt")
        exclude("META-INF/license.txt")
        exclude("META-INF/NOTICE")
        exclude("META-INF/NOTICE.txt")
        exclude("META-INF/notice.txt")
        exclude("META-INF/ASL2.0")
        exclude("META-INF/*.kotlin_module") 
      }          
}

The exclude function is deprecated in 7.0.2 and you should use something similar to this:

android {
   ...
   packagingOptions {
       resources.excludes.add("META-INF/*")
   }
}
Shaaban Ebrahim
  • 9,766
  • 1
  • 18
  • 20
  • 81
    Can you please explain why this works and what the meaning of "META-INF" is? Thank you! – Shreshth Kharbanda Jan 07 '18 at 02:50
  • 2
    @CodeSlave it causes some problem with apk if we didn't exclude theses files from https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.PackagingOptions.html Exclude Paths that match an exclude pattern will not be included in the APK. that all i know . i have not complete idea about this problem – Shaaban Ebrahim Jan 21 '18 at 12:47
  • Can you tell a bit about this? I tried and it worked but my error said it discover two 'META-INF/DEPENDENCIES'. As I'm packaging this file as well, it should have given me error again, right? – Kumar Gaurav Feb 03 '18 at 16:30
  • Adding this to my projects leads to many other errors, what can be done now? – hetsgandhi Aug 01 '18 at 05:20
  • @KumarGaurav i didn't get it – Shaaban Ebrahim Aug 01 '18 at 08:15
  • 1
    @ShaabanEbrahim I have found the solution for this problem in my app. Explicit jar dependency was the issue. check https://stackoverflow.com/a/52965839/1199154 – Sandeep Yohans Oct 24 '18 at 09:49
  • @Shaaban Ebrahim : I've been sitting with the Google API for a couple of weeks now, why does this cause an issue now? What are these things for anyhow? – goldenmaza Jan 15 '19 at 13:43
  • 56
    While this may answer the question it's better to add some description on how this answer may help to solve the issue. Please read [_How do I write a good answer_](https://stackoverflow.com/help/how-to-answer) to know more. – Roshana Pitigala Feb 10 '19 at 05:11
  • 5
    In my case I also needed to add 'META-INF/LICENSE.md' and 'META-INF/LICENSE-notice.md' – Klyner Apr 24 '19 at 09:05
  • I just updated the androidx core libraries in my app's build.gradle and it solved the problem. 1. androidx.core:core: 2. androidx.core:core-ktx: – Jaspal Dec 14 '19 at 04:18
  • 3
    Add exclude("META-INF/*.kotlin_module") in packaginOptions list – Jaspal Dec 14 '19 at 05:03
  • this answer is correct. if in a project someone has more modules like library projects, this packagingOptions {} should be in app/ Gradle ,exactly in android tags. – charitha amarasinghe Apr 17 '20 at 07:07
  • To me, the reason was adding this into build.gradle(app) so I deleted, then everything is fine now: implementation 'com.google.cloud:google-cloud-storage:1.113.6' – Bay Jan 02 '21 at 15:25
  • @ShaabanEbrahim This is not and explanation: " it causes some problem with apk if we didn't exclude theses files from google.github.io/android-gradle-dsl/current/… Exclude Paths that match an exclude pattern will not be included in the APK. that all i know . i have not complete idea about this problem" – Farid Jul 04 '22 at 12:05
  • 1
    Does not work for me, I don't see how these exclusions refer to '2 files found with path 'win32-x86-64/attach_hotspot_windows.dll'. I'm compiling android where the heck is this coming from and I'm on a MAC ' – JPM Sep 30 '22 at 16:38
  • What's the purpose of META-INF? https://stackoverflow.com/questions/70216/whats-the-purpose-of-meta-inf – Michael Geier Mar 01 '23 at 10:20
  • I had just added this implementation group: 'org.apache.httpcomponents.client5', name: 'httpclient5', version: '5.2.1' into build.gradle. Did not need it. Removed it. Problem solved. – Mick Jun 06 '23 at 20:40
  • Read the error properly. I updated the plugins which caused an error and the error gone. :) – Aanal Shah Jun 13 '23 at 13:19
  • 1
    packagingOptions is now deprecated, suggest renaming to packaging – ayitinya Jun 20 '23 at 08:17
215

In my case it was enough to exclude only path 'META-INF/DEPENDENCIES' on yourProject/app/build.gradle inside android{} . Here it is

packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
}

And then do Clean Project and Rebuild Project.

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
Mara
  • 2,947
  • 2
  • 12
  • 17
109

The solutions here didn't help me, but this link did.

If you have a library that's adding some android .so files –like libassmidi.so or libgnustl_shared.so– you have to tell gradle to pick just one when packaging, otherwise you'll get the conflict.

android {
  packagingOptions {
    pickFirst 'lib/armeabi-v7a/libassmidi.so'
    pickFirst 'lib/x86/libassmidi.so'
  }
}

I was having this issue when using a React Native app as a library in an Android project.

starball
  • 20,030
  • 7
  • 43
  • 238
Daniel Reina
  • 5,764
  • 1
  • 37
  • 50
  • 2
    Thank you, I had the same problem with only one .so file. There is another file called gdbserver in the same folder and after adding the packagingOptions for the library it works now. – Mr. Fish Feb 20 '18 at 07:51
  • 1
    Thank you! Helped when I had a JNA aar as nested dependency – faisal00813 Jun 06 '18 at 16:21
  • thanks! In my case I added such line `pickFirst "**/libarcore_sdk_c.so"`, works well – Sirop4ik Jan 06 '21 at 07:46
48

Basically when gradle puts together the apk file, it copies content from all the compile dependencies, It is intelligent enough to see that there is a duplicate file..coming from two different jar files. This could be any file like a.txt or META-INF/DEPENDENCIES. It might be best to exclude these files using the below, in case the file is present there only for informational purposes.

android{
    packagingOptions {
       exclude 'META-INF/DEPENDENCIES'
    }
}

Or if in case, the file is a mandatory file like a class file, that has been duplicated across two jar dependencies that are related to each other, it is best to find alternatives to these jars, in the way of a more compatible version.

  • When i add this to my project, i get another error which again i am not able to resolve! Any other solution? – hetsgandhi Aug 01 '18 at 05:11
  • In my case this dependency was causing the problem : implementation 'com.google.apis:google-api-services-people:v1-rev20210120-1.31.0' – DragonFire Feb 15 '21 at 10:58
39

I have faced a similar issue working in a multiple modules app environment:

Error: Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. More than one file was found with OS independent path 'META-INF/AL2.0'

This issue was being reported by several of these modules of mine and none of the above solutions were fixing it. Turns out, I was using version Coroutines 1.3.6 which seemed to be embedding META-INF/AL2.0 which was already embedded by another of the libraries I was using. To fix it, I have added the following code snippet to the build.gradle of the module that was failing:

configurations.all {
    resolutionStrategy {
        exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-debug"

    }
}

Given that it was happening on multiple modules, I have moved that resolutionStrategy code to my project level build.gradle. Everything worked after that.

Fred B.
  • 1,631
  • 12
  • 11
37

Had similar message

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. More than one file was found with OS independent path 'constant-values.html'

To resolve it, I had to enable packages view(1) in Android Studio, then browse through the tree to libraries, and locate the duplicates(2)

Then, ctrl+alt+f12 (or RMB menu)(3) - and found libraries which caused the issue. Made list of files inside those libs which caused the issues, and wrote them to app's build.gradle file inside android section. Other option is to deal with the library, containing duplicate files

packagingOptions {
    exclude 'allclasses-frame.html'
    exclude 'allclasses-noframe.html'
    <..>

enter image description here

Pieter
  • 895
  • 11
  • 22
Dmitrii Chichuk
  • 674
  • 7
  • 15
  • 4
    How do you "then browse through the tree to libraries, and locate the duplicates" ? Did you really go over all of the files in all folders? – android developer Jun 17 '19 at 07:23
  • 2
    I'm also confused about "locate the duplicates." There are maybe 100 subentries in app/Libraries... how does one go about locating duplicates? – Freewalker Jun 19 '19 at 17:40
  • In my case, I had to add a wild card to the path in order to exclude the file in the subfolder. e.g "\*\*/file_to_exclude.dll". I also exclude a folder and its children with "folder_content_to_exclude/\*\*" – Pitos Jun 07 '22 at 11:34
35

It's perfectly safe to exclude all meta-info files which are there just for documentation and information purposes:

android{
    packagingOptions {
       exclude 'META-INF/*'
    }
}

Source: https://stackoverflow.com/a/49101232/13093413

Trake Vital
  • 1,019
  • 10
  • 18
29

For Gradle 7.2 and later Add in-app Gradle file

android {
  
   packagingOptions {
       resources.excludes.add("META-INF/*")
   }


}
Yogendra
  • 4,817
  • 1
  • 28
  • 21
26

Add the following in app level gradle file inside android{}

packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/ASL2.0'
    exclude("META-INF/*.kotlin_module") 
} 

     
Thermech
  • 4,371
  • 2
  • 39
  • 60
Mirza Ahmed Baig
  • 5,605
  • 3
  • 22
  • 39
25

This happens when using

org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.0

And is fixed in next version

org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.1

Vairavan
  • 1,246
  • 1
  • 15
  • 19
21

I was having the same problem and I tried this

Error: More than one file was found with OS independent path 'META-INF/proguard/androidx-annotations.pro'

Solution: All you have to do to fix this is to add this to the android { } section in your app's 'build.gradle'

packagingOptions {
    exclude 'META-INF/proguard/androidx-annotations.pro'
}
Mohsen
  • 4,536
  • 2
  • 27
  • 49
Apoorva Jain
  • 475
  • 5
  • 11
18

I has encountered the same error, and I found that it resulted from different modules contained the same classes from different packages.

e.g. One used androidx package, and the other used pre-androidx

I solved it by migrating the pre-androidx module to androidx using built-in feature of Android Studio: "Refactor --> Migrate to Androidx..." without excluding anything.


For your situation, you may check if you have any dependency mismatches among modules.

Owen Chen
  • 1,700
  • 1
  • 11
  • 10
  • 1
    Don't forget to make a **backup** of the current version of your project since migrating to AndroidX may break your project! – Kathir Nov 27 '18 at 08:29
15

I have read all the answers related to getting this message in Android Studio:

More than one file was found with OS independent path 'META-INF/LICENSE'

but in this case excluding classes is no neccessary, we only need to exclude 'META-INF/DEPENDENCIES', this can be done inside the /app/build.gradle:

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

}
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
12

Based on the answers provided by @JohnnyLambada and @Shaaban Ebrahim

For excluding nested files, I used the following.

    packagingOptions {
        resources.excludes.add("META-INF/**/*")
    }

PS: Not spamming with same answer, but just a suggestion if you get an error something like

4 files found with path 'META-INF/gradle/incremental.annotation.processors'.

which catches errors from nested directories and FYI I had setup my dependencies using kotlin-dsl

Fayaz
  • 1,846
  • 2
  • 16
  • 21
10

I my app, I was adding the jar dependencies like this:

implementation files('libs/json-simple-1.1.1.jar')

But I realised that they were already added because of the following first line in dependencies:

implementation fileTree(include: ['*.jar'], dir: 'libs')

This line adds all the jars in lib folder to app dependency.

Hence after removing the extra dependency implementation files('libs/json-simple-1.1.1.jar')

it is working fine.

Sandeep Yohans
  • 929
  • 1
  • 14
  • 36
  • This is helpful but not related to the answer. In the libraries, you find files meta-folders META-INF which can be similar in name and format. These cause duplicate file issue hence must be removed. – Abhinav Saxena Jan 11 '19 at 03:18
6

I updated Studio from Java 7 to Java 8, and this problem occurred. Then I solved it this way:

android {
    defaultConfig {
    }
    buildTypes {
    }
    packagingOptions{
        exclude 'META-INF/rxjava.properties'
    }
}
juzraai
  • 5,693
  • 8
  • 33
  • 47
Manasvi
  • 450
  • 7
  • 9
5

Adding

android.useAndroidX=true

android.enableJetifier=true

to gradle.properties worked for me.

Parag Jain
  • 355
  • 4
  • 5
5

This work for me

packagingOptions {
   exclude 'META-INF/*'
}
Sam
  • 241
  • 3
  • 7
4

This error is caused by adding a support library instead of AndroidX. Make sure you use which one:

for AndroidX:

dependencies {
    def multidex_version = "2.0.1"
    implementation 'androidx.multidex:multidex:$multidex_version'
}

If you aren't using AndroidX:

dependencies {
  implementation 'com.android.support:multidex:1.0.3'
}

Also in manifest use the application class name instead of "android.support.multidex.MultiDexApplication" in the application tag(my application class name is G):

the mistake:

<application
            android:name="android.support.multidex.MultiDexApplication" >
        ...
</application>

right:

<application
            android:name=".G" >
        ...
</application>
M.ghorbani
  • 129
  • 8
4

Since it's not already mentioned, you may alternatively merge the files to stay in accordance with license requirements (or just use pickFirst as stated by Daniel Reina).

packagingOptions {
    merge "META-INF/LICENSE"
    merge "META-INF/AL2.0"
    merge "META-INF/LGPL2.1"
}

Reference: Gradle API 4.2 Packaging Options

AaronC
  • 155
  • 2
  • 9
  • I guess that `merge` is more "legally safe" than `exclude` and `pickFirst` functions. – M.Ed Jul 18 '22 at 08:56
3

I had a similar problem. i was getting the error message -

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.

More than one file was found with OS independent path 'javax/annotation/WillCloseWhenClosed.java'

As stated in some of the answers above, using the below code works

'packagingOptions {
       exclude 'allclasses-frame.html'
    }'

But i was getting this error for 20 different files, so after excluding 20 files, i stopped and tried to find a decent solution. I had also encountered the

'Unable to execute dex: Multiple dex files' error.

I finally managed to solve this.

Firstly (as in my case WillCloseWhenClosed.java was the duplicate file), in android studio you have an option of 'search everywhere'. Write and search for the file there. There i found multiple instances of this file. So i clicked on both instances and saw their location by right clicking on the file and seeing its location when they opened in android studio.

Secondly, I figured out that i had some dependencies in gradle file. I was using the below code

dependencies {
    compile 'com.google.api-client:google-api-client:1.23.0'
}

and also i had their same zip packages in the location :\Users\user\AndroidStudioProjects\git\appname\app\libs\google-http-client-1.23!.

So this was redundant and thats why gradle is finding 2 files. So i deleted my zip packages and it solved the errors for me. Be carefull while doing this. You have to figure out which is the correct file or package to be deleted.

Thirdly, gradle also makes a zip of these bug files in this location (At least for me it made) - C:\Program Files\Android\Android Studio\gradle\m2repository\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar!

so i went and deleted the jsr305-1.3.9.jar file from here also.

If you are still confused, just go the 'search everywhere' in android studio, find instances of your file there and you would have to delete one of them. Take your time and figure out which one.

Vi012
  • 41
  • 2
3

I'm having same problem and I tried this

Error: More than one file was found with OS independent path 'META-INF/library_release.kotlin_module'

Solution:

android {
    packagingOptions {
    exclude 'META-INF/library_release.kotlin_module'
    }
}
Community
  • 1
  • 1
Bhavesh Moradiya
  • 1,323
  • 14
  • 18
3

Worked for me. Add these to your app's build.gradle within the android{} block.

packagingOptions {
    exclude 'META-INF/AL2.0'
    exclude 'META-INF/LGPL2.1'
}
M.Ed
  • 969
  • 10
  • 12
2

put this inside the build.gradle (Module AppName)

android {
   // ....
   packagingOptions{
     pickFirst "androidsupportmultidexversion.txt"
  }
}
Nadeem Iqbal
  • 2,357
  • 1
  • 28
  • 43
2

in gradle 7.2 and above you can fix the problem like this:

in your app/build.gradle in android block:

  packagingOptions {
        resources.excludes.add("META-INF/notice.txt")
        resources.merges.add("META-INF/LICENSE")
        resources.merges.add("META-INF/AL2.0")
        resources.merges.add("META-INF/LGPL2.1")
    }
Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44
2

I faced this issue when I updated Gradle to 7+ I just added inside the /app/build.gradle:

android{
  packagingOptions {
    exclude 'META-INF/AL2.0'
    exclude 'META-INF/LGPL2.1'
}

}

omarezz332
  • 127
  • 1
  • 5
1

I faced this issue, first with some native libraries (.so files) and then with java/kotlin files. Turned out I was including a library from source as well as referencing artifactory through a transitive dependency. Check your dependency tree to see if there are any redundant entries. Use ./gradlew :app:dependencies to get the dependency tree. Replace "app" with your module name if the main module name is different.

ben_joseph
  • 1,660
  • 1
  • 19
  • 24
1

Try to remove multidex from default config and check the build error log. If that log is some relatable with INotification class. Use this in android{}

configurations {
    all*.exclude group: 'com.android.support', module: 'support-v4'
}

This helps me.

Harsh Mittal
  • 449
  • 3
  • 10
0

In many of the answers on SO on this problem it has been suggested to add exclude 'META-INF/DEPENDENCIES' and some other excludes. However none of these worked for me. In my case scenario was like this:

I had added this in dependancies:

implementation 'androidx.annotation:annotation:1.1.0'

And also I had added this in gradle.properties:

android.useAndroidX=true

Both of these I had added, because I was getting build error 'cannot find symbol class Nullable' and it was suggested as solution to this on some of answers like here

However, eventually I landed up in getting error:

 More than one file was found with OS independent path 'androidsupportmultidexversion.txt'

No exclude was working for me. Finally I just removed those added lines above just out of suspecion (Solved 'cannot find symbol class Nullable' in some alternative way) and finally I got rid of this "More than one file was found with OS..." build error. I wasted hours of mine. But finally got rid of it.

Atul
  • 3,778
  • 5
  • 47
  • 87
0

If you have this problem and you have a gradle .jar dependency, like this:

implementation group: 'org.mortbay.jetty', name: 'jetty', version: '6.1.26'

Interval versions until one matches and resolves the excepetion,and apply the best answer of this thread.`

Raksha Saini
  • 604
  • 12
  • 28
0

If you work with multi module project and face this issue while espresso testing, you need to add packagingOptions code each gradle file. In my case , I added below code for each gradle file.

packagingOptions {
    exclude 'META-INF/lib_release.kotlin_module'
}
Enes Zor
  • 973
  • 8
  • 14
0

Add multiDexEnabled true inside android {}

And remove implementation 'com.android.support:multidex:x.x.x' from dependencies

0

I had same problem and simply fixed by changing multidex implementation

From

implementation 'com.android.support:multidex:1.0.3'

To

implementation 'androidx.multidex:multidex:2.0.1'
Radesh
  • 13,084
  • 4
  • 51
  • 64
0

This is another syntax for Kotlin DSL (build.gradle.kts):

android {
    packagingOptions {
        resources {
            excludes += "META-INF/*"
            excludes += "anotherFile"
            excludes += "andSoOn..."
        }
    }
    // ...
}
Mahozad
  • 18,032
  • 13
  • 118
  • 133
-1

In my case code bellow worked, In your

app build.gradle

file add

android {    
    ..
    packagingOptions {
        pickFirst  '**'
    }
    ..
}