Can the Android JAR library depend on the Android AAR library?

I have an Android library project FooLib . FooLib refers to things like Android Context , but does not require any resource files (things in res/ ), so I am currently packing this as a JAR for use by my application.

Now I want FooLib depend on BarLib , but BarLib uses resources, so I cannot pack BarLib as a JAR. Instead, it is packaged as an AAR. Is it possible that FooLib dependent on BarLib , but will continue the FooLib package as a JAR? Or, does the AAR dependency in FooLib make me also make AAR?

+7
android android-gradle jar gradle aar
source share
2 answers

If you have a project that includes JAR files and AAR files as dependencies (see note below), then you can use Android JARs as dependencies that depend on classes in the Android API, although JAR files cannot contain Android Resources , As you already know. Just because it depends on the classes of Android, it does not mean that it should be packaged as AAR.

Here I am talking about that, perhaps, a single-module project that includes a number of JAR and AAR dependencies. A JAR file is just a collection of class files (and possibly non-Android resources and other files) and does not make sense of dependencies, so nothing is broken. When the time comes for assembly, the builder simply bundles everything together and packs it and does not check if the JAR has unsolvable dependencies on other classes - you will find exceptions with loading classes at runtime.

If you are talking about the modules library in a multi-module project in the IDE, then this is another matter. If you have module A that can compile a simple JAR (it uses the apply plugin: 'java' statement in the assembly file), then it cannot depend on the Android module ( apply plugin: 'android-library' ) that compiles into AAR . This will probably never be fixed, or at least not in the foreseeable future: since the Android modules have a much more complicated idea of ​​the source folders, and the Java plugin in Gradle cannot understand the original Android settings.

The converse is true, though - Android modules may depend on simple Java modules.

Note

Due to limitations not yet allowed in the Gradle builder, you cannot access local AARs in the same way as JARs in build files (by referencing them using compile files(...) operators); you should fool Gradle into thinking that they are in some Maven repository (perhaps putting them in the Maven repository is the actual one , which may be local). See Adding local .aar files to my Gradle construct for a workaround if you need to.

+5
source share

Because the AAR format does not contain classes directly, since jar files make this useless for a project without Android. Therefore, it would be better to make FooLib AAR.

Another solution would be to extract the jar library from BarLib and depend on it.

+1
source share

All Articles