0

I have to develop an Android library which needs to be packaged and distributed to Android developers. It has both Java classes and resources (icons etc).

I'm wondering about the best way of distributing such a library. As we all know many well-known libraries are distributed in different formats:

  • ActionBAr Sherlock - ZIP or APKLIB
  • Flurry - JAR

My Prerequisites:

  1. I can't distribute sources - only compiled java classes and resources are allowed
  2. My aim is to have integration with client project instruction as simple as possible - e.g. one line in Maven/Gradle as dependency and few lines of code how to use it within the app.
  3. I have to respect every build system: those using NBS, those using android-maven-plugin and those using legacy ANT.

My first idea was to distribute this library as APKLIB (Maven) and AAR (NBS) but then I realized no support for ANT.

Questions:

  1. Can APKLIB and AAR have only compiled classes and resources instead of plain Java sources?
  2. How to distribute a compiled android library project for ANT? As a ZIP?
  3. What about generating R for libraries?

Thanks for any suggestions!

  • "How to distribute a compiled android library project for ANT? As a ZIP?" -- yes. Create a regular Android library project and get it working. Then, clone the Android library project directory and replace the `src/` tree with a JAR in `libs/`, where the JAR contains the classes compiled with your original library project (optionally with manual ProGuard-ing). You will still need an empty `src/` directory, last I checked, or the traditional build system will choke. ZIP up the cloned library project, and that's your distribution. The Play Services SDK is pretty much distributed this way AFAICT. – CommonsWare Jun 11 '13 at 14:20

1 Answers1

2
  1. An APKLIB is only supposed to contain sources, libs and resources, not the compiled .class files. Complementary, the AAR format is "the binary distribution of an Android Library Project" and must contain classes.jar, but no sources. Edit: However, there is a trick that you can use for an APKLIB to contain compiled classes instead of source code: Compile the source code and put it at libs/<library-name>.jar inside the APKLIB instead of the bin directory. Then replace all files in src with some dummy file.

  2. You can distribute a compiled Android Library Project for Ant as an AAR, but you have to keep a few things in mind when using it. Ant explicitly looks for the src directory in the extracted AAR file, but there is none, and Ant looks for classes.jar in the bin directory, but it is located at the root of the extracted AAR file. You can work around this by creating an empty src directory and copying classes.jar to bin/classes.jar. That is exactly what we do in an Ivy post-retrieve-artifact step after retrieving the AAR artifact. Edit: This has the disadvantage that ant clean will delete the bin/classes.jar, and you're probably better off with using an APKLIB with a jar packaged as part of the libs directory as described above.

  3. Neither APKLIB nor AAR are not supposed to come with a compiled R class, both ship with their original resources in the res directory. Additionally, an AAR has to contain an R.txt file listing all the resources (as output by aapt --output-text-symbols).

sschuberth
  • 28,386
  • 6
  • 101
  • 146