上述解决方案不适用于脚本gradle 插件(仅适用于二进制 gradle 插件)
我在脚本gradle plugins中实现了我自己的支持 Dokka 的解决方案。
对于 Android Java/Kotlin 项目:
task androidDokka(type: Exec) {
downloadFile('https://jcenter.bintray.com/org/jetbrains/dokka/dokka-fatjar/0.9.17/dokka-fatjar-0.9.17.jar', 'dokka-fatjar.jar')
def srcDirsNumber = android.sourceSets.main.java.srcDirs.size()
def classpathNumber = android.getBootClasspath().size()
def classpath = (classpathNumber > 0) ? android.getBootClasspath()[0] : ''
def javaAPISpecificationLink = getJavaAPISpecificationLink()
if (srcDirsNumber > 0) {
commandLine 'java', '-jar', "$buildDir/download/dokka-fatjar.jar",
android.sourceSets.main.java.srcDirs[0],
(srcDirsNumber == 2) ? android.sourceSets.main.java.srcDirs[1] : '',
(srcDirsNumber == 3) ? android.sourceSets.main.java.srcDirs[2] : '',
'-output', "$buildDir/javadoc", '-format', 'javadoc',
(classpathNumber > 0) ? '-classpath' : '', (classpathNumber > 0) ? classpath : '',
'-links', "https://developer.android.com/reference/^${android.sdkDirectory}/docs/reference/^${javaAPISpecificationLink}"
} else {
commandLine 'java', '-jar', "$buildDir/download/dokka-fatjar.jar"
}
}
task androidDokkaJar(type: Jar, dependsOn: androidDokka) {
classifier = 'javadoc'
from "$buildDir/javadoc"
}
对于其他 Java/Kotlin 项目(非 Android):
task coreDokka(type: Exec) {
downloadFile('https://jcenter.bintray.com/org/jetbrains/dokka/dokka-fatjar/0.9.17/dokka-fatjar-0.9.17.jar', 'dokka-fatjar.jar')
def srcDirsNumber = sourceSets.main.java.srcDirs.size()
def javaAPISpecificationLink = getJavaAPISpecificationLink()
if (srcDirsNumber > 0) {
commandLine 'java', '-jar', "$buildDir/download/dokka-fatjar.jar",
sourceSets.main.java.srcDirs[0],
(srcDirsNumber == 2) ? sourceSets.main.java.srcDirs[1] : '',
(srcDirsNumber == 3) ? sourceSets.main.java.srcDirs[2] : '',
'-output', "$buildDir/javadoc", '-format', 'javadoc',
'-links', "${javaAPISpecificationLink}"
} else {
commandLine 'java', '-jar', "$buildDir/download/dokka-fatjar.jar"
}
}
task dokkaJar(type: Jar, dependsOn: coreDokka) {
classifier = 'javadoc'
from "$buildDir/javadoc"
}