Saturday, 25 May 2013

Referencing android library packages in Gradle

UPD: Seems like referencing local .aar packages is not recommended. But you can just as easily set them up in a local maven repo, which will work even better!

Using local .aar Android library packages in gradle builds


Playing with the new Gradle Android build system, I created some multi project setups, and it seems to work great! I had a project with a main android app, an android library and a java library all wired up and working well.

But once I tried to decouple the android library to a separate location, and just inject the .aar package in the project depencency list I ran into a problem. The project completely refused to build, stating:

:mainapp:packageDebug
Error: duplicate files during packaging of APK D:\Development\MyProject\mainapp\build\apk\mainapp-debug-unaligned.apk
        Path in archive: AndroidManifest.xml
        Origin 1: D:\Development\MyProject\mainapp\build\libs\mainapp-debug.ap_
        Origin 2: D:\Development\MyProject\mainapp\libs\AndroidExtensions.aar

Everything seemed to be configured correctly, the android library was producing a proper .aar library package file, and I was sure that it should work out of the box, but it was just refusing to work...

The solution was actually much simpler than I expected:

I checked my build.gradle file, and had the library package referenced in the dependencies list:

dependencies {
    compile files('libs/AndroidExtensions.aar')
}

But after playing around with different alternatives, I found out that if I just skipped the file extension for the .aar package, it would still be picked up AND will be processed correctly! Not sure if it's a bug in 0.4/0.4.1 or just the way it works, but it's certainly not documented.

Oh, and the .jar packages should be referenced WITH the extension file, in case you're wondering :)
Yup, certainly needs some work in terms of consistency!

So the right way to reference it would be:

dependencies {
    compile files('libs/AndroidExtensions')
}