使用Android Gradle插件生成JavaDocs

我如何使用新的Gradle构build系统为Android项目生成JavaDocs?

这是我想出来的,但不起作用。

task generateJavadoc(type: Javadoc) { source = android.sourceSets.main.java.srcDirs ext.cp = android.libraryVariants.collect { variant -> variant.javaCompile.classpath.files } classpath = files(ext.cp) } 

主要的问题是我没有在类path上得到合适的android.jar,因此JavaDocs中的一些链接没有parsing。 我必须find一种方法来获得类path中所有必需的jar。

我采取的方法的另一个问题是收集所有构build变体的类path,而不是select一个。

对于Android gradle插件1.1.2+(com.android.tools.build:gradle:1.1.2+)

libraryvariant – 不再工作

使用:

 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 /”) – 在项目目录的根目录下findjavadocs(这样jenkins javadoc插件可以find它并显示在特殊的文档面板中)

failOnError false – 用于抑制可能导致jenkins失败的警告

Gradle 1.11 – Gradle插件0.10.0

android.plugin.sdkDirectoryreplaceandroid.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' } } 

我最终解决的解决scheme如下:

 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确认这是在adt-dev组中做到这一点的方式(注意事项), https: //groups.google.com/forum/#!searchin/adt-dev/javadoc/adt-dev/seRizEn8ICA/bafEvUl6mzsJ 。

使用Android gradle tools 1.10 +获取android SDK dir与以前不同。 您必须更改以下内容:

 android.sdkDirectory 

代替

 android.plugin.sdkDirectory 

这是问题的完整解决scheme:

 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' } } 

Android的jar子似乎在属性android.plugin.runtimeJarList 。 虽然没有logging在任何地方,所以它可能会随时中断。

我已经完善了您的解决scheme以跨越构build变体:

 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' } } 

在主分支上做一个javadoc通常是没有意义的,因为你可能依赖于产品风格中的一些东西。 即使debugging与发布可能有一些差异。 你当然可以select一个默认的variables来使用。 所以你可以做一些像,

 task javadoc(dependsOn: javadocDebug) 

以下是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()}"] } } 

我为此做了一个开源的插件。 GitHub仓库

 buildscript { repositories { maven { url "https://plugins.gradle.org/m2/" } } dependencies { classpath "gradle.plugin.com.vanniktech:gradle-android-javadoc-plugin:0.2.1" } } 

将此行添加到您的build.gradle

 apply plugin: "com.vanniktech.android.javadoc" 

然后执行以下任一操作:

 ./gradlew generateDebugJavadoc ./gradlew generateReleaseJavadoc 

java文档可以在module/javaDoc/

我发现这个解决scheme在Gradle插件1.3.1上工作,如果你有不同的产品口味。

这将创buildGradle任务来为每个产品风格和构buildtypes生成Javadoc。 例如,如果模块名称是app并且您有productiondev产品风格以及debugrelease构buildtypes,那么您将拥有以下Gradle任务:

  • :应用:generateDevDebugJavadoc
  • :应用:generateDevReleaseJavadoc
  • :应用:generateProductionDebugJavadoc
  • :应用: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 } } // ... } 

多一个

 android.libraryVariants.all {variant  - >
    如果(variant.name.equals( '释放'))
        任务(“generateJavadoc”,types:Javadoc){
            描述“生成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)+文件(ext.androidJar)
 // println'=== classpath ==='
 // classpath.collect {relativePath(it)} .sort()。each {println it}
         }
 }

使用:

gradle生成Javadoc