10

I can not find any documentation on how to configure my Gradle file to create the JavaDoc for my project. I already tried some snippets from SO and blog articles but none of them seem to work or they do not provide context.

This is my Gradle file. What do I need to do to add a task to generate JavaDoc?

import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'com.android.library'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
}
Jan Deinhard
  • 19,645
  • 24
  • 81
  • 137

2 Answers2

11

In our projects we added the following to the app's build.gradle:

android.applicationVariants.all { variant ->
  task("generate${variant.name.capitalize()}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/");
  }
}

This will add tasks to the build of the form generate<build_variant>Javadoc. So let's say you have a free and a pro version of your app, this would add tasks like:

generateFreeDebugJavadoc
generateFreeReleaseJavadoc
generateProDebugJavadoc
generateProReleaseJavadoc

Hope this helps

Fred
  • 16,367
  • 6
  • 50
  • 65
  • i get a weird error trying this : `Error:(77, 0) Could not find property 'applicationVariants' on com.android.build.gradle.LibraryExtension_Decorated@19be5795.` - any thoughts? – trippedout Apr 28 '15 at 16:39
  • 6
    2 things - 1. The ``applicationVariants`` only works for the app plugin, i.e., if you're building a library it would be ``libraryVariants``. 2. Check your android gradle plugin version. It might be that it's not up to date or it's too recent and this changed as well. – Fred Apr 29 '15 at 07:27
  • When running this initially, I got errors regarding the R.** files. Added the following "exclude '**/BuildConfig.java" and "exclude '**/R.java'" right after the class path. – Dan Devine May 15 '15 at 22:43
2

In android studio, write a gradle task of type Javadoc :

 task createJavadocs (type: Javadoc)
  {
        source = project.android.sourceSets.main.java.srcDirs
        options.linkSource true
        classpath += project.files(project.android.getBootClasspath().join(File.pathSeparator))
        failOnError false
  }

Simply execute this task will create javadocs.

sver
  • 866
  • 1
  • 19
  • 44