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("${}Javadoc", type: Javadoc, dependsOn: "assemble${}") {
        source = variant.javaCompile.source

        title = "My Awesome Library API"


        // 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 '**/'
        exclude '**/'

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("${}JavadocJar", type: Jar, dependsOn: "${}Javadoc") {
            classifier = 'javadoc'
            from tasks["${}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["${}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: Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s