33

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?

crgarridos
  • 8,758
  • 3
  • 49
  • 61

6 Answers6

29

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'
Adnan Abdollah Zaki
  • 4,328
  • 6
  • 52
  • 58
27

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.

TooCool
  • 10,598
  • 15
  • 60
  • 85
Kiskae
  • 24,655
  • 2
  • 77
  • 74
  • 11
    Effectively I was running `./gradlew` with the system jdk(java9). Using `./gradlew compileDebugAndroidTestKotlin -Dorg.gradle.java.home=` solved the proble. Thanks :) – crgarridos Nov 07 '18 at 10:08
  • This is a good distinction - building using the play button worked because the IDE was using the JDK included as part of Android Studio. Thank you – dazza5000 Feb 20 '19 at 17:13
  • @crgarridos you sir saved me HOURS! I love you! – noloman Mar 12 '19 at 16:27
  • 3
    how to set the jre path without using the command line parameter: https://stackoverflow.com/a/21212790/1363742 – luca992 Jul 23 '19 at 23:16
  • I have to say, thank you, this response saved my butt!! – Travis Sep 14 '22 at 17:18
14

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:

AliSh
  • 10,085
  • 5
  • 44
  • 76
2

change to embeded Android studio version and only clean!(not rebuild) Improove @kiskae answer

Fortran
  • 2,218
  • 2
  • 27
  • 33
1

I also got "package javax.annotation.processing does not exist import javax.annotation.processing.Generated" in Android Studio while trying to build the project with Gradle. Due to some reasons I cannot upgrade any dependencies in the project. What worked for me in this case:

  1. not only install Java 8 instead of any higher Java versions you have (you can download it from here, for example: https://www.oracle.com/ru/java/technologies/javase/javase-jdk8-downloads.html),
  2. but also check out Project Structure -> SDK location -> JDK location and make sure that this folder really contains jdk. The right option to choose can start with JAVA_HOME, for instance.

My case was that after some update this location was set by default as embedded JDK which did not contain any jdk and as a quite newbie to Android I have spent a couple of beautiful hours trying to figure out what's wrong with my Java 8.

1

In my case I change "Gradle JDK" and it's work perfectly.

Go to "File ==> project structure ==> SDK Location ==> Gradle settings ==> then change the selected gradle JDK from the drop down menu "

Hope this will help you.