Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate JavaDocs with Android Gradle plugin

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.

like image 834
Matt Accola Avatar asked Jun 10 '13 22:06

Matt Accola


3 Answers

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

like image 150
Oleksandr B Avatar answered Nov 10 '22 02:11

Oleksandr B


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'
    }
}
like image 24
flav Avatar answered Nov 10 '22 00:11

flav


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.

like image 11
Matt Accola Avatar answered Nov 10 '22 02:11

Matt Accola