Cheborra
Cheborra

Reputation: 2687

Android Gradle javadoc annotation does not exists

I'm trying to build an aar to publish on jcenter.

The assembleRelease task works ok, the javadoc task also works fine, but the javadocJar task outputs this error:

/Users/martinmoreno/Projects/android-dev-utils/dev-utils/src/main/java/com/tinchoapps/devutils/BitmapUtils.java:11: error: package android.support.annotation does not exist
import android.support.annotation.NonNull;

Here is the (simplified) gradle file:

apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'


android {
    ...
    buildTypes {
        release {
            minifyEnabled false
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.debug
        }
    }
...
}

...

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:support-annotations:20.0.0'
    compile 'com.android.support:support-v4:22.0.0'
}

task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier = 'sources'
}

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

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

artifacts {
    archives sourcesJar
    archives javadocJar
}

It seems related to the dependencies because it's giving some "class not found" exceptions too on classes inside the imports, but can't figure out what's wrong.

Any thoughts?

Upvotes: 25

Views: 9931

Answers (5)

stare.in.the.air
stare.in.the.air

Reputation: 271

Since the introduction of the api and implementation dependency configuration in Android Gradle 3.0.0, compile is deprecated. To include an implementation dependency in the javadoc classpath, I updated Loius CADs answer:

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

I don't expect this to be the cleanest solution possible. There probably is a reason why configuration.implementation is not resolvable by default.

Upvotes: 6

Squeazer
Squeazer

Reputation: 1254

Following from Markus's answer, this is how I had to do it to make it work:

configurations {
    javadocDeps
}

dependencies {
    compile 'com.android.support:support-annotations:25.3.1'
    javadocDeps 'com.android.support:support-annotations:25.3.1'
}

tasks.whenTaskAdded { task ->
    if (task.name == 'androidJavadocs') {
        task.configure {
            classpath += configurations.javadocDeps
        }
    }
}

Upvotes: 1

Louis CAD
Louis CAD

Reputation: 11529

Just add this line in your javadoc task (notice the last part: + configurations.compile):

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

The last part of the line makes sure the javadoc use the compile dependencies to resolve the classes it's using for javadoc. At this point, it shouldn't fail anymore.

Upvotes: 11

Sufian
Sufian

Reputation: 6555

I've faced this while running ./gradlew install. It was occurring when JavaDocs were being compiled (as far as I can understand).

I just added failOnError false to task javadoc.

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

What I understand is that we avoided failure on warnings that annotation library couldn't be found.

Upvotes: 2

Markus Kreth
Markus Kreth

Reputation: 748

This fixed various similar errors for me:

Add the following to build.gradle:

configurations {
    javadocDeps
}

dependencies {
    compile 'com.android.support:support-annotations:22.2.0'
    javadocDeps 'com.android.support:support-annotations:22.2.0'
    androidTestCompile 'junit:junit:4.+'
    androidTestCompile 'com.jayway.android.robotium:robotium-solo:+'
}

In the JavaDoc Task add this line:

classpath += configurations.javadocDeps

Upvotes: 18

Related Questions