I've just published a new Android library: joda-time-android. The readme explains why you might want it and how to use it. I wanted to go into detail on two topics I learned about while making it. While the library itself was a simple task (as most of it is imported from joda-time) I'd never before taken the effort to add tests or work on maven deployments before.
Tests were a key goal for this project. I wanted to verify that my additions to the codebase weren't mucking anything up, especially next time I need to update the joda-time code to a new version.
It turns out that testing a library on Android is tricky, especially if you're building it in the aar format. I originally tried using the maven-android-plugin to build the library and test it, but there's no way to directly test the library - I'd have to build an intermediary APK and test that. Not my cup of tea.
Ultimately I bit the bullet and decided to use the Android Testing Framework with gradle (and an emulator). I assume that one day the gradle android plugin will support unit testing and in the meantime this proved the simplest solution.
I decided on two release formats:
- The project would be in the form of a standard Android library. That way anyone using Eclipse/ADT can consume it.
- The project would be packaged as an aar. (I was able to skip the apklib format because maven-android-plugin supports aar now.)
Generating an aar is simple; just use the gradle plugin android-library. Uploading it to Maven Central so that you can easily pull in the library was a bit more tricky due to my inexperience. Ultimately, a few handy guides got me through it:
- The Sonatype Maven Repository Usage Guide - Crucial instructions on how to get a project into Sonatype's repositories, which offer Maven Central hosting.
- Publishing an Android Library (AAR) to a Maven Repository - A nice blog post outlining the steps needed to push Android projects in particular to Sonatype.
- gradle-mvn-push - A handy script which makes uploading Android projects to Sonatype a snap. It helps skip some of the steps in the aforementioned blog post (and its readme summarizes the upload process).