10

Our Gradle build script fails when building Javadocs for Android Studio project for a library we develop, with errors like:

/MY_LOCAL_PATH/MyClass.java:5: error: package com.google.gson does not exist import com.google.gson.Gson;

Details:

We are building an Android library with Gradle, and want to build the Javadocs for it.

The Android Studio project is configured to require a Maven dependency for GSON, although I'm guessing it would happen with every dependency whose lib file is not provided explicitly.

One of our classes, of course, imports com.google.gson.

Here is our Gradle script:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

apply plugin: 'com.android.library'

android {
    compileSdkVersion 19
    buildToolsVersion '21.1.2'
    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 19
        versionCode 8000
        versionName "0.8.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.code.gson:gson:2.2.4'
}

And the task that's expected to build the docs (based on this question):

task generateJavaDocs(type:Javadoc) {
  source = 'src/main/java/com'

  ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
  classpath = files(source) + files(ext.androidJar)

  options.memberLevel = JavadocMemberLevel.PUBLIC
  include '**/*.java'
  exclude '**/BuildConfig.java'
  exclude '**/R.java'
}

When reaching this task, Gradle fails with these errors:

/MY_LOCAL_PATH/MyClass.java:5: error: package com.google.gson does not exist import com.google.gson.Gson;

/MY_LOCAL_PATH/MyClass.java:6: error: package com.google.gson does not exist import com.google.gson.JsonParseException;

/MY_LOCAL_PATH/MyClass.java:7: error: package com.google.gson does not exist import com.google.gson.JsonSyntaxException;

/MY_LOCAL_PATH/MyClass.java:8: error: package com.google.gson.reflect does not exist import com.google.gson.reflect.TypeToken;

Anyone has a thoughts on how to solve this?

Thanks!

Community
  • 1
  • 1
mllm
  • 17,068
  • 15
  • 53
  • 64
  • Useful post with instructions: http://andydyer.org/blog/2014/09/29/delombok-and-javadoc-with-gradle/, but note that he excludes packages that he's got their sources so he can exclude them by their dir. – mllm Mar 09 '15 at 13:33
  • Did using `exclude` work for you? – Jared Burrows Apr 14 '15 at 19:33
  • Did you solve your problem? – Chris.Zou Oct 28 '15 at 04:56
  • 1
    Exclude doesn't work for me. Actually the only workaround I was able to find for this issue, is to add in the classpath for the javadoc gradle task the 'classes.jar' that is generated when my .aar is built. But this is messy and changes the order of the build. Anyone found a cleaner way? I mean this seems a very typical issue to not have a proper solution – atsakiridis Feb 15 '17 at 11:13

1 Answers1

5

I was working on java-library project built with gradle and I had a similar issue with gradle javadocs task:

task javadocs(type: Javadoc) {
  source = sourceSets.main.java.srcDirs
  exclude '**/R.html', '**/R.*.html', '**/index.html'
}

My library dependency was 'org.json:json:20180130' and my javadocs task was failing with error:

error: package org.json does not exist

I fixed it by adding classpath to javadocs task like this:

task javadocs(type: Javadoc) {
  source = sourceSets.main.java.srcDirs
  classpath = sourceSets.main.runtimeClasspath
  exclude '**/R.html', '**/R.*.html', '**/index.html'
}

If you are working with Android plugin instead of standard java library you can get the classpath like this:

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

I hope this helps someone.

Tomislav Kordic
  • 81
  • 2
  • 10
  • ERROR: Could not get unknown property 'runtimeClasspath' for source set main of type com.android.build.gradle.internal.api.DefaultAndroidSourceSet. – PapaWhiskey Apr 10 '20 at 19:28
  • @PapaWhiskey you are probably using android plugin, this example was created for java-library plugin, let me see how it should look like on android. – Tomislav Kordic Apr 11 '20 at 22:06
  • @PapaWhiskey I edited the answer, see if it working for you :-) – Tomislav Kordic Apr 11 '20 at 22:16
  • already have the getBootClasspath...but yes i found this page because of searching for javadoc for android studio...even the OP said "Our Gradle build script fails when building Javadocs for Android Studio project " ... else i'd have not commented...going to play with it some more today and if i get a fix, i'll post – PapaWhiskey Apr 13 '20 at 12:52
  • couldn't edit previous comment, but this solution [link](https://stackoverflow.com/questions/46879499/android-studio-gradle-javadoc-task) is what worked for me overall – PapaWhiskey Apr 16 '20 at 16:12