Creating zip of android project along with its dependencies through gradle - android

I have an android project which have three different dependencies (library projects) and I want to create a zip of each project src folder after we are finished with the builds. I did the following,
task srcZip(type: Zip) {
from projectDir
println "SOURCE THE ZIP"
exclude 'build'
exclude 'gen'
exclude 'bin'
}
In order to make a zip however I have to call this task manually. This works only if I have to create a zip file of one project which does not include the sources of other dependencies (library projects). How can I create a zip of all the sources at once?
Thanks in advance.

So I was able to fix the problem on my own,
Copying the following to all the dependent projects,
task srcZip(type: Zip) {
from projectDir
println "SOURCE THE ZIP"
exclude 'build'
exclude 'gen'
exclude 'bin'
}

Related

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.

Adding .so files to jniLibs in Android Studio: duplicate files during packaging of APK

Following various sets of instructions, it seems that I should add native libraries to app/src/main/jniLibs and they'll be linked up automagically. However when I do this (for OpenCV) I get the following:
Error:duplicate files during packaging of APK /.../app/build/outputs/apk/app-debug-unaligned.apk
Path in archive: lib/armeabi/libopencv_java.so
Origin 1: /.../app/src/main/jniLibs/armeabi/libopencv_java.so
Origin 2: /.../build/intermediates/exploded-aar/AppName/.libraries/opencv/unspecified/jni/armeabi/libopencv_java.so
You can ignore those files in your build.gradle:
android {
packagingOptions {
exclude 'lib/armeabi/libopencv_java.so'
}
}
Error:Execution failed for task ':app:packageDebug'.
> Duplicate files copied in APK lib/armeabi/libopencv_java.so
File 1: /.../app/src/main/jniLibs/armeabi/libopencv_java.so
File 2: /.../app/src/main/jniLibs/armeabi/libopencv_java.so
Adding the exclude section doesn't work. There's only one copy of the file, but somehow it's being referenced twice, and it seems the second (build) include is something auto-generated. What can I do?
In case of duplicate libraries (*.so) files, exclude option will not help as we cannot completely exclude the native binaries. There is one more option in packagingOptions. It is 'pickFirst'. We can avoid duplicate files error and include the first one the compiler encounters.
packagingOptions {
pickFirst 'lib/armeabi/libopencv_java.so'
}
ndk-build was generating duplicates of all of the packages from OpenCV. I removed all of the .so libraries from my app, apart from my actual app library, and then it packaged up fine.
I also had this issue. It seems it's because the documentation and various conversation threads on setting up OpenCV for Android say to put the JNI libraries at the path /src/main/jniLibs or /src/main/libs. The problem is that the /src/ folder is for source code, not libraries. The solution is to move the /main/ folder out of /src/ and into the project root, which is how Google documents this project structure: https://developer.android.com/tools/projects/index.html
I also have the following settings in build.gradle:
android {
sourceSets {
main {
jni.srcDirs = [] //disable automatic ndk-build call
jniLibs.srcDir 'main/libs'
}
}
}

How to Exclude Duplicate C Shared Libraries (.so) in a Multi-Project Android Build?

I get a "duplicate files" conflict when building a parent project with two library modules, which make use of the same libc++_shared.so shared library.
(NOTE: Please do not consider this a "duplicate question". I have read several related posts, which have helped me get this far. However, no posts have provided an answer that works in my case involving NDK artifacts.)
The build was working correctly when I only had 1 such library module. The addition of the second library module is now creating the conflict.
Consider the following project structure: 1 parent project, 2 "child" projects - but each project is located at the same directory level (i.e. Not nested hierarchically)
ProjectA/ (Parent)
LibraryModuleA1/
build/exploded-aar/com.package.name/
LibraryModuleB1/<version>/jni/armeabi-v7a/libc++_shared.so
LibraryModuleC1/<version>/jni/armeabi-v7a/libc++_shared.so
build.gradle (bgA1)
Test_APK_Module A1T/
build.gradle (bgA1T)
build.gradle (bgPA)
ProjectB/
LibraryModuleB1/ (Uses NDK)
build/lib/armeabi-v7a/libc++_shared.so
build.gradle (bgB1)
build.gradle (bgPB)
ProjectC/
LibraryModuleC1/ (Uses NDK)
build/lib/armeabi-v7a/libc++_shared.so
build.gradle (bgC1)
build.gradle (bgPC)
Library Module A1 depends on both Library Modules B1 & C1.
A1 -> B1
A1 -> C1
Projects B and C both have NDK-based code and build/test correctly. Both depend on the libc++_shared.so shared library.
However, when building Project A, I get the following error during the :LibraryModuleA1:packageDebugTest task:
Error: duplicate files during packaging of APK /ProjectA/LibraryModuleA1/build/apk/LibraryModuleA1-debug-test-unaligned.apk
Path in archive: lib/armeabi-v7a/libc++_shared.so
Origin 1: /ProjectA/LibraryModuleA1/build/exploded-aar/com.package.name/LibraryModuleB1/<version>/jni/armeabi-v7a/libc++_shared.so
Origin 2: /ProjectA/LibraryModuleA1/build/exploded-aar/com.package.name/LibraryModuleC1/<version>/jni/armeabi-v7a/libc++_shared.so
You can ignore those files in your build.gradle:
android {
packagingOptions {
exclude 'lib/armeabi-v7a/libc++_shared.so'
}
}
* What went wrong:
Execution failed for task ':LibraryModuleA1:packageDebugTest'.
> Duplicate files copied in APK lib/armeabi-v7a/libc++_shared.so
File 1: /ProjectA/LibraryModuleA1/build/exploded-aar/com.package.name/LibraryModuleC1/<version>/jni/armeabi-v7a/libc++_shared.so
File 2: /ProjectA/LibraryModuleA1/build/exploded-aar/com.package.name/LibraryModuleC1/<version>/jni/armeabi-v7a/libc++_shared.so
:LibraryModuleA1:packageDebugTest FAILED
What I've Tried So Far
I attempted to add the suggested closure to my build.gradle file, but which build.gradle file do I add it to? I have added the closure to bgA1, bgB1, and bgC1 (one at a time), with no success.
The suggested closure says to use exclude 'lib/armeabi-v7a/libc++_shared.so'. Each "child" library module builds the libc++_shared.so file under the build/lib path. However, I noticed that the parent library module copies the libc++_shared.so file under jni/armeabi-v7a/libc++_shared.so inside the build/exploded-aar directory structure. (See above) Should the closure instead read exclude 'jni/armeabi-v7a/libc++_shared.so (i.e. jni vs. lib)?
Since I am using Gradle plugin 0.9.1, I tried using pickFirst in place of exclude, but that wasn't successful either.
Can someone help determine how I should configure the `packagingOptions' closure for my given case?
Thank you for your help!
I ran into the same problem and had no luck with exclude or pickFirst. So I used a somewhat ugly workaround. The idea is to create a 'native-libs' folder in the build directory of the main project, copy all required *.so files from ndk library projects there and then tell the build system to package those libs in the apk.
In my main project (the app project), I explicitely define the list of modules that contain ndk codes on which I depend
// Ndk stuff. We have to explicitely manage our NDK dependencies
ext.jniProjects = [project(':ndklib1'), project(':ndklib2'), project(':ndklib3')]
apply from: '../depend_ndk.gradle'
And then, 'depend_ndk.gradle' is a gradle external script that contains
// Build helper for projects that depends on a native library with a NDK part
// Define the list of ndk library you depend on in project main file :
// ext.jniProjects = [project(':ndklib1')]
// apply from : 'depend_ndk.gradle'
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
import com.android.build.gradle.tasks.PackageApplication
// As a workaround, we create a new 'native-libs' folder in the current project and
// copy all the .so we depend on into it
def ndkLibsDir = new File(buildDir, 'native-libs')
ndkLibsDir.mkdir()
task copyDependingNativeLibs(type: Copy) {
// Doc for copy http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.Copy.html
println 'jniProjects ' + jniProjects
jniProjects.each {
from(new File(it.buildDir, 'native-libs')) {
include '**/*.so'
}
}
into ndkLibsDir
}
tasks.withType(PackageApplication) { pkgTask ->
pkgTask.jniFolders = new HashSet<File>()
pkgTask.jniFolders.add(ndkLibsDir)
pkgTask.dependsOn copyDependingNativeLibs
}

Gradle puts duplicate files in the apk file

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.

Categories

Resources