可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
How can I generate JavaDocs for an Android project using the new Gradle build system?
Here is what I have come up with but it doesn't work.
task generateJavadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
ext.cp = android.libraryVariants.collect { variant ->
variant.javaCompile.classpath.files
}
classpath = files(ext.cp)
}
The main problem is that I do not get the appropriate android.jar on the classpath so some of the links in the JavaDocs are not resolved. I have to find a way to get all the necessary jars on the classpath.
Another problem with the approach I took is it collects the classpaths for all the build variants, rather than selecting one.
回答1:
For Android gradle plugin 1.1.2+ (com.android.tools.build:gradle:1.1.2+)
libraryVariants - does not work anymore
use:
task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
destinationDir = file("../javadoc/")
failOnError false
}
destinationDir = file("../javadoc/") - locate javadocs at root of project directory (in this way jenkins javadoc plugin could find it and show in special Document panel)
failOnError false - for suppress warnings that can cause fail build on jenkins
回答2:
Gradle 1.11 - Gradle Plugin 0.10.0
Replace android.plugin.sdkDirectory
by android.sdkDirectory
android.libraryVariants.all { variant ->
task("generate${variant.name}Javadoc", type: Javadoc) {
description "Generates Javadoc for $variant.name."
source = variant.javaCompile.source
ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
classpath = files(variant.javaCompile.classpath.files) + files(ext.androidJar)
options.links("http://docs.oracle.com/javase/7/docs/api/");
options.links("http://d.android.com/reference/");
exclude '**/BuildConfig.java'
exclude '**/R.java'
}
}
回答3:
The solution I ended up settling on is as follows:
android.libraryVariants.all { variant ->
task("generate${variant.name}Javadoc", type: Javadoc) {
description "Generates Javadoc for $variant.name."
source = variant.javaCompile.source
ext.androidJar = "${android.plugin.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
classpath = files(variant.javaCompile.classpath.files) + files(ext.androidJar)
}
}
Xavier Ducrohet confirmed this is the way to do it (with caveats) on the adt-dev group, https://groups.google.com/forum/#!searchin/adt-dev/javadoc/adt-dev/seRizEn8ICA/bafEvUl6mzsJ.
回答4:
With android gradle tools 1.10.+ getting the android SDK dir is different than before. You have to change the following:
android.sdkDirectory
instead of
android.plugin.sdkDirectory
This is the complete solution to the problem:
android.applicationVariants.all { variant ->
task("generate${variant.name.capitalize()}Javadoc", type: Javadoc) {
title = "Documentation for Android $android.defaultConfig.versionName b$android.defaultConfig.versionCode"
destinationDir = new File("${project.getProjectDir()}/doc/compiled/", variant.baseName)
source = variant.javaCompile.source
ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
classpath = files(variant.javaCompile.classpath.files) + files(ext.androidJar)
description "Generates Javadoc for $variant.name."
options.memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PRIVATE
options.links("http://docs.oracle.com/javase/7/docs/api/");
options.links("http://developer.android.com/reference/");
exclude '**/BuildConfig.java'
exclude '**/R.java'
}
}
回答5:
The android jars seem to be in the property android.plugin.runtimeJarList
. It's not documented anywhere though, so it might break at anytime.
I've refined your solution to work across build variants:
android.applicationVariants.all { variant ->
def name = variant.name
task "javadoc$name"(type: Javadoc) {
description = "Generates javadoc for build $name"
destinationDir = new File(destinationDir, variant.baseName)
source = files(variant.javaCompile.source)
classpath = files(android.plugin.runtimeJarList, variant.javaCompile.classpath)
exclude '**/R.html', '**/R.*.html'
}
}
It generally doesn't make sense to do a javadoc on just the main branch since you might rely on some things from the product flavors. Even debug vs release could have some differences. You could of course, just pick a default variant to use. So you could do something like,
task javadoc(dependsOn: javadocDebug)
回答6:
Here is a updated version that used to work in 2014:
android.libraryVariants.all { variant ->
def name = variant.buildType.name
if (name.equalsIgnoreCase("debug")) {
return; // Skip debug builds.
}
task("javadoc${variant.name.capitalize()}", type: Javadoc) {
description "Generates Javadoc for $variant.name."
source = variant.javaCompile.source
ext.androidJar = files(plugins.findPlugin("com.android.library").getBootClasspath())
classpath = files(variant.javaCompile.classpath.files) + ext.androidJar
exclude '**/internal/**'
failOnError false
}
task("bundleJavadoc${variant.name.capitalize()}", type: Jar) {
description "Bundles Javadoc into zip for $variant.name."
classifier = "javadoc"
from tasks["javadoc${variant.name.capitalize()}"]
}
}
回答7:
I made an open source plugin for that. GitHub Repository
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.vanniktech:gradle-android-javadoc-plugin:0.2.1"
}
}
Add this line into your build.gradle
apply plugin: "com.vanniktech.android.javadoc"
Then just execute one of the following:
./gradlew generateDebugJavadoc
./gradlew generateReleaseJavadoc
The java documentation can be found in module/javaDoc/
回答8:
I found that this solution works on Gradle plugin 1.3.1 if you have different product flavors.
This will create Gradle tasks to generate Javadoc for each product flavor and build type. For instance, if the module name is app
and you have a production
and dev
product flavor and debug
and release
build types, you'll have the following Gradle tasks:
- :app:generateDevDebugJavadoc
- :app:generateDevReleaseJavadoc
- :app:generateProductionDebugJavadoc
- :app:generateProductionReleaseJavadoc
app/build.gradle
android {
// ...
applicationVariants.all { variant ->
// create tasks to generate Javadocs
task("generate${variant.name.capitalize()}Javadoc", type: Javadoc) {
source = variant.javaCompile.source
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
// choose the destination that works best for you here
// I chose this particular directory because Jenkins pulls reports
// from this directory already if you need to have the output
// folder be parameterized for the build variant, use
// "build/outputs/docs/javadoc-${variant.name}/" instead and it'll
// be in `javadoc-productionRelease` for example
destinationDir = file("build/outputs/docs/javadoc/")
// the name that will appear in the docs
title = rootProject.name
// you will probably get errors from using the @annotations and
// the support library, so just turn off failing for errors
failOnError false
}
}
// ...
}
回答9:
One more
android.libraryVariants.all { variant ->
if(variant.name.equals('release'))
task("generateJavadoc", type: Javadoc) {
description "Generate Javadoc"
source = android.sourceSets.main.java.srcDirs
// println '=== source ==='
// source.collect { relativePath(it) }.sort().each { println it }
ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
classpath = files(variant.javaCompile.classpath.files) + files(ext.androidJar)
// println '=== classpath ==='
// classpath.collect { relativePath(it) }.sort().each { println it }
}
}
Use:
gradle generateJavadoc