Duplicate files copied in APK META-INF/library_release.kotlin_module - android

I recently added two Android libraries through JitPack and I have the following error:
Duplicate files copied in APK META-INF/library_release.kotlin_module
I've cleared the cached and I've tried excluding the module using
exclude group: 'org.jetbrains'
and
exclude group: 'org.jetbrains.kotlin'
but neither seems to resolve the issue. Is there any way to stop the kotlin stdlib from being added through JitPack? Oddly, other libraries like DbFlow don't have this issue, though I don't see anything special about their setup (other than that it isn't through JitPack)

As suggested in the post Kotlin M13 is out! by jetbrains:
Make sure these .kotlin_module files are not stripped by your packaging process.
So, we can't use exclude option to exclude this resource file from being generated.
As descripted in Kotlin M13 is out!, we should:
in Maven we use groupId and artifactId for module names, but you can say
<configuration>
<moduleName>com.example.mymodule</moduleName>
</configuration>
in Gradle it’s project name + build task name, to customize:
compileKotlin {
kotlinOptions.moduleName = "com.example.mymodule"
}
This is my configuration for Android library project:
ext {
GROUP_ID = 'custom.group.id'
ARTIFACT_ID = 'artifactid'
}
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
compileOptions {
kotlinOptions.freeCompilerArgs += ['-module-name', "$GROUP_ID.$ARTIFACT_ID"]
}
defaultConfig {
...
}
buildTypes {
...
}
}
Resource file named META-INF/custom.group.id.artifactId.kotlin_module will be generated instead of META-INF/library_release.kotlin_module.No more duplicate files will be found.
You can read this post and this post for more information.

You should add this to the build.gradle file of your app inside the android tag
packagingOptions {
exclude 'META-INF/library_release.kotlin_module'
}

After looking at other conflicts, it seems like the resolution is
packagingOptions {
pickFirst 'META-INF/library_release.kotlin_module'
}
under android in the app gradle.
This allows the apk to build

Related

How to solve Duplicate class java.lang.RuntimeException: Duplicate class com.google.zxing.client

I am currently facing a Duplicate class RuntimeException. In our current use case we are producing an artifact which is using com.journeyapps:zxing-android-embedded. Our client wants to include our artifact but they are getting a RuntimeException during the compilation.
The Exception occurs because they are using com.google.zxing which is currently clashing with the journeyapps port of zxing which we are using. Journeyapps did a port of the ZXing Android application as an Android library project, for embedding in an Android application.
Caused by: java.util.concurrent.ExecutionException: java.lang.RuntimeException: Duplicate class com.google.zxing.client.android.camera.CameraConfigurationUtils found in modules android-core-3.3.0.jar (com.google.zxing:android-core:3.3.0) and classes.jar (com.journeyapps:zxing-android-embedded:3.6.0)
Go to the documentation to learn how to Fix dependency resolution errors.
at com.android.ide.common.workers.ExecutorServiceAdapter.await(ExecutorServiceAdapter.kt:56)
... 71 more
Caused by: java.lang.RuntimeException: Duplicate class com.google.zxing.client.android.camera.CameraConfigurationUtils found in modules android-core-3.3.0.jar (com.google.zxing:android-core:3.3.0) and classes.jar (com.journeyapps:zxing-android-embedded:3.6.0)
Go to the documentation to learn how to Fix dependency resolution errors.
at com.android.build.gradle.internal.tasks.CheckDuplicatesRunnable.run(CheckDuplicateClassesDelegate.kt:132)
at com.android.ide.common.workers.ExecutorServiceAdapter$submit$submission$1.run(ExecutorServiceAdapter.kt:40)
... 72 more
I have tried to exclude the duplicate classes inside the gradle build file. Using the following code and vise versa.
implementation("com.google.zxing:android-core:3.3.0")
implementation("module which includes com.journeyapps:zxing-android-embedded:3.6.6") {
exclude group: 'com.google.zxing', module: 'android-core'
}
Without a solution.
I even tried to exclude the module inside the configuration. If I do so it is missing during the insertion of the dependencies.
You can reproduce the error using the following code
implementation("com.google.zxing:android-core:3.3.0")
implementation("com.journeyapps:zxing-android-embedded:3.6.0")
As I tried to solve the problem for a lot of hours without a solution, I am very thankful for every advise.
this solve for me
add below code in Android/app/build.gradle file
configurations {
compile.exclude group: 'com.google.zxing'
}
try using this transitive flag maybe this can help you out
implementation("module which includes com.journeyapps:zxing-android-embedded:3.6.6") {
exclude group: 'com.google.zxing', module: 'android-core'
transitive = false
}
We came to the conclusion that we can not exclude the dependencies because their is a naming issue in case of some classes with com.journeyapps:zxing and com.google.zxing.
We ended up migrating our libary from com.journeyapps:zxing to com.google.zxing.
Thanks to PJain for the advise.
Thanks to your comments I solved the issue like below image
dependencies {
configurations.all {
exclude group: 'com.google.zxing'
}
}
android {
compileSdkVersion 33
defaultConfig {
minSdkVersion 17
generatedDensities = []
multiDexEnabled true
versionCode 1
versionName "1.0.0"
}
aaptOptions {
additionalParameters "--no-version-vectors"
}
}

Using different versions of same library - More than one file was found with OS independent path 'builddef.lst'

The problem
i am trying to trim and compress a video using two libraries: Silicompressor and k4l-video-trimmer.
the problem is both libraries use mp4parser but different versions of it.
k4l-video-trimmer is using version 1.1.20 as a dependency in build.gradle and Silicompressor is using version 1.0.6 as a jar file.
now the project builds fine but when i try to run it, i get the error:
More than one file was found with OS independent path 'builddef.lst'
The things i have already tried
Exclude builddef.lst in packaging options
i added these lines in app level build.gradle:
android {
packagingOptions {
exclude 'builddef.lst'
exclude 'version.txt'
exclude 'isoparser-default.properties'
}
}
and then i got another error which i was unable to solve:
Program type already present: com.coremedia.iso.BoxParser
Message{kind=ERROR, text=Program type already present:
com.coremedia.iso.BoxParser, sources=[Unknown source file], tool
name=Optional.of(D8)}
Exclude mp4parser from k4l-video-trimmer
i used
implementation ('life.knowledge4:k4l-video-trimmer:1.0') {
exclude group: 'com.googlecode.mp4parser'
}
instead of
implementation 'life.knowledge4:k4l-video-trimmer:1.0'
and now the project runs and crashes when i try to trim a video.
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/googlecode/mp4parser/FileDataSourceViaHeapImpl;
at life.knowledge4.videotrimmer.utils.TrimVideoUtils.genVideoUsingMp4Parser(TrimVideoUtils.java:72)
at life.knowledge4.videotrimmer.utils.TrimVideoUtils.startTrim(TrimVideoUtils.java:65)
at life.knowledge4.videotrimmer.K4LVideoTrimmer$5.execute(K4LVideoTrimmer.java:354)
at life.knowledge4.videotrimmer.utils.BackgroundExecutor$Task.run(BackgroundExecutor.java:212)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Exclude mp4parser from Silicompressor
i imported Silicompressor's module instead of using dependency line and removed mp4parser's jar file to force the library to use the version 1.1.20. now app runs again but the compressed video has no audio!
Use both versions of mp4parser
i used jarjar to repackage the jar file Silicompressor was using from com.googlecode.mp4parser to org.repackage.com.googlecode.mp4parser. still having the very same issue.
Build.gradle
here's my app level build.gradle file:
apply plugin: 'com.android.application'
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
}
android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
defaultConfig {
applicationId "---"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
repositories {
google()
jcenter({url "http://jcenter.bintray.com/"})
mavenLocal()
jcenter()
maven { url "http://jzaccone.github.io/SlidingMenu-aar" }
maven { url "https://jitpack.io" }
maven { url 'https://maven.google.com' }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}
/*packagingOptions {
exclude 'builddef.lst'
exclude 'version.txt'
exclude 'isoparser-default.properties'
}*/
}
dependencies {
...
implementation ('life.knowledge4:k4l-video-trimmer:1.0') {
exclude group: 'com.googlecode.mp4parser'
}
implementation 'com.iceteck.silicompressorr:silicompressor:2.1'
}
and THANK YOU FOR YOUR TIME!
With the help of #MilanPansuriya I was finally able to solve the issue. Follow below steps to resolve above issue:-
Create a new library combining both libraries (kl-trimmer and Silicompressor). In my case it was (kl-trimmer and VideoCompressor (https://github.com/fishwjy/VideoCompressor/)).
Use isoparser-1.0.6.jar in your library.
Now after building your new library you will get an error in class TrimVideoUtils of library kl-trimmer.
Error will be for FileDataSourceViaHeapImpl not found. Actually particular class is available in higher version of isoparser. Change it to FileDataSourceImpl.
Now library will build just fine in your project.
Note:- Both libraries will work fine now, but since we have changed FileDataSourceViaHeapImpl to FileDataSourceImpl sometimes I am getting Out Of Memory while trimming some large size videos (which is obviously supposed to happen & is also reported by users of older version of kl-trimmer).
As of now this is the only solution I am able to get. If someone have a better solution please update us too. Thanks.
I was able to resolve this problem by doing just what the above answer says (combining both libraries) but in my case, i'm using SiliCompressor and
AndroidDeveloperLB/VideoTrimmer, and i also created a library just for anyone who comes across this issue and wants a quick fix :) https://github.com/tobioyelekan/VideoTrimmerCompressor

More than one file was found with OS independent path 'lib/x86/libusb.so'

I am using libusb in my android application. When I am trying to build libusb native library then I get below error message, *.so files generated.
Error:Execution failed for task ':app:transformNativeLibsWithMergeJniLibsForDebug'.
More than one file was found with OS independent path 'lib/x86/libusb.so'
build.gradle
import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.williams.libusbpoc"
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
ndkBuild {
path 'src/main/jni/Android.mk'
}
}
sourceSets.main {
jniLibs.srcDir 'src/main/libs'
jni.srcDirs = [] //disable automatic ndk-build call
}
// call regular ndk-build(.cmd) script from app directory
task ndkBuild(type: Exec) {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
} else {
commandLine 'ndk-build', '-C', file('src/main').absolutePath
}
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation ('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.0.0-beta2'
testImplementation 'junit:junit:4.12'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
compile "org.jetbrains.anko:anko-appcompat-v7-commons:$anko_version"
}
I am on windows machine. Does anyone know what could be the issue ?
I was having this issue in my React-Native Bridge project after I added AAR files of 3rd party SDK. And I was linking the Bridge into my Main React-native application.
Solution (May differ for you):
Add this in app/build.gradle the Main React-Native application:
android {
// ...
packagingOptions {
pickFirst '**/*.so'
}
}
Test the Build on React-Native Bridge project after adding the AAR libraries.
Clean the React-Native Bridge project
Clean the React-Native application project
Remove node_modules and re-install the bridge package into the project.
Run the application.
I faced another issue related to this (If you include AAR into library project that's not being linked to main application)
https://stackoverflow.com/a/58588503/3197778
I removed jniLibs.srcDir 'src/main/libs' code inside sourceSets.main block. It was creating *.so files twice.
sourceSets.main {
jniLibs.srcDir 'src/main/libs'
jni.srcDirs = [] //disable automatic ndk-build call
}
you can use like this:
add the following code into build.gradle ,
packagingOptions {
pickFirst 'lib/armeabi-v7a/your_name.so'
pickFirst 'lib/arm64-v8a/your_name.so'
pickFirst 'lib/x86/your_name.so'
pickFirst 'lib/x86_64/your_name.so'
}
this pickFirst that means : if more than one path matches the first-pick, only the first found will be selected. please click Get more information
In case of react-native
add android/app/build.gradle file into andorid : {.....} section this :
packagingOptions {
pickFirst 'lib/x86/libc++_shared.so'
pickFirst 'lib/x86_64/libjsc.so'
pickFirst 'lib/arm64-v8a/libjsc.so'
pickFirst 'lib/arm64-v8a/libc++_shared.so'
pickFirst 'lib/x86_64/libc++_shared.so'
pickFirst 'lib/armeabi-v7a/libc++_shared.so'
}
then use "gradlew clean"
I've seen a similar error running my app after migration to Android Studio 3.0. A build clean solved the issue.
in my case adding these fixed the issue to build.gradle app level module
packagingOptions {
pickFirst 'lib/armeabi-v7a/libblasV8.so'
pickFirst 'lib/mips/librsjni.so'
pickFirst 'lib/x86/libblasV8.so'
pickFirst 'lib/mips/libRSSupport.so'
pickFirst 'lib/mips/libblasV8.so'
}
but in your case library names could be different so replace them with your library names
I was seeing this error after adding react-native-pdf to my project. FAHID's solution worked for me.
Note that in later versions of Gradle, pickFirst() is deprecated in favor of resources.pickFirsts() or jniLibs.pickFirsts.add().
Manually clean the project. Perform the following in the project directory:
rm -rf .idea .gradle */build */.cxx
rm -rf ~/.gradle
This will clean up all intermediate files and also clear the gradle cache.
I just want to add to Nonos answer that I think I received this issue after running ndk-build within my app/jni directory, and then running ./gradlew installDebug from the top-level directory of my Android NDK project. So doing an ndk-build clean in app/jni before doing another Gradle build did indeed solve the problem.
N Sharma solutions is good.
But in case your conflict is with an external module that you added as a library in your project and you are not able to edit its code as it is readonly you can follow below steps:
I was facing same issue with ffmpeg library after merging two Android projects as one project.
Actually issue was arriving due to two different versions of ffmpeg library but they were loaded with same names in memory. One library was placed in JNiLibs while other was inside another library used as module. I was not able to modify the code of module as it was readonly so I renamed the one used in my own code to ffmpegCamera and loaded it in memory with same name.
System.loadLibrary("ffmpegCamera");
This resolved the issue and now both versions of libraries are loading well as separate name and process id in memory.
faced this issue in react-native "react-native": "0.64.2"
facing this issue because of recent react native updates, old builds are failing
i have done the fallowing to run the build successfully & this solution works after most of the issue caused in recent update.
in android/build.gradle (not the project level) change the followings
add kotlin version in ext of buildScripts {... kotlin_version = '1.6.10'}
in dependecies add the classpath
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
add this line in between ending of buildscript and beginning of allprojects
add this line in the first beginning of all projects
configurations.all {
resolutionStrategy {
force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
}
}
If you are using react native version below 0.70.5 then you should implement this,
Add this code in android/build.gradle file
repositories {
exclusiveContent {
// We get React Native's Android binaries exclusively through npm,
// from a local Maven repo inside node_modules/react-native/.
// (The use of exclusiveContent prevents looking elsewhere like Maven Central
// and potentially getting a wrong version.)
filter {
includeGroup "com.facebook.react"
}
forRepository {
maven {
url "$rootDir/../node_modules/react-native/android"
}
}
}
// ...
}
}
It worked for me.
If Your gradle version 6.1 or below, you must use a different workaround as gradle 6.1 does not support exclusiveContent. You can use this,
def REACT_NATIVE_VERSION = new File(['node', '--print',"JSON.parse(require('fs').readFileSync(require.resolve('react-native/package.json'), 'utf-8')).version"].execute(null, rootDir).text.trim())
allprojects {
configurations.all {
resolutionStrategy {
// Remove this override in 0.65+, as a proper fix is included in react-native itself.
force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
}
}

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?
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/*")
}
}
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.
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.
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.
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.
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'
<..>
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
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")
}
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
For Gradle 7.2 and later
Add in-app Gradle file
android {
packagingOptions {
resources.excludes.add("META-INF/*")
}
}
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'
}
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.
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'
}
}
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.
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
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'
}
}
This work for me
packagingOptions {
exclude 'META-INF/*'
}
Adding
android.useAndroidX=true
android.enableJetifier=true
to gradle.properties worked for me.
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>
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
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.
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'
}
}
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'
}
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")
}
put this inside the build.gradle (Module AppName)
android {
// ....
packagingOptions{
pickFirst "androidsupportmultidexversion.txt"
}
}
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.
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.
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.
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.`
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'
}

Can you exclude classes with packagingOptions in android gradle?

I'm trying to figure out if it's possible to exclude class files or use pickfirst for classes in a jar file dependency. We always seem to run into issues where third party libs package up jars in aar files instead of using gradle dependencies and therefore cause duplicate file zip exceptions:
like this:
com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/apache/commons/codec/StringEncoderComparator.class
The only solution we've found so far is to unzip the aar, delete the offending jar, and rezip it back up. Is there a way to just exclude the jar or class from gradle?
Looking at the source it seems like I should be able to use packaging options. So I've tried various combinations of pick first and exclude but no luck:
packagingOptions {
pickFirst '**/StringEncoderComparator.class'
pickFirst 'org/apache/commons/codec/StringEncoderComparator.class'
pickFirst 'org/apache/commons/codec/*'
}
Use resolutionStrategy.
https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html
apply plugin: 'java' //so that there are some configurations
configurations.all {
resolutionStrategy {
// fail eagerly on version conflict (includes transitive dependencies)
// e.g. multiple different versions of the same dependency (group and name are equal)
failOnVersionConflict()
// prefer modules that are part of this build (multi-project or composite build) over external modules
preferProjectModules()
// force certain versions of dependencies (including transitive)
// *append new forced modules:
force 'asm:asm-all:3.3.1', 'commons-io:commons-io:1.4'
// *replace existing forced modules with new ones:
forcedModules = ['asm:asm-all:3.3.1']
// add dependency substitution rules
dependencySubstitution {
substitute module('org.gradle:api') with project(':api')
substitute project(':util') with module('org.gradle:util:3.0')
}
// cache dynamic versions for 10 minutes
cacheDynamicVersionsFor 10*60, 'seconds'
// don't cache changing modules at all
cacheChangingModulesFor 0, 'seconds'
}
}

Categories

Resources