如何使用Gradle生成javadoc?

2022-09-02 21:32:33

我想从aar lib文件中生成javadoc,但我的实际任务不起作用,并以错误结束。我对娇生哗之以忘怀。

应该在哪里生成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*'
    }
}

答案 1

如果您遵循标准项目结构,请使用:

apply plugin: 'java'

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

如果不是,则需要指定闭包。添加如下闭包:include

include 'path/to/directory/*'

如果包含闭包不起作用,则添加指向目录的闭包:sourceSetsrcDirmodule/java

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

    }
}

之后运行命令。然后检查构建目录中是否有包含所有 javadocs 的 html 文件。注意:将 srcDir 替换为根包路径。$gradle javadoc


答案 2

推荐