Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Generated annotation using gradlew + dagger

I'm confronting a weird issue using gradlew(4.10.2) + dagger(2.18).

The problem is when I call:

./gradlew :app:compileDebugAndroidTestKotlin 

The build fails with:

Task :Common:compileDebugJavaWithJavac FAILED

/CommonModule_ProvidesGsonFactory.java:6: error: package javax.annotation.processing does not exist import javax.annotation.processing.Generated;

/CommonModule_ProvidesGsonFactory.java:8: error: cannot find symbol @Generated(

But if I run the task from Android Studio, the task succeed and the @Generated is not present in the dagger generated class.

Do you have some clue to avoid the @Generated annotation using ./gradlew?

like image 536
crgarridos Avatar asked Nov 07 '18 09:11

crgarridos


3 Answers

Dagger uses auto-common's GeneratedAnnotations to figure out which @Generated annotation to use. It does this based on the classpath.

What this means is that while the annotation processor is running, javax.annotation.processing.Generated is available and used in the generator, but when the resulting file is compile in another task, it is no longer on the classpath.

Potential causes could be that code was generated using a newer version of the JDK and the incremental build is invalid, requiring a full "clean & rebuild" or there is a problem with java language levels where the annotation processor runs on Java 9+, but android is compiled as Java 8 or lower.

Using ./gradlew compileDebugAndroidTestKotlin -Dorg.gradle.java.home=<Android studio jre path> solved the problem.

like image 143
Kiskae Avatar answered Oct 11 '22 00:10

Kiskae


if you have :

javax annotation does not exist

I have this issue on my macOS this error occures because your jdk is above of 1.8

just add below code on build.gradle of your apps .

//Resolve jdk8+ Generation Annotations - javax annotation does not exist
compileOnly 'com.github.pengrad:jdk9-deps:1.0'
like image 42
Adnan Abdollah Zaki Avatar answered Oct 11 '22 01:10

Adnan Abdollah Zaki


I had this problem when I updated my Android Studio to 4.2

The problem was solved when I upgrade the Kotlin version from 1.3.51 to 1.4.31

It seems the problem is reported and solved in this issue:

  • https://github.com/google/dagger/issues/1449
  • https://youtrack.jetbrains.com/issue/KT-32804
like image 29
AliSh Avatar answered Oct 11 '22 01:10

AliSh