How to generate JavaDoc for Android using Gradle

So, today I’ve been trying to get JavaDoc produced and I’ve come across a number of issues. Basically, there doesn’t seem to be a single place that just says do this. There are many stackoverflow answers, some work, some don’t then if you are running JDK 1.8 then there are other issues. So, I’ll show below how to generate JavaDoc, how to call it from Gradle (because I’m new to this and it wasn’t that obvious) and how to get around the JDK 1.8 issue.

Add the following configuration to your modules build.gradle file in your project. This is the one in the app folder if you are using a standard project setup.


android.applicationVariants.all { variant ->
    task("generate${variant.name.capitalize()}JavaDoc", type: Javadoc) {
        description "Generates Javadoc for $variant.name."
        source = variant.javaCompile.source
        ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
        classpath = files(variant.javaCompile.classpath.files) + files(ext.androidJar)
    }
}

To run this on the commandline then call ./gradlew generateDebugJavaDoc or ./gradlew generateReleaseJavaDoc

If you are running JDK 1.8 on your system you will get a whole pile of lint failures from your R.java file and since you can’t fix those then you have to work around the issue for now. This information was taken from http://blog.joda.org/2014/02/turning-off-doclint-in-jdk-8-javadoc.html

As you are using gradle it is simpler in my view to put this workaround in the gradle file so in the top-level project gradle file add the following.


if (JavaVersion.current().isJava8Compatible()) {
    allprojects {
        tasks.withType(Javadoc) {
            options.addStringOption('Xdoclint:none', '-quiet')
        }
    }
}

One comment

  1. I put this in my build.gradle for a sub module and get errors relating to the property ‘applicationVariants’

    Error:(85, 0) Could not find property ‘applicationVariants’ on com.android.build.gradle.LibraryExtension_Decorated@2c6f56e9.
    Open File

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 )

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 )

w

Connecting to %s