8

I have an Android library project FooLib. FooLib references things like Android Contexts but doesn't require any resource files (things in res/) so I am currently packaging this as a JAR for my app to consume.

I now want to make FooLib depend on BarLib, but BarLib does use resources so I can't package BarLib as a JAR. Instead, it is packaged as an AAR. Is it possible for me to have FooLib depend on BarLib but continue to package FooLib as a JAR? Or, does having an AAR dependency in FooLib force me to make it an AAR also?

Matthew
  • 6,356
  • 9
  • 47
  • 59
  • 1
    How can a JAR "depend" on anything? It's just a container for classes. – Scott Barta Mar 01 '14 at 15:46
  • Hmm yeah maybe I should have phrased it -- "Can an Android Library project that can be packaged as a JAR depend on another Android Library project that uses resources (and is therefore packaged as an AAR) and still be packaged as a JAR?" I suspect the answer is no, but I just want to confirm. – Matthew Mar 01 '14 at 17:33

2 Answers2

6

If you have a project that includes JAR files and AAR files as dependencies (see note below), then you can have Android-specific JARs as dependencies that rely on classes in the Android APIs, though the JAR files can't contain Android resources as you already know. Just because it depends on Android classes doesn't mean it needs to be packaged as an AAR.

Here I'm talking about what's 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 perhaps non-Android resources and other files) and doesn't have a sense of dependencies, so there's nothing to break. When it comes time to do the build, the builder just bundles everything together and packages it and doesn't check to see if the JAR has unresolvable dependencies on other classes -- you'll find out with classloader exceptions at runtime.

If you're talking about library modules in a multimodule project in the IDE, then that's a different matter. If you have a module A which can compile to a plain JAR (it uses the apply plugin: 'java' statement in the build file), then it can't depend on an Android module (apply plugin: 'android-library') which compiles to an AAR. This will likely never be fixed, or at least not in the foreseeable future: it's because Android modules have a much more complex notion of source folders, and the Java plugin in Gradle can't understand Android's source sets.

The converse is true, though -- Android modules can depend on plain Java modules.

NOTE

Due to limitations not yet resolved in the Gradle builder, you can't access local AARs in the same way you do as JARs in your build files (by referencing them via compile files(...) statements); you have to fool Gradle into thinking they're in some sort of Maven repository (perhaps by putting them in an actual Maven repository, which can be local). See Adding local .aar files to my gradle build for a workaround if you need it.

Community
  • 1
  • 1
Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • Yes, I'm talking about library modules in a multimodule project. Let's assume module A and module B are both Android modules (both have the `apply plugin: 'android-library'` statements in their build.gradle files). It seems that I can still use http://stackoverflow.com/a/19484146/128948 to package module A as a JAR if it requires no resources. If module B does need resources and I have it packaged as an AAR, can I have module A depend on module B and still package module A as a JAR? – Matthew Mar 01 '14 at 20:35
  • That looks like it should work. android-library plugins can depend on each other, and Jake Wharton's script looks like it should work to have it snag a JAR output out of the module. I believe with these changes in place the module will generate two outputs, both AAR and JAR. – Scott Barta Mar 02 '14 at 01:00
  • A question on this...because for instance appcompat-v7 provides both .aar and .jar in the same maven subfolder...when i specify the dependency though, gradle always picks the .aar file and of course the gradle java plugin doesn't know what it is...is there a way to pick the .jar over the .aar even if the POM specifies package .aar? – Andrea Richiardi Mar 03 '14 at 07:24
  • 1
    Appcompat only supplies .aar. I don't know where you're seeing a .jar version of it. – Scott Barta Mar 03 '14 at 14:22
1

Because AAR format does not contain classes directly as jar files do it is no use for a non Android project. Therefore it would be better to make FooLib an AAR.

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

Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105
  • I'm not looking to use FooLib in a non-Android project, but I'd like to keep FooLib as a JAR to maximize compatibility since if it was an AAR, I wouldn't be able to use it in my Ant-based Android apps. To answer the original question, are you saying "no, an Android library JAR cannot depend on an Android library AAR"? – Matthew Feb 28 '14 at 23:48