Building Javadocs for Android library projects with Gradle

This post is about how to build Javadocs for Android library projects with Gradle. More importantly it also tells you how to upload the Javadoc artifacts with the uploadArchives task of the Maven plugin. This post will walk you through how I got it to work.

The first issue to solve is that Android library projects have variants, so the usual way of building Javadocs is a little different. At the very least you are going to have a debug and release variant. Let’s add a task in the build.gradle file of our library project to generate the Javadoc.

android.libraryVariants.all { variant ->
    task("${variant.name}Javadoc", type: Javadoc, dependsOn: "assemble${variant.name}") {
        source = variant.javaCompile.source

        title = "My Awesome Library API"

        options.links("http://docs.oracle.com/javase/7/docs/api/");
        options.linksOffline("http://d.android.com/reference","${android.sdkDirectory}/docs/reference");

        // First add all of your dependencies to the classpath, then add the android jars
        classpath += files(variant.javaCompile.classpath.files)
        classpath += files(android.getBootClasspath())

        // We're excluding these generated files
        exclude '**/BuildConfig.java'
        exclude '**/R.java'
    }
}

What this code will do is to add a few tasks dynamically. In our simple example these will be debugJavadoc and releaseJavadoc because these are the default variants.

The next thing to do is to add some more tasks which will package up your Javadocs as jars.

android.libraryVariants.all { variant ->
    task("${variant.name}JavadocJar", type: Jar, dependsOn: "${variant.name}Javadoc") {
            classifier = 'javadoc'
            from tasks["${variant.name}Javadoc"].destinationDir
    }

    // Add the Javadoc jar to the project's artifacts. This will allow us to upload it easily later
    project.artifacts.add("archives", tasks["${variant.name}JavadocJar"]);
}

You should be all set at this stage. You can now generate the Javadocs like this:

# Debug Javadoc
./gradlew :MyLibrary:debugJavadoc
# Release Javadoc
./gradlew :MyLibrary:releaseJavadoc
# Debug Javadoc Jar
./gradlew :MyLibrary:debugJavadocJar
# Release Javadoc Jar
./gradlew :MyLibrary:releaseJavadocJar

If you’re using the maven plugin the Javadoc jar will automatically be uploaded to your artifact server because you added it to the archives in the second snippet of Gradle. To do this you would just run:

./gradlew :MyLibrary:assembleRelease :MyLibrary:uploadArchives

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s