Cyrus
Cyrus

Reputation: 9415

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

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?

Upvotes: 630

Views: 549402

Answers (30)

Nitesh Singh
Nitesh Singh

Reputation: 11

android {

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

}

Upvotes: 0

Mahozad
Mahozad

Reputation: 24472

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

android {
    packagingOptions {
        resources {
            excludes += "META-INF/*"
            excludes += "anotherFile"
            excludes += "andSoOn..."
        }
    }
    // ...
}

Note that with newer versions of AGP (Android Gradle Plugin), the exclude function is deprecated and replaced with resources.excludes or resources { excludes...

Upvotes: 2

kangear
kangear

Reputation: 2603

for me, just add this line:

android.enableJetifier=true

Upvotes: 0

Tasin Haiyan
Tasin Haiyan

Reputation: 368

packagingOptions is deprecated. Use packaging instead. Below is the code for the Kotlin DSL (build.gradle.kts):

android {
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
            excludes += "/META-INF/DEPENDENCIES"
            excludes += "/META-INF/LICENSE"
            excludes += "/META-INF/LICENSE.txt"
            excludes += "/META-INF/license.txt"
            excludes += "/META-INF/NOTICE"
            excludes += "/META-INF/NOTICE.txt"
            excludes += "/META-INF/notice.txt"
            excludes += "/META-INF/ASL2.0"
            excludes += "/META-INF/*.kotlin_module"
        }
    }
}

Upvotes: 1

RajeshKumar_
RajeshKumar_

Reputation: 151

If you are using multi modular architecture, then try to add this in gradle files of all modules

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

Upvotes: 0

For Kotlin DSL in Kotlin Multiplatform go to build.gradle.kts (:androidApp)

android {
    packagingOptions.resources.excludes.add("paste-the-path-here")
}

Upvotes: 0

omarezz332
omarezz332

Reputation: 157

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

}

Upvotes: 2

Shaaban Ebrahim
Shaaban Ebrahim

Reputation: 10412

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

Upvotes: 935

Radesh
Radesh

Reputation: 13555

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'

Upvotes: 0

Daniel Reina
Daniel Reina

Reputation: 6327

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.

Upvotes: 111

Tanvir Rahman Ovi
Tanvir Rahman Ovi

Reputation: 13

In my case code bellow worked, In your

app build.gradle

file add

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

Upvotes: -1

Yogendra
Yogendra

Reputation: 5260

For Gradle 7.2 and later Add in-app Gradle file

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


}

Upvotes: 29

M.Ed
M.Ed

Reputation: 1328

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

Upvotes: 3

Abdul Subhan
Abdul Subhan

Reputation: 31

put this inside the build.gradle (Module AppName)

android {
   // ....
   packagingOptions{
     pickFirst "androidsupportmultidexversion.txt"
  }
}

Upvotes: 2

Sana Ebadi
Sana Ebadi

Reputation: 7210

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

Upvotes: 2

Fayaz
Fayaz

Reputation: 2114

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

Upvotes: 13

Sam
Sam

Reputation: 261

This work for me

packagingOptions {
   exclude 'META-INF/*'
}

Upvotes: 5

AaronC
AaronC

Reputation: 314

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

Upvotes: 4

Ibrahim AbdelGawad
Ibrahim AbdelGawad

Reputation: 553

Add multiDexEnabled true inside android {}

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

Upvotes: 0

Trake Vital
Trake Vital

Reputation: 1167

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

Upvotes: 38

Enes Zor
Enes Zor

Reputation: 1180

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

Upvotes: 0

Santiago Gonzalez
Santiago Gonzalez

Reputation: 31

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.`

Upvotes: 0

Mirza Ahmed Baig
Mirza Ahmed Baig

Reputation: 5865

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

     

Upvotes: 26

M.ghorbani
M.ghorbani

Reputation: 149

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>

Upvotes: 4

Fred B.
Fred B.

Reputation: 1721

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.

Upvotes: 40

Atul
Atul

Reputation: 4320

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.

Upvotes: 0

Bhavesh Moradiya
Bhavesh Moradiya

Reputation: 1373

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

Upvotes: 3

Mara
Mara

Reputation: 3077

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.

Upvotes: 222

Parag Jain
Parag Jain

Reputation: 355

Adding

android.useAndroidX=true

android.enableJetifier=true

to gradle.properties worked for me.

Upvotes: 5

Jorgesys
Jorgesys

Reputation: 126445

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

}

Upvotes: 15

Related Questions