12

I'm trying to prepare and upload my Android library to Bintray and part of that process runs the following javadoc task:

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

This task is part of a larger gradle script here: https://raw.githubusercontent.com/attwellBrian/JCenter/master/bintrayv1.gradle

When the javadoc task runs, the following problems occur:

  1. Every @NonNull and @Nullable annotation in the project reports an error of "error: cannot find symbol"
  2. Every Javadoc reference I've written for an Android class, like {@link Toolbar}, reports an error of "error: reference not found"

How can I correct these reference issues when generating Javadocs?

EDIT It looks like its not all Android class links that are creating an issue, it may just be classes that come from the Android support library (which is also where the annotations come from). Does something special need to be done to link to source files in gradle dependencies?

SuperDeclarative
  • 1,479
  • 3
  • 16
  • 31

2 Answers2

30

You should also add all your dependency to the javadoc.classpath. Try this:

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

afterEvaluate {
    javadoc.classpath += files(android.libraryVariants.collect { variant ->
        variant.javaCompileProvider.get().classpath.files
    })
}
swooby
  • 2,903
  • 2
  • 34
  • 43
xkor
  • 607
  • 5
  • 11
  • 2
    Welcome to stackoverflow, please dont just post code but also explain what your code is doing and why its working. – LJᛃ Jan 03 '16 at 01:55
  • Can you explain why we have to add those files in "afterEvaluate" instead of adding them directly in the "javadoc()" task? – SuperDeclarative Jan 03 '16 at 03:08
  • Unfortunaly the javadoc task's body is called before `android.libraryVariants` is filled. So we have to wait until `android.libraryVariants` is filled. – xkor Jan 03 '16 at 15:42
  • 2
    Another module in the same project cannot be added as a dependency using above solution. I am facing that problem in my case, Anyone has any idea? – Vignesh Sundaramoorthy Dec 10 '16 at 11:35
  • Necroing here, but this outputs `WARNING: API 'variant.getJavaCompile()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()'. It will be removed at the end of 2019.` I tried replacing `javaCompile` with `javaCompileProvider`, but that fails with `Could not get unknown property 'classpath' for provider(task compileDebugJavaWithJavac, class com.android.build.gradle.tasks.AndroidJavaCompile) of type org.gradle.api.internal.tasks.DefaultTaskContainer$TaskCreatingProvider_Decorated.` Any other ideas, or just ignore it? – swooby Mar 19 '19 at 19:12
  • I know this is 3 years old, but why does including the build variants solve the annotation errors the original poster had? – Belphegor Jul 09 '19 at 23:26
  • 1
    @swooby you should replace `javaCompile` with `javaCompileProvider.get()`. So the full line should be like this: `variant.javaCompileProvider.get().classpath.files`. Then it works without errors. – mfb Jun 04 '20 at 09:38
0

So these error mean that JavaDoc can't link the classes which are not in the same module (without further config). If you don't care about this (ie. having hyperlinks to classes outside of the module) you could just ignore the errors with

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

See here for more info on the Javadoc Task

Patrick Favre
  • 31,961
  • 9
  • 105
  • 121