How to include an external jar file to aar file in Android - android

I want to develop a solution that allows me to use an external jar library in an SDK generated in aar file for an Android project.
What I have as inputs :
SDK : "de.xx.sdk:xxx-android-v1.0.0"
external library : "libs/xxxx-v1.0.0"
what I found out in my investigation is how to exclude a library from project like the following :
implementation('android.arch.work:work-runtime:1.0.0') {
exclude group: 'com.google.guava', module: 'listenablefuture'
}
Is there something similar to write in gradle file how to include a library instead of exclude it.
implementation('de.xx.sdk:xxx-android-v1.0.0') {
include group: 'libs/xxxx-v1.0.0'
}
Thank you

You should try to put your jar into a module (File->New->New Module->Import JAR/AAR Package) and then add the module as a dependency.

implementation('de.xx.sdk:xxx-android-v1.0.0') {
include group: 'libs/xxxx-v1.0.0'
}
You can't do it in this way.
Something like exclude group: 'com.google.guava', module: 'listenablefuture' is possible because you have an artifact with a pom file published in a maven repo.
In the pom file there is the list of the transitive dependencies, and if there is a match with the group/module gradle is able to exclude it.
To achieve something similar, you have to publish the lib a in maven repo with a group/name (it can't be lib/....v1.0.0)

I know this is quite old, but it might can help the others
can add this snippet to build.gradle
https://gist.github.com/stepio/824ef073447eb8d8d654f22d73f9f30b

Related

Android exclude jar from aar, there are classes.jar and lint.jar in appcompat-1.2.0.aar

Is there a way to exclude jar from aar? there are classes.jar and lint.jar in appcompat-1.2.0.aar . I want to exclude lint.jar when I implmentation 'androidx.appcompat:appcompat:1.2.0'
when I place implmentation 'androidx.appcompat:appcompat:1.2.0' to build gradle , my customized Lint rules will donot work. Because there is a lint.jar in appcomat.aar, so I want exclude lint.jar from appcomat.aar.
Because lint is just a jar in appcompat.aar, There is no lint dependency in pom.xml, So I cannot use blow code to exclude lint.jar :
implementation('androidx.appcompat:appcompat:1.2.0') {
exclude group: 'androidx.appcompat', module: 'lint'
}
You can use something like
configurations {
all*.exclude group: 'androidx.appcompat:appcompat', module: 'lint'
}
in your app level build.gradle file.
This basically excludes some of the modules inside the libraries which you do not need to use.
Group specifies the package name of the dependency you have imported, and module specifies the particular module that you wish to exclude.

Exclude package/classes from #aar, gradle dependency

I've following dependency added in build.gradle file.
compile 'com.aerisweather:aeris-maps-lib:2.0.0#aar'
It is from
https://oss.sonatype.org/content/repositories/comaerisweather-1027/com/aerisweather/aeris-maps-lib/2.0.0/
If you the see artifacts from following URL, It has android support v7 library classes.
https://oss.sonatype.org/#nexus-search;quick~aerisweather
I want to exclude that package when running/packaging the application. I'm unable to run/package the app due to duplicate class error.
I've tried adding configurations like this,
configurations {
all*.exclude group: 'com.android.support', module: 'appcompat-v7'
}
But this excludes it from entire project which leads me to many errors.
I've tried everything but still getting following error.
Error:Execution failed for task ':transformClassesWithJarMergingForDebug'.
com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: android/support/v7/appcompat/R$anim.class
This library has also as dependency support-v4 and mediarouter-v7.
You need to exclude them all from aeris-maps-lib and include as your own dependency.
def supportLibraryVersion = '25.0.1'
dependencies {
compile "com.android.support:support-v4:${supportLibraryVersion}"
compile "com.android.support:support-annotations:${supportLibraryVersion}"
compile "com.android.support:appcompat-v7:${supportLibraryVersion}"
//... other deps
compile ('com.aerisweather:aeris-maps-lib:2.0.0#aar', {
exclude group: 'com.android.support', module: 'support-v4'
exclude group: 'com.android.support', module: 'appcompat-v7'
exclude group: 'com.android.support', module: 'mediarouter-v7'
})
}
PS.
aeris-maps-lib has also com.google.android.gms:play-services dependency, which is the whole Play Services package (it's large) and you will need to enable MultiDex or shrink code with proguard.
not a direct answer, but an advice.
The exclusion feature provided by gradle (exclude method invocation) doesn't work for contents inside local aar files as those contents aren't defined by dependency management and hence aren't recognised by the same.
As far as the dependency resolution is concerned, the aar file is an individual unit (including all the resources/classes within). So the file needs to be built in a way which doesn't include those entries; Or if the file is not built by you, you can unpack and omit the files in question and repack.
While there may be hackish ways to drop certain files using gradle (I couldn't find any reliable one yet), where we could possibly hook into some intermediate build steps and get rid of the files; but the generally advised best practise is to avoid packaging publicly available dependencies into the aar/jar to avoid duplicate entry issues and keep the aar/jar size smaller.

In an Android Gradle build, how to exclude dependencies from an included jar file?

In my Android project, I use a library that comes as a jar.
I include it in the dependencies section like so:
dependencies {
...
compile files('libs/thethirdpartylibrary.jar')
...
}
I also want to use the okhttp library, which I include like this:
compile ('com.squareup.okhttp:okhttp:2.7.5')
(This particular version of okhttp depends on okio 1.6.0.)
The problem is that the thirdparty jar library depends on okio v0.9.0 and what's worse, bundles it.
As a result, I get a dex conflict error at build time.
I was able to resolve this by manually removing okio from the jar file and this seems to work. But I'm wondering if there's a way to do this in gradle.
My question: Can I remove bundled, transitive ( <- I hope I'm using this word the right way) dependencies from an included jar during build-time with gradle?
Exclude the Group in the dependencies by using the below lines.
1.
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
}
2.
dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile ("com.xxx:xxx-commons:1.+") {
exclude group: 'junit', module: 'junit'
}
}
3.
configurations {
runtime.exclude group: "org.slf4j", module: "slf4j-log4j12"
}
Try this one.
For more detail
According to this discussion here https://groups.google.com/forum/#!topic/adt-dev/g1AiJM7PeVs, what you want to do is not possible.
The syntax suggested in the other answers is for "normal" Maven dependencies.

How to prevent dependency from compiling duplicate dependency

I have an SDk in the form of an aar in my project's lib folder. I am attempting to add it as a dependency in my project's build.gradle.
dependencies {
...
compile(name:'mySDK', ex:'aar')
...
}
However, when I try to rebuild gradle, I get the following:
Error:(48, 0) No such property: ex for class: org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency_Decorated.
> java.util.zip.ZipException: duplicate entry: org/apache/http/impl/cookie/IgnoreSpecFactory.class
I unziped mySDK and confirmed that httpclient-android-4.3.5.1.jar is a library used in mySDK (this jar contains org.apache.http...).
I then updated my gradle to try and exclude this library.
dependencies {
...
compile(name:'mySDK', ex:'aar'){
exclude(group: 'org.apache.http', module: 'httpclient-android-4.3.5.1')
}
...
}
I've also tried excluding:
exclude module: 'httpclient'
exclude(group: 'org.apache.http', module: 'httpclient-android')
exclude(group: 'org.apache.http', module: 'httpclient')
Unfortunately, I'm still receiving the same java.util.zip.ZipException. Any idea how I can solve this?
You can't do it with an aar file and a flat repo.
You can do it only it you are using a maven dependency. In this case you have a pom file which describes the dependencies (with modules and group).
In an aar there isn't any pom file which describes that the jar file is a dependency called httpclient-android-4.3.5.1 for example.
Usually the aar file doesn't contain the dependencies.
The best option is to have this aar in a maven repo in order to exclude dependencies.
Since it is not possible, an alternative could be to unzip the aar file, remove the jar dependency and repack the aar (it is a simple zip file).

How to exclude module from all dependencies but leave explicit declaration Gradle

I am trying to solve the problem,
I have some dependencies declared in build.gradle file for my android app, but the problem is that a lot of these dependencies use the same compat library, in my case appcompat-v7.
It is possible to exclude this library for each dependency
compile ('com.github......'){
exclude group: 'com.android.support', module: 'appcompat-v7'
}
But I need to do this in for each dependency
Another way is to use such expression
configurations {
compile.exclude module: 'appcompat-v7'
}
This works, but even If declare this library explicitly it is ignored compile 'com.android.support:appcompat-v7:+'
All what I need it is to include this library only once for the whole app, because if compile without exclude it will show a lot of errors like has been already defined.
Maybe there is an easier way to get this working. I would be grateful for any help, thanks.
We use a provided configuration in gradle (so that when we gradle:eclipse, the packages are included, but are not included when compiled into a jar, as these jars are expected to be provided at runtime). This configuration looks like the following:
configurations {
provided {
dependencies.all {dep ->
configurations.default.exclude group: dep.group, module:dep.name
}
}
compile.extendsFrom provided
}
This allows us to include dependencies as follows:
dependencies {
compile("org.scala-lang:scala-library:2.11.7")
compile("org.scala-lang:scala-compiler:2.11.7")
provided("org.apache.spark:spark-core_2_11:2.0.0")
}
Try creating a configuration which contains all dependencies where you want to exclude appcompat-v7, and then extend compile from this new configuration.

Categories

Resources