JHH
JHH

Reputation: 9285

Android studio / Gradle javadoc task

I've been struggling to setup a gradle task to generate Javadocs for my Android library, but when there are external dependencies to other libraries, doc generation fails. This seems to be a common task, but somehow there doesn't seem to be an easy solution, as for example this answer will reveal (re-generating exploded-aar manually is a bit absurd, and also, on Android Studio 3.0 even that doesn't work anymore due to the new dependency directives).

However, I have noticed that generating Javadoc through the Android Studio GUI (Tools menu) works just fine - dependencies to other libraries are resolved etc. So how does this work - does this menu not utilize a gradle task for generating Javadoc?

Since I need to generate Javadoc using gradle as part of CI I find it very frustrating that there is no documented way of getting it to work, while there is a way that works through the menues. Doesn't the Android Studio Tools -> Generate Javadoc menu in turn use a gradle task? Since dependencies are listed with gradle files, and the Javadoc tools menu apparently is able to resolve those dependencies - how is it implemented? How does it source the jars embedded in the dependant aar libraries, etc? How can it be used stand-alone and not though the Android Studio GUI?

Upvotes: 18

Views: 5685

Answers (3)

shizhen
shizhen

Reputation: 12573

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
}

Upvotes: 8

nsndvd
nsndvd

Reputation: 830

I use a gradle task that just executes a bash script file, with a single (pretty long) javadoc command.

What you can do is run the Javadoc generation from Android Studio once, then copy the executed javadoc command from the Studio log, with all the right parameters, and automate the execution of the same command in your gradle.

Upvotes: 2

Eddie Lopez
Eddie Lopez

Reputation: 1139

The tool to generate java style documentation is called javadoc and it comes installed in every JDK. You can configure which classes or packages you want to be included, which ones should be excluded and many other options. Type javadoc in a terminal where a JDK is available and you'll get an idea. See also https://docs.oracle.com/javase/9/javadoc/javadoc.htm#JSJAV-GUID-7A344353-3BBF-45C4-8B28-15025DDCC643

After you get to your optimal configuration, you can include a javadoc step in your CI.

Upvotes: -3

Related Questions