Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 654
JHH Avatar asked Oct 22 '17 21:10

JHH


People also ask

What is Javadoc in gradle?

Javadoc. Generates HTML API documentation for Java classes. If you create your own Javadoc tasks remember to specify the 'source' property! Without source the Javadoc task will not create any documentation. Example: plugins { id 'java' } task myJavadocs(type: Javadoc) { source = sourceSets.main.allJava }

How do you write a Javadoc comment for a method?

Writing Javadoc Comments In general, Javadoc comments are any multi-line comments (" /** ... */ ") that are placed before class, field, or method declarations. They must begin with a slash and two stars, and they can include special tags to describe characteristics like method parameters or return values.

How do I add a Javadoc?

Place the caret at the declaration in the editor, press Alt+Enter , and select Add Javadoc from the list.

What are Javadoc tags?

The javadoc TagsRepresents the relative path to the generated document's root directory from any generated page. Adds a comment indicating that this API should no longer be used. Adds a Throws subheading to the generated documentation, with the classname and description text.


2 Answers

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
}

like image 149
shizhen Avatar answered Sep 28 '22 04:09

shizhen


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.

like image 42
nsndvd Avatar answered Sep 28 '22 03:09

nsndvd