Android studio / Gradle javadoc task

Maybe you have got the solution to this. Just in case not, below is how I generate API doc for my Jenkins CI.

task generateApiDoc() {
    group "reporting"
    description "Generates Javadoc."
}

android.libraryVariants.all { variant ->
    // Only consider release 
    if (variant.buildType.name == "release") {
        def task = project.tasks.create("generate${variant.name.capitalize()}Javadoc", Javadoc) {
            group "ApiDoc"
            description "Generates Javadoc for $variant.name."

            // Source files from the variant
            source = variant.javaCompiler.source
            // Classpath from the variant + android.jar
            classpath = variant.javaCompiler.classpath + files(prj.android.getBootClasspath()) + files("$buildDir/intermediates/classes/release")

            /* add the excluded packages */
            exclude "**/R**"
            exclude "**/BuildConfig*"

            options.windowTitle = "My Library"
            options.memberLevel = JavadocMemberLevel.PROTECTED
            options.linkSource false
            options.author = true
            //options.links("http://docs.oracle.com/javase/7/docs/api/", "http://d.android.com/reference");

            failOnError false
        }

        task.dependsOn assemble

        generateApiDoc.dependsOn task
    }
}

Then run below gradle commands to get your api doc in place of "$buildDir/docs".

./gradlew assembleRelease
./gradlew generateApiDoc

Edit for Gradle Plugin 3.4.1

android.libraryVariants.all { variant ->

    def task = project.tasks.create("generate${variant.name.capitalize()}Javadoc", Javadoc) {
        title "API Documentation (${project.android.defaultConfig.versionName})"
        group "ApiDoc"
        description "Generates Javadoc for $variant.name."

        // Source files from the variant
        source = variant.sourceSets.collect { it.java.sourceFiles }.inject { m, i -> m + i }

        // To fix issue: Error: Can not create variant 'android-lint' after configuration ': library: debugRuntimeElements' has been resolved
        doFirst {
            classpath = project.files(variant.javaCompileProvider.get().classpath.files,
                    project.android.getBootClasspath())
        }

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

        exclude "**/R"
        exclude "**/R.**"
        exclude "**/R$**"
        exclude "**/BuildConfig*"

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

        options.windowTitle = "API Documentation (${project.android.defaultConfig.versionName})"
        options.memberLevel = JavadocMemberLevel.PROTECTED
        options.linkSource false
        options.author = false

        failOnError true
    }

    task.dependsOn "assemble${variant.name.capitalize()}"
    generateApiDoc.dependsOn task
}

Leave a Comment