Can you exclude classes with packagingOptions in android gradle? - android

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

Related

More than one file was found with OS independent path 'lib/armeabi-v7a/libarcore_sdk_jni.so'

I know there is a few similar questions on SO, but it does not work for me...
I created Android lib, that use ArCore. It was a question on SO how to don't include .so file if I use created ndk lib? There is also one answer that sounds right
https://stackoverflow.com/a/58963852/5709159
But after I putted libarcore.so files under my jniLib
I got such error
More than one file was found with OS independent path 'lib/armeabi-v7a/libarcore_sdk_jni.so'
So, I tried to fix it this ways
https://stackoverflow.com/a/44962630/5709159
sourceSets.main {
jniLibs.srcDir 'src/main/jniLibs'
jni.srcDirs = [] //disable automatic ndk-build call
}
https://stackoverflow.com/a/56453718/5709159
packagingOptions {
pickFirst 'src/main/jniLibs/arm64-v8a/libarcore_sdk_jni.so'
pickFirst 'src/main/jniLibs/armeabi-v7a/libarcore_sdk_jni.so'
pickFirst 'src/main/jniLibs/x86/libarcore_sdk_jni.so'
pickFirst 'src/main/jniLibs/x86_64/libarcore_sdk_jni.so'
}
then this
packagingOptions {
pickFirst 'lib/arm64-v8a/libarcore_sdk_jni.so'
pickFirst 'lib/armeabi-v7a/libarcore_sdk_jni.so'
pickFirst 'lib/x86/libarcore_sdk_jni.so'
pickFirst 'lib/x86_64/libarcore_sdk_jni.so'
}
and also this
packagingOptions {
exclude 'lib/arm64-v8a/libarcore_sdk_jni.so'
exclude 'lib/armeabi-v7a/libarcore_sdk_jni.so'
exclude 'lib/x86/libarcore_sdk_jni.so'
exclude 'lib/x86_64/libarcore_sdk_jni.so'
}
Nothing helped.
As far as I understand issue is - I have one copy of arcore.so files under my jniLibs dir and one copy created after Build here
So, how to fix it?
You've likely added the shared .so files and build from source (or reference them otherwise).
One cannot do both at the same time, so you'd either need to build from source and delete those .so files - or delete the arcore-android-sdk module and keep the .so files. Java dependencies might also pull in native assembly, while that part of the build.gradle is missing (just browse AR core in the "External Libraries" to see what it contains, in case it exists there). Using pre-built libraries generally builds quicker and saves time - which is suggested, unless needing to edit cpp sources.
Usually one can provide the dependencies alike in this build.gradle:
dependencies {
implementation "com.google.ar:core:1.13.0"
natives "com.google.ar:core:1.13.0"
}
// Extracts the shared libraries from aars in the natives configuration.
// This is done so that NDK builds can access these libraries.
task extractNativeLibraries() {
// Always extract, this ensures the native libs are updated if the version changes.
outputs.upToDateWhen { false }
doFirst {
configurations.natives.files.each { f ->
copy {
from zipTree(f)
into arcore_libpath
include "jni/**/*"
}
}
}
}
tasks.whenTaskAdded {
task-> if (task.name.contains("external") && !task.name.contains("Clean")) {
task.dependsOn(extractNativeLibraries)
}
}
Such Gradle task could also be the reason for the duplicates, when it's not configured properly. packagingOptions are in every case the wrong approach, when the linker already doesn't know which one to link.
this solution will work
https://github.com/facebook/react-native/issues/35210#issuecomment-1304536693
adding the below code in android/gradle
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())
configurations.all {
resolutionStrategy {
// Remove this override in 0.66, as a proper fix is included in react-native itself.
force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
}
}

How to exclude a Jar file from the gradle dependancy in android project

I am developing an android application in which I am using two gradle dependency and both gradle dependency has libwebrtc.jar with different version so I want to exclude libwebrtc.jar from one of the dependency
dependencies {
implementation 'com.twilio:video-android:3.2.1'
implementation project(":webrtc-android-framework")
}
This two dependancy has libwebrtc.jar file with different version if I am remove the libwebrtc.jar file from the webrtc-android-framework module then some classes is not found so I can't remove the jar file so I want to exclude the libwebrtc.jar file from the com.twilio:video-android:3.2.1 dependancy
I am getting below error when build the application
Error: Program type already present: org.webrtc.BaseBitrateAdjuster
Below is the way enforcing a dependency version:
configurations.all {
resolutionStrategy.force 'your-dependency:version'
}
If you want to exclude the downstream dependencies depended by your dependencies, try below:
implementation "one-of-your-dependencies" {
exclude group:'org.webrtc'
}
Or more generally,
configurations.implementation.transitive = false
Or configurations.compile.transitive = false if you are using gradle version below 2.3.3.

How to exclude duplicate native library from Gradle project dependency?

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

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

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

How to exclude a dependency for a flavor in gradle

We an android projects with lots of flavors. One flavor includes an aar that bundles android-async-http inside the aar file.
The problem is we already define the compile dependency for all our flavors:
compile 'com.loopj.android:android-async-http:1.4.6'
and that results in failed builds with the error:
> com.android.build.api.transform.TransformException: java.util.zip.ZipException:
duplicate entry: com/loopj/android/http/AssertUtils.class
I'm looking to try to exclude that dependency for just one flavor. Something like:
freeCompile('com.loopj.android:android-async-http:1.4.6') {
exclude = true
}
Anyway to do that without defining the dependency for every flavor?

Categories

Resources