Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle Javadoc fails with "error: package ... does not exist"

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!

like image 366
mllm Avatar asked Mar 09 '15 13:03

mllm


Video Answer


1 Answers

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.

like image 132
Tomislav Kordic Avatar answered Oct 09 '22 14:10

Tomislav Kordic