'Javadoc and Android studio : Cannot find symbol

I am currently working an Android app (on Android Studio, using mainly Java).

I want to generate javadoc (using the Tools -> Generate menu).

Following this other post (Android Studio Javadoc: Cannot find symbol), I added the following code to my build.gradle :

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

Unfortunately this does not work. When I try to generate the javadoc, I have the following errors (and a lot more) :

error: cannot find symbol
import android.Manifest;
...
error: package android.compat.annotation does not exist
import android.compat.annotation.UnsupportedAppUsage;
...
error: package android.compat.annotation does not exist
import android.compat.annotation.UnsupportedAppUsage;
...

I tried adding the following to the gradle file :

afterEvaluate {
    javadoc.classpath += files(android.libraryVariants.collect { variant ->
        variant.javaCompileProvider.get().classpath.files
    })
}

But with this added, I get an error when pressing sync gradle :

> Could not get unknown property 'libraryVariants' for extension 'android' of type com.android.build.gradle.internal.dsl.BaseAppModuleExtension.


Solution 1:[1]

I came along the same way and after a while I figured out that it's because I'm not developing a library, but an application. Consequently, you just exchange libraryVariants for applicationVariants.

afterEvaluate {
    javadocs.classpath += files(android.applicationVariants.collect { variant ->
        variant.javaCompileProvider.get().classpath.files
    })
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tumble