Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 450
Cheborra Avatar asked Apr 16 '15 01:04

Cheborra


4 Answers

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
like image 121
Markus Kreth Avatar answered Nov 15 '22 13:11

Markus Kreth


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.

like image 44
Louis CAD Avatar answered Nov 15 '22 13:11

Louis CAD


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.

like image 6
stare.in.the.air Avatar answered Nov 15 '22 11:11

stare.in.the.air


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.

like image 2
Sufian Avatar answered Nov 15 '22 12:11

Sufian