I'm facing a situation where some packages import the same native library:
> 2 files found with path 'lib/arm64-v8a/libc++_shared.so' from inputs:
- ~/.gradle/caches/transforms-3/7d9d92dc8ec1ba2e45aff2ecbb549550/transformed/jetified-react-native-0.68.2/jni/arm64-v8a/libc++_shared.so
- ~/.gradle/caches/transforms-3/e21c7f468f769020a3f8f2f5f3ed5664/transformed/jetified-libvlc-all-3.3.10/jni/arm64-v8a/libc++_shared.so
The standard solution is to use pickFirst:
packagingOptions {
pickFirst 'lib/x86/libc++_shared.so'
pickFirst 'lib/arm64-v8a/libc++_shared.so'
pickFirst 'lib/x86_64/libc++_shared.so'
pickFirst 'lib/armeabi-v7a/libc++_shared.so'
}
The thing is that, in my example as seen above, "first" here is the react-native file. I want to pick the libvlc file.
As far as I know, I can't exclude files based on the source package name.
How can I tell gradle to "pickLast" or "pickSomethingElseThanFirst"?
Thank you
Related
The app builds successfully but while running, i get the following error:
Duplicate files copied in APK lib/x86/librealm-jni.so
File1: /Users/apple/.gradle/caches/modules-2/files-2.1/io.realm/realm-android/0.87.1/6d9a1bba4e31252cc8183aa27a32e6edbdacaeb7/realm-android-0.87.1.jar
File2: /Users/apple/NodeProjects/react-native-mod/droid/databinder/build/intermediates/bundles/default/jni
When I remove the realm dependency from databinder module, it cannot find the lib.
How do I remove the cached version of jar file from adding into the APK?
Add below into your gradle.build file
packagingOptions {
pickFirst 'lib/x86/librealm-jni.so'
pickFirst 'lib/x86_64/librealm-jni.so'
pickFirst 'lib/armeabi/librealm-jni.so'
pickFirst 'lib/armeabi-v7a/librealm-jni.so'
pickFirst 'lib/arm64-v8a/librealm-jni.so'
}
This option will only pick up the .so files when it is firstly seen by the build system and ignore all the subsequent same .so files.
I have two Gradle modules for Android, one is a library and one is an application, and the application has a dependency on the module, like so: compile project(path ':library' configuration: 'debug')
The problem is that both modules use JNI, so they both packagelibc++_shared.so
from the NDK, causing:
com.android.build.api.transform.TransformException:
com.android.builder.packaging.DuplicateFileException:
Duplicate files copied in APK lib/armeabi-v7a/libc++_shared.so
I tried using packagingOptions, but it seems like I can only use that to completely exclude that file from being packaged at all, not just from being copied from the other module?
What type of packagingOptions have you tried to use? Probably, exlude? It looks like pickFirst should work for you. In this case you explicitly tell Gradle that you know about the problem and accept any of these files. Depending on the architectures you support you may need only some of the lines. You can find details in documentation
android {
// some stuff
packagingOptions {
pickFirst 'lib/armeabi-v7a/libgnustl_shared.so'
pickFirst 'lib/arm64-v8a/libgnustl_shared.so'
pickFirst 'lib/x86_64/libgnustl_shared.so'
pickFirst 'lib/x86/libgnustl_shared.so'
}
}
Let's say my project is dependent on two libraries A and B. These libraries are dependent on the same version of a library C. Library C is dependent on a couple of shared library files D. To use library C, libraries A and B both include D in their jniLibs directories.
Now, the issue here is that when I attempt to build this project with the dependencies configured:
compile('group:A:1.0#aar')
compile('group:B:1.0#aar') {
exclude group: 'group', module: 'C'
}
I receive an error stating that there are duplicate D files. How can I go about informing gradle to ignore certain jniLibs from only one of my libraries? Is there an exclude analog for jniLibs transitive dependencies?
I was able to achieve this via gradle by forcing gradle to just use the first version of the shared library that it found:
android {
packagingOptions {
pickFirst 'lib/armeabi/D.so'
pickFirst 'lib/x86_64/D.so'
pickFirst 'lib/armeabi-v7a/D.so'
pickFirst 'lib/x86/D.so'
pickFirst 'lib/arm64-v8a/D.so'
}
}
I'm trying to use the gcloud-java-datastore library in an Android app project. However, I keep running into the following error when trying to build:
Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: ... [one of several different classes]
I've tried excluding various dependencies (e.g. com.google.guava) from gcloud-java-datastore (v0.2.8) in build.gradle to get it to compile. If I exclude com.google.api.grpc, com.google.guava, com.google.api-client, and one of datastore-v1-protos or protobuf-java, I can get it to compile successfully. However, excluding either one of those last two dependencies breaks the core functionality of the library.
Is it even possible to use this library in Android? If so, what am I doing wrong?
Ok, figured it out. I downloaded and extracted datastore-v1-protos-1.0.1.jar, removed everything except the com/google/datastore folder, made a new jar, and included that as a library in my Android Studio project. Then I added the gcloud-java-datastore library to build.gradle with the following exclusions:
compile('com.google.cloud:gcloud-java-datastore:0.2.8') {
exclude group: 'com.google.api-client', module: 'google-api-client-appengine'
exclude group: 'com.google.guava', module: 'guava-jdk5'
exclude group: 'com.google.cloud.datastore', module: 'datastore-v1-protos'
}
and the following packagingOptions:
packagingOptions {
pickFirst 'META-INF/INDEX.LIST'
pickFirst 'META-INF/services/io.grpc.ManagedChannelProvider'
pickFirst 'META-INF/io.netty.versions.properties'
pickFirst 'META-INF/maven/com.google.guava/guava/pom.xml'
pickFirst 'META-INF/maven/com.google.guava/guava/pom.properties'
}
I replaced Java's Date classes with Joda's DateTime classes recently in my Android app. I use Jackson for parsing json. I added the following lines to my build.gradle file
compile com.fasterxml.jackson.datatype:jackson-datatype-joda:2.4.3
compile net.danlew:android.joda:2.7.1
It broke my build. The error message is duplicate files during packaging of APK. It also suggested the following option
android {
packagingOptions {
exclude 'org/joda/time/format/messages_da.properties'
}
}
There are many such files like that in JodaTime like "messages_da.properties", "messages_fr.properties". I believe those are used to provide locale based formatting.
My hunch says that these files should not be excluded. If experts out there can provide a solution for this, it would be great
This is actually an issue that results from depending on multiple joda-time modules in your project.
To fix this, you should exclude any duplicate joda-time module(s) from any dependency in your project that contains a duplicate joda-time module.
To find out what dependencies are including the duplicate joda-time, use the command ./gradlew app:dependencies to list your complete dependency graph. Then look through the list of dependencies and find the ones that includes the duplicate joda-time module. Then exclude joda-time from any dependency that includes a duplicate of it. After doing this your app will build fine.
Example of how to exclude joda-time from a dependency:
// An offending dependency that contains a duplicate joda-time.
compile('com.some.project:some-module:0.1') {
// Exclude joda-time from this dependency to remove the errors.
exclude module: 'joda-time'
}
This is the correct way to handle dependency conflicts.
I solved this issue like
android {
packagingOptions {
exclude 'org/joda/time/format/*.properties'
}
}
My dirty solution:
android {
packagingOptions {
exclude 'META-INF/maven/joda-time/joda-time/pom.properties'
exclude 'META-INF/maven/joda-time/joda-time/pom.xml'
pickFirst 'org/joda/time/format/messages.properties'
pickFirst 'org/joda/time/format/messages_cs.properties'
pickFirst 'org/joda/time/format/messages_da.properties'
pickFirst 'org/joda/time/format/messages_de.properties'
pickFirst 'org/joda/time/format/messages_en.properties'
pickFirst 'org/joda/time/format/messages_es.properties'
pickFirst 'org/joda/time/format/messages_fr.properties'
pickFirst 'org/joda/time/format/messages_it.properties'
pickFirst 'org/joda/time/format/messages_ja.properties'
pickFirst 'org/joda/time/format/messages_no.properties'
pickFirst 'org/joda/time/format/messages_nl.properties'
pickFirst 'org/joda/time/format/messages_pl.properties'
pickFirst 'org/joda/time/format/messages_pt.properties'
pickFirst 'org/joda/time/format/messages_ru.properties'
pickFirst 'org/joda/time/format/messages_tr.properties'
}
}