Android Gradle Javadoc broken after upgrade to Gradle 7

Viewed 755

A few days ago I upgraded to the new Android Studio Arctic Fox release, which also now requires Gradle 7

Prior to the upgrade, I had the following javadoc task, which worked perfectly

def javaDocsAllowList = [
    "com/mycompany/SomeFile.java",
    "com/mycompany/OtherFile.java",
    ...
]

task javadocs(type: Javadoc) {
    source android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) + configurations.compile
    include javaDocsAllowList
    destinationDir = file("./docs/javadoc")
}

Now though, it's all broken.

First error was

Could not get unknown property 'compile' for configuration container of type org.gradle.api.internal.artifacts.configurations.DefaultConfigurationContainer

so I change the classpath line to:

classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) + configurations.implementation

Then I get

Resolving dependency configuration 'implementation' is not allowed as it is defined as 'canBeResolved=false'.

so I add

configurations.implementation.setCanBeResolved(true)

THEN I get a bunch of errors that it cannot find symbol for @Nullable and other attributes.

Other stackoverflow answers suggest adding this, which I do:

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

This fixes the errors resolving @NonNull, etc, but now I am left with a bunch of errors where JavaDoc cannot resolve my own code, such as:

void addNotificationsListener(@NonNull NotificationsListener listener); (cannot find symbol NotificationsListener)

These seem to be reduced when I add more files to the AllowList, but I explicitly do not want to generate documentation for these other files. Help!

Why did Gradle 7 break all the JavaDocs and how can I fix it?

(recap: this is my failing task:)

task javadocs(type: Javadoc) {
    source android.sourceSets.main.java.srcDirs
    configurations.implementation.setCanBeResolved(true)

    classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) + configurations.implementation
    include javaDocsAllowList
    destinationDir = file("./docs/javadoc")

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

Update: based on suggested gist https://gist.github.com/Robyer/a6578e60127418b380ca133a1291f017?, I changed my task to the following:

task javadocs(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    android.libraryVariants.all { variant ->
        if (variant.name == 'release') {
            owner.classpath += variant.javaCompileProvider.get().classpath
        }
    }
    include javaDocsAllowList
    destinationDir = file("./docs/javadoc")
}

However this makes no difference; it still gets the same error where it cannot resolve my own code

void addNotificationsListener(@NonNull NotificationsListener listener); (cannot find symbol NotificationsListener)`

1 Answers

I am currently using gradle version 7.0.2 and Android Studio Arctic Fox | 2020.3.1 Patch 2, and to generate sourcesJar, javadocs and javadocJar, I use:

task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    archiveClassifier.set('sources')
    archivesBaseName ="singleAdapter-1.1.0"
}
task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.sourceFiles
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    android.libraryVariants.all { variant ->
        if (variant.name == 'release') {
            owner.classpath += variant.javaCompileProvider.get().classpath
        }
    }
    options.memberLevel = JavadocMemberLevel.PRIVATE
}
task javadocJar(type: Jar, dependsOn: javadoc) {
    archiveClassifier.set('javadoc')
    archivesBaseName = "singleAdapter-1.1.0"
    from javadoc.destinationDir
}

Finally, the resulting files can be found at:

  1. project view -> myModule/build/docs/javadoc - for javadoc
  2. project view -> myModule/build/docs/libs - for sourcesJar and javadocJar

I've posted it on my Github Gist: Here

I hope that in your case I can help you.

Related