Generate JavaDocs with Android Gradle plugin

AndroidGradleJavadocAndroid Gradle-Plugin

Android Problem Overview


How can I generate JavaDocs for an Android project using the new Gradle build system?

Here is what I have come up with but it doesn't work.

task generateJavadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    ext.cp = android.libraryVariants.collect { variant ->
        variant.javaCompile.classpath.files
    }
    classpath = files(ext.cp) 
}

The main problem is that I do not get the appropriate android.jar on the classpath so some of the links in the JavaDocs are not resolved. I have to find a way to get all the necessary jars on the classpath.

Another problem with the approach I took is it collects the classpaths for all the build variants, rather than selecting one.

Android Solutions


Solution 1 - Android

For Android gradle plugin 1.1.2+ (com.android.tools.build:gradle:1.1.2+)

libraryVariants - does not work anymore

use:

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    destinationDir = file("../javadoc/")
    failOnError false
}

destinationDir = file("../javadoc/") - locate javadocs at root of project directory (in this way jenkins javadoc plugin could find it and show in special Document panel)

failOnError false - for suppress warnings that can cause fail build on jenkins

Solution 2 - Android

Gradle 1.11 - Gradle Plugin 0.10.0

Replace android.plugin.sdkDirectory by android.sdkDirectory

android.libraryVariants.all { variant ->
    task("generate${variant.name}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)
        options.links("http://docs.oracle.com/javase/7/docs/api/");
        options.links("http://d.android.com/reference/");
        exclude '**/BuildConfig.java'
        exclude '**/R.java'
    }
}

Solution 3 - Android

The solution I ended up settling on is as follows:

android.libraryVariants.all { variant ->

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

}

Xavier Ducrohet confirmed this is the way to do it (with caveats) on the adt-dev group, https://groups.google.com/forum/#!searchin/adt-dev/javadoc/adt-dev/seRizEn8ICA/bafEvUl6mzsJ.

Solution 4 - Android

With android gradle tools 1.10.+ getting the android SDK dir is different than before. You have to change the following:

android.sdkDirectory 

instead of

android.plugin.sdkDirectory

This is the complete solution to the problem:

android.applicationVariants.all { variant ->

    task("generate${variant.name.capitalize()}Javadoc", type: Javadoc) {
        title = "Documentation for Android $android.defaultConfig.versionName b$android.defaultConfig.versionCode"
        destinationDir = new File("${project.getProjectDir()}/doc/compiled/", variant.baseName)
        source = variant.javaCompile.source

        ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
        classpath = files(variant.javaCompile.classpath.files) + files(ext.androidJar)

        description "Generates Javadoc for $variant.name."

        options.memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PRIVATE
        options.links("http://docs.oracle.com/javase/7/docs/api/");
        options.links("http://developer.android.com/reference/");
        exclude '**/BuildConfig.java'
        exclude '**/R.java'
    }
}

Solution 5 - Android

The android jars seem to be in the property android.plugin.runtimeJarList. It's not documented anywhere though, so it might break at anytime.

I've refined your solution to work across build variants:

android.applicationVariants.all { variant ->
    def name = variant.name
    task "javadoc$name"(type: Javadoc) {
        description = "Generates javadoc for build $name"
        destinationDir = new File(destinationDir, variant.baseName)
        source = files(variant.javaCompile.source)
        classpath = files(android.plugin.runtimeJarList, variant.javaCompile.classpath)
        exclude '**/R.html', '**/R.*.html'
    }
}

It generally doesn't make sense to do a javadoc on just the main branch since you might rely on some things from the product flavors. Even debug vs release could have some differences. You could of course, just pick a default variant to use. So you could do something like,

task javadoc(dependsOn: javadocDebug)

Solution 6 - Android

Here is a updated version that used to work in 2014:

android.libraryVariants.all { variant ->
    def name = variant.buildType.name

    if (name.equalsIgnoreCase("debug")) {
        return; // Skip debug builds.
    }
    task("javadoc${variant.name.capitalize()}", type: Javadoc) {
        description "Generates Javadoc for $variant.name."
        source = variant.javaCompile.source
        ext.androidJar = files(plugins.findPlugin("com.android.library").getBootClasspath())
        classpath = files(variant.javaCompile.classpath.files) + ext.androidJar
        exclude '**/internal/**'
        failOnError false
    }

    task("bundleJavadoc${variant.name.capitalize()}", type: Jar) {
        description "Bundles Javadoc into zip for $variant.name."
        classifier = "javadoc"
        from tasks["javadoc${variant.name.capitalize()}"]
    }
}

Solution 7 - Android

I made an open source plugin for that. GitHub Repository

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "gradle.plugin.com.vanniktech:gradle-android-javadoc-plugin:0.2.1"
    }
}

Add this line into your build.gradle

apply plugin: "com.vanniktech.android.javadoc"

Then just execute one of the following:

./gradlew generateDebugJavadoc
./gradlew generateReleaseJavadoc

The java documentation can be found in module/javaDoc/

Solution 8 - Android

I found that this solution works on Gradle plugin 1.3.1 if you have different product flavors.

This will create Gradle tasks to generate Javadoc for each product flavor and build type. For instance, if the module name is app and you have a production and dev product flavor and debug and release build types, you'll have the following Gradle tasks:

  • :app:generateDevDebugJavadoc
  • :app:generateDevReleaseJavadoc
  • :app:generateProductionDebugJavadoc
  • :app:generateProductionReleaseJavadoc

app/build.gradle

android {

    // ...

    applicationVariants.all { variant ->
        // create tasks to generate Javadocs
        task("generate${variant.name.capitalize()}Javadoc", type: Javadoc) {
            source = variant.javaCompile.source
            classpath += project.files(android.getBootClasspath().join(File.pathSeparator))

            // choose the destination that works best for you here
            // I chose this particular directory because Jenkins pulls reports 
            // from this directory already if you need to have the output 
            // folder be parameterized for the build variant, use
            // "build/outputs/docs/javadoc-${variant.name}/" instead and it'll 
            // be in `javadoc-productionRelease` for example
            destinationDir = file("build/outputs/docs/javadoc/")

            // the name that will appear in the docs
            title = rootProject.name

            // you will probably get errors from using the @annotations and 
            // the support library, so just turn off failing for errors
            failOnError false    
        }
    }

    // ...

}

Solution 9 - Android

One more

android.libraryVariants.all { variant ->
if(variant.name.equals('release'))
task("generateJavadoc", type: Javadoc) {
description "Generate Javadoc"
source = android.sourceSets.main.java.srcDirs
//            println '=== source ==='
//            source.collect { relativePath(it) }.sort().each { println it }
ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
classpath = files(variant.javaCompile.classpath.files) + files(ext.androidJar)
//            println '=== classpath ==='
//            classpath.collect { relativePath(it) }.sort().each { println it }
}
}

Use:

gradle generateJavadoc

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatt AccolaView Question on Stackoverflow
Solution 1 - AndroidOleksandr BView Answer on Stackoverflow
Solution 2 - AndroidflavView Answer on Stackoverflow
Solution 3 - AndroidMatt AccolaView Answer on Stackoverflow
Solution 4 - AndroidJulian PielesView Answer on Stackoverflow
Solution 5 - AndroidthatsmydoingView Answer on Stackoverflow
Solution 6 - AndroidvolkersfreundeView Answer on Stackoverflow
Solution 7 - AndroidNiklasView Answer on Stackoverflow
Solution 8 - AndroidPhazorView Answer on Stackoverflow
Solution 9 - Androidmc.devView Answer on Stackoverflow