Gradle puts duplicate files in the apk file - android

I'm using gradle 1.10 and the version of the android plugin is 0.8.0. My android projects needs these two jars: jackson-core-asl-1.9.11.jar and jackson-mapper-asl-1.9.11.jar. I add the files thus:
dependencies {
compile files('libs/jackson-core-asl-1.9.11.jar')
compile files('libs/jackson-mapper-asl-1.9.11.jar')
}
During gradle build I get an error message saying that these two files are duplicated in META-INF/ASL2.0. I solved the problem by excluding the following files:
packagingOptions {
exclude 'META-INF/ASL2.0'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
}
I have to exclude all of them because there apparently is a duplicate file in all of them.
I'd like to know why this problem occurs. Is it a bug of the android plugin or the gradle itself? Can excluding the above files cause any problems? Am I just excluding the above mentioned jars or is there anything else in those META-INF files? I don't want to exclude anything my project needs

First this is not a bug of gradle .
It occurs in MergeJavaResourcesTransform task:
As we know ,APK is just a zip file, so when put META-INFO/xxx into zip file, if file has been added before ,we can not put it again.
And there is no merge rules for META-INFO files , so we can only add on file which names NOTICE etc

Here's some info from the Jackson team https://github.com/FasterXML/jackson-databind/issues/214
There are a variety of posts about this on SO, such as this one: Error generating final archive: Found duplicate file for APK: LICENSE.txt
I've used Jackson with Eclipse and Ant as well (and I'm currently using it in Studio with the same workaround you are using), and I had to use zip on the command to rename the license.txt file. There's more info about the whole thing (including an example of using zip to rename the file) in this post Android Gradle plugin 0.7.0: "duplicate files during packaging of APK".
I have not found anything indicating that renaming or excluding these files will have negative consequences, and I never experienced any problems in about 2 years of using Jackson with Eclipse and Ant.

Related

META-INF/version duplicate error when using Proguard

Gradle : 4.10.1
Gradle Android Plugin version : 3.3.2
Proguard : 6.0.3
JDK - 1.9
Android Studio 3.3.2
When I try to build apk release version along with Proguard. I get the following error -
Caused by: java.io.IOException: Please correct the above warnings first.
at proguard.InputReader.execute(InputReader.java:149)
at proguard.ProGuard.readInput(ProGuard.java:255)
at proguard.ProGuard.execute(ProGuard.java:96)
......
This seems to be caused due to this -
Warning: class [META-INF/versions/9/module-info.class] unexpectedly contains class [module-info]
Note: duplicate definition of program class [module-info]
Note: there were 20 duplicate class definitions.
(http://proguard.sourceforge.net/manual/troubleshooting.html#duplicateclass)
Warning: there were 21 classes in incorrectly named files.
You should make sure all file names correspond to their class names.
The directory hierarchies must correspond to the package hierarchies.
From extensive searching it looks like Proguard has a problem with META-INF/versions/9. I have multiple dependencies that contain this.
While the issue to seems to somewhat documented, no solutions prescribed seem to work.
https://sourceforge.net/p/proguard/bugs/665/
suggests filtering out those class files via -
-injars my_lib.jar(!META-INF/versions/**.class)
However when I try this it just labels more files as duplicate and incorrectly named. I also tried excluding it via gradle-
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/DEPENDENCIES.txt'
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/INDEX.LIST'
exclude 'META-INF/versions'
exclude 'META-INF/versions/9/module-info.class'
}
This also fails to resolve the problem. How do I solve this problem ?
I realise this is a very old question, but I was able to get this to work using this gradle configuration:
task obfuscate(type: proguard.gradle.ProGuardTask) {
configuration files("proguard-project.txt")
libraryjars files("build/rt.jar", "build/jce.jar")
injars files("build/libs/desktop-${version}.jar"), filter: "!META-INF/versions/**/*.class"
outjars files("build/libs/obfuscated.jar")
}
I think the issue with the injars directive you used might be the path - should be META-INF/versions/**/*.class.
Using the option -ignorewarnings in proguard-project.txt 'fixed' it for me, the generated jar works fine as long as the only warnings mentioned by Proguard are related to META-INF.
I realise this is an old question, but I had issues with this when upgrading some other libs in an application.
I found the <inLibsFilter> tag at the proguard maven plugin page which helped me get it to work. I used it like this (in my <configuration> tag:
<inLibsFilter>!META-INF/versions/**</inLibsFilter>
Hopefully it can help others that may stumble upon it..

Data error while building apk in android

I am trying to generate unsigned APK it shows error.
Cannot copy 'E:---------------\app\libs\apache-mime4j-0.6.jar' to 'C:\Users--------.AndroidStudio2.2\system\jars\apache-mime4j-0.6.jar'. Reason: Data error (cyclic redundancy check).
i tried adding in code in default config
multiDexEnabled true
and also
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
}
is it necessary toi do disk check ?
any help will be really appreciated
CRC error would indicate data corruption. Try replacing the lib with a fresh copy.
Btw, seeing that it is copying the lib from E: drive to generate the Apk, why don't you try adding the dependency from Maven Central from Project Structure(assuming you are using Android Studio). Adding it this way will auto manage any issues as it will be stored in with the other libs.
After trying a few solutions off the internet I decided to try deleting the .gradle folder and that worked for me
I also face this problem. Just given the same folder for .jks and build output file. Then this problem will be solved.

Duplicate files copied in APK META-INF/LICENSE - Cordova Couchabse-Lite Plugin

When I add the couchbase lite cordova plugin from:
https://github.com/couchbaselabs/Couchbase-Lite-PhoneGap-Plugin
I get this error when building:
Duplicate files copied in APK META-INF/LICENSE
File 1: C:\JavaScript\Project-Angular\platforms\android\libs\jackson-databind-2.5.0.jar
File 2: C:\JavaScript\Project-Angular\platforms\android\libs\jackson-databind-2.5.0.jar
You can ignore those files in your build.gradle:
android {
packagingOptions {
exclude 'META-INF/LICENSE'
}
}
I know it has something to do with the build.gradle file but I've searched everywhere and all the questions are about Android directly. I don't know how I'm supposed to edit the build.gradle file since its edited.
Removing the plugin fixes the problem
To fix this problem, you must create a file called platforms/android/build-extras.gradle within your project. With that file created, you can paste the following code:
android {
packagingOptions {
exclude 'META-INF/ASL2.0'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
}
The next time you try to build and run your project for Android, you should no longer suffer from that problem.
Further documentation on Apache Cordova's build-extras.gradle file can be found in the official documentation:
https://cordova.apache.org/docs/en/5.0.0/guide/platforms/android/tools.html
Best,

Restlet Error:duplicate files during packaging of APK in androidstudio

I added all the jars from restlet-android-2.3.1 archive to my libs folder in android studio. I can Make the project, but when I try to debug I'm getting this errors
Path in archive: META-INF/services/org.restlet.engine.converter.ConverterHelper
Origin 1: D:\android\restlet\app\libs\org.restlet.ext.gson.jar
Origin 2: D:\android\restlet\app\libs\org.restlet.ext.html.jar
You can ignore those files in your build.gradle:
android {
packagingOptions {
exclude 'META-INF/services/org.restlet.engine.converter.ConverterHelper'
}
}
AND
Error:Execution failed for task ':app:packageDebug'.
> Duplicate files copied in APK META-INF/services/org.restlet.engine.converter.ConverterHelper
File 1: D:\android\restlet\app\libs\org.restlet.ext.gson.jar
File 2: D:\android\restlet\app\libs\org.restlet.ext.gson.jar
If, for example to:
packagingOptions {
exclude 'META-INF/services/org.restlet.engine.converter.ConverterHelper'
}
then I'm getting this:
Error:duplicate files during packaging of APK D:\android\restlet\app\build\outputs\apk\app-debug-unaligned.apk
Path in archive: META-INF/services/org.restlet.engine.ClientHelper
Origin 1: D:\android\restlet\app\libs\org.restlet.ext.sip.jar
Origin 2: D:\android\restlet\app\libs\org.restlet.ext.nio.jar
You can ignore those files in your build.gradle:
android {
packagingOptions {
exclude 'META-INF/services/org.restlet.engine.ClientHelper'
}
}
And so on.
The classes I'm adding to exclude are base classes, and are referenced in other jars.
If to remove problematic jars from libs folder then it will work, but I ended up cutting off half of them. I'll need them. How to fix this? Are all of this restlet jars play well together?
I was having the same issue. I'm just using four of the restlet jars in my project and had to put in the following to the build.gradle file. You would think there's some studio wide setting like "ignore duplicates" but I didn't find it. I eventually added enough exclusions for it to work.
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
exclude 'META-INF/services/com.fasterxml.jackson.core.JsonFactory'
exclude 'META-INF/services/com.fasterxml.jackson.core.ObjectCodec'
}

Deleting Jar libraries from Project

I created a folder named libs and added a .jar file to it and right click -> add as library.
Later when I wanted to remove that jar file I just did right click -> delete. But noticed it is still in the build.gradle's dependencies block, removed that line as well.
Should have done it in a different way?
I later added the same jar file back. Now there are no errors in the project, but when I try to run the app, I get 2 errors:
Error:duplicate files during packaging of APK C:\Users\UserName\projectname\appname\build\outputs\apk\appname-debug-unaligned.apk Path in archive: META-INF/LICENSE
Origin 1: C:\Users\UserName\projectname\appname\libs\jackson-core-asl-1.9.13.jar
Origin 2: C:\Users\UserName\projectname\appname\libs\jackson-mapper-asl-1.9.13.jar
You can ignore those files in your build.gradle: android { packagingOptions { exclude 'META-INF/LICENSE' } }
-----------
Error:Execution failed for task ':appname:packageDebug'.
Duplicate files copied in APK META-INF/LICENSE
File 1: C:\Users\UserName\projectname\appname\libs\jackson-core-asl-1.9.13.jar
File 2: C:\Users\UserName\projectname\appname\libs\jackson-core-asl-1.9.13.jar
I tried the
You can ignore those files in your build.gradle: android {
packagingOptions { exclude 'META-INF/LICENSE' } }
But nothing changed.
The error message tells you to exclude 'META-INF/LICENSE' but instead you have exclude 'META-INF/ASL2.0'. Change it to the statement it's recommending to you.

Categories

Resources