Consuming AARs from Eclipse

Increasingly, you will find that libraries for Android developers come only in AAR form. For example, many of the libraries that are part of the “L” Developer Preview have only been shipped to date in AAR form.

This is wonderful if you are using Gradle for Android, with or without Android Studio. It is less than wonderful if you are using Eclipse/ADT.

UPDATE: If you are using Eclipse/ADT with Maven, the android-maven-plugin supports AARs, which I had forgotten when I originally wrote this post. Many thanks to William Ferguson for slapping me with an appropriately-sized trout for missing that.

Fortunately, a StackOverflow user named FD_ came up with a basic recipe for converting an AAR into an Android library project, one that can be used from Eclipse/ADT. I have tested the recipe, have confirmed that it appears to work, and have created a Ruby script to automate the conversion.

The steps that my deaar script goes through are reminiscent of FD_’s recipe:

  1. UnZIP the AAR into some directory.

  2. Create an empty directory that will be the home for the Android library project. For the rest of these steps, I will refer to this as “the output directory”.

  3. Copy the AndroidManifest.xml, res/, and assets/ directories from the AAR into the output directory.

  4. Create a libs/ directory in the output directory. Copy into libs/ the classes.jar from the root of the unZIPped AAR, plus anything in libs/ in the AAR (e.g., mediarouter-v7 has its own JAR of proprietary bits).

  5. Decide what build SDK you want to try to use. You might just choose the highest SDK version you have installed. Or, you can use the android:minSdkVersion and the -vNN resource set qualifiers to get clues as to what a good build SDK might be. If desired, create a project.properties file with a target=android-NNN line, where NNN is your chosen build SDK. Or, you can address this in Eclipse later on.

  6. Import the resulting project into Eclipse, and if needed adjust the build SDK (Project > Properties > Android). Also, you will need to attach to this library project any library projects it depends upon (e.g., mediarouter-v7 depends upon appcompat-v7).

This is all based on the current AAR packaging, and therefore is subject to change.

Also, be sure to see if the AAR is natively available as an Eclipse-friendly Android library project, such as from an open source project’s repository. For example, all of my CWAC libraries ship as Eclipse-friendly Android library projects, in addition to AARs (and, for those not needing resources, JARs). This sort of AAR conversion should be an avenue of last resort, not the first thing you try.

If you try the deaar script and run into problems with it, please use the support options listed in the project’s README. If you run into AARs that do not appear to work using this script, drop me a line.