19

I would like generate javadoc out of aar lib file, but my actual task not work and finish with error. I'm somewhat lost with gradle.

Where should generate javadocs?

android.libraryVariants.all { variant ->

    // This part is for change AAR location
    variant.outputs.each { output ->
        def outputFile = output.outputFile
        if (outputFile != null && outputFile.name.endsWith('.aar')) {
            def fileName = "${archivesBaseName}-${version}.aar"
            output.outputFile = new File("$rootProject.projectDir/build/generated-aar", fileName)
        }
    }

    // Here generate tasks for variant to generate Javadocs
    task "generate${variant.name.capitalize()}Javadoc"(type: Javadoc) {
        description = "Generates javadoc for build $variant.name"
        destinationDir = new File("$rootProject.projectDir/build/generated-javadoc", variant.baseName)

        // add SDK classes
        source = variant.javaCompiler.source

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

        ext.androidDoc = "${android.sdkDirectory}/docs/reference"

        options.linksOffline("http://d.android.com/reference", ext.androidDoc)
        options.links("http://docs.oracle.com/javase/7/docs/api/")
        options.links("http://d.android.com/reference/")

        exclude '*BuildConfig.java'
        exclude '*R.java'
        exclude '*impl*'
        exclude '*tests*'
    }
}
Óscar
  • 809
  • 1
  • 8
  • 33

1 Answers1

25

If you are following standard project structure then use:

apply plugin: 'java'

javadoc {
    source = sourceSets.main.allJava
    classpath = configurations.compile
}

If not then you need to specify the include closure. Add the closure like:

include 'path/to/directory/*'

If include closure is not working then add sourceSet closure with srcDir pointing to the module/java directory:

sourceSets {
    build {
        java.srcDir file('src/main/java')

    }
}

And after that run $gradle javadoc command. Then check your build directory for html files having all javadocs. Note: Replace srcDir with your root package path.

Abhijit Maity
  • 467
  • 5
  • 11
  • I've use android.library plugin and maven plugin. – Óscar Sep 03 '17 at 18:46
  • I try `include **/*.java` but without results – Óscar Sep 03 '17 at 18:48
  • 4
    I'm not sure if the classpath field is necessary anymore. One thing to note is that the java-library plugin has the javadoc tasks available. You can also look [here](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.javadoc.Javadoc.html) for the updated specifications since the `configurations.compile` option is being deprecated. – joshpetit Jan 11 '21 at 12:20
  • https://stackoverflow.com/questions/72952400/gradle-skipping-task-groovydoc-as-it-has-no-source-files-and-no-previous-ou - I'm facing a similar problem with groovydoc.. can you please take a look? – Saili Gaitonde Jul 14 '22 at 02:38
  • As suggested by @jsohpetit, only including: `plugins { id 'java' }` and ` `javadoc { source = sourceSets.main.allJava }` worked for me. The `classpath = ..` line resulted in [this](https://stackoverflow.com/q/69044250/7437143) error. Thank you for sharing your solution! – a.t. Aug 19 '23 at 15:45