dagger: @Generated not found *javax.annotation.processing.Generated*

I am seeing this issue when trying to compile an Android project with Dagger2 (Google I/O 2018 sample app).

package javax.annotation.processing does not exist
import javax.annotation.processing.Generated;

I have tried the several solutions mentioned in the issues and stack overflow for resolving javax.annoation.Generated but not having any luck. Note the annotation is in the javax.annotation.processing package.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 37
  • Comments: 40

Commits related to this issue

Most upvoted comments

Unfortunately, it doesn’t work for android projects. Any tip how we can compile android, jdk9+ and dagger?

Manually create Generated interface in your project. There could be package exists in another module: java.compiler error if adding in android app module, but creating in android-library module (and then adding it to main via implementation project(':module-generated')) works fine.

I’ve uploaded package with such class to JCenter https://github.com/pengrad/jdk9-deps Now it’s possible to just add this gradle dependency and it compiles with JDK11: compileOnly 'com.github.pengrad:jdk9-deps:1.0'

My issue turned out to be related to JDK9 - explicitly using JDK 1.8 resolved the issue for me. ¯\_(ツ)_/¯

Add the following to gradle.properties

org.gradle.java.home={PATH TO JAVA 8}

Note setting JAVA_HOME did not work for me

Had this issue with Android Studio 4.2. Tried solutions from above and what finally worked was to update Kotlin from 1.3.50 to 1.4.31.

I am using (for Android)

compileOptions {
  sourceCompatibility JavaVersion.VERSION_1_8
  targetCompatibility JavaVersion.VERSION_1_8
}

I have also manually tried setting JAVA_HOME to jdk1.8 and jdk9 - same result.

I recently had this issue on a project when migrating to java 11. I figured out the issue was caused by me using an outdated version of assisted-inject. I made it work by updating dagger to 2.35.1 and both of my assisted-inject dependencies to 0.8.1:

compileOnly 'com.squareup.inject:assisted-inject-annotations-dagger2:0.8.1'
kapt 'com.squareup.inject:assisted-inject-processor-dagger2:0.8.1'

This

Also, make sure to update gradle plugin to 4.2.0 according to this https://developer.android.com/studio/releases/gradle-plugin#4-2-0

And this, did it for me on Android Studio 4.2.1. Thank you very very very much.

The issue seems to have gone away with Android Studio 4.2 Canary 14 @MichalDanielDobrzanski

Having same issue with Canary 14

My issue turned out to be related to JDK9 - explicitly using JDK 1.8 resolved the issue for me. ¯_(ツ)_/¯

Add the following to gradle.properties

org.gradle.java.home={PATH TO JAVA 8}

Note setting JAVA_HOME did not work for me

just in case, if you don’t want to put it to gradle.properties, you can use gradlew TASK_NAME -Dorg.gradle.java.home="C:\Program Files\Android\Android Studio\jre" . This is useful for CI or if several people work on the same project.

I recently had this issue on a project when migrating to java 11. I figured out the issue was caused by me using an outdated version of assisted-inject. I made it work by updating dagger to 2.35.1 and both of my assisted-inject dependencies to 0.8.1:

compileOnly 'com.squareup.inject:assisted-inject-annotations-dagger2:0.8.1'
kapt 'com.squareup.inject:assisted-inject-processor-dagger2:0.8.1'

Also, make sure to update gradle plugin to 4.2.0 according to this https://developer.android.com/studio/releases/gradle-plugin#4-2-0

@ericntd I am facing the same problems on Android Studio 4.2 Preview and Dagger 2.22.

I was not able to use this com.github.pengrad:jdk9-deps:1.0 external dependency inside my project - gradle complained that javax annotation is still missing.

I was trying with: compileOnly("javax.annotation:jsr250-api:1.0") dependency, which provide official JSR250 annotations, but with no luck.

with the stable Android Studio 4.2, I have the same problem with Dagger 2.25, after I updated dagger to latest version, the problem solved

this add in dependency.

// implementation ‘javax.annotation:javax.annotation-api:1.3.2’

4.2 preview same issue. if i use 4.0 works…whats the issue?

Fwiw, with 2.14 (using auto-common 0.9), Dagger will use @javax.annotation.processing.Generated unless --release 8 is used (in which case @javax.annotation.Generated will be used instead):

tasks.withType(JavaCompile) {
  options.compilerArgs += [ "--release", "8" ]
}

From 2.15 onward (using auto-common 0.10), Dagger will also respect -source 8 -target 8, i.e. Gradle’s sourceCompatibility and targetCompatibility.

I fixed the issue with

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlin {
        jvmToolchain(8)
    }

Just add this dependency

implementation ‘javax.annotation:javax.annotation-api:1.3.2’

Unfortunately, it doesn’t work for android projects. Any tip how we can compile android, jdk9+ and dagger?

Manually create Generated interface in your project. There could be package exists in another module: java.compiler error if adding in android app module, but creating in android-library module (and then adding it to main via implementation project(':module-generated')) works fine.

I’ve uploaded package with such class to JCenter https://github.com/pengrad/jdk9-deps Now it’s possible to just add this gradle dependency and it compiles with JDK11: compileOnly 'com.github.pengrad:jdk9-deps:1.0'

I have this issue this morning and after spent hours on trying different things and Java versions, this fixed my problem. So, add compileOnly 'com.github.pengrad:jdk9-deps:1.0' to my build.gradle files fix the issue.

I get this on a very under-developed project, fix was to update Dagger from 2.14.1 to 2.25.4 (before Android X as my project is not upgraded yet). About time to do that methinks.

4.2 preview same issue. if i use 4.0 works…whats the issue?

If you are updating your android studio and using arctic fox (2020.3.1) don’t forget to update the gradle plugin. classpath "com.android.tools.build:gradle:7.0.0-alpha14"

My issue turned out to be related to JDK9 - explicitly using JDK 1.8 resolved the issue for me. ¯_(ツ)_/¯

Add the following to gradle.properties

org.gradle.java.home={PATH TO JAVA 8}

Note setting JAVA_HOME did not work for me

Thank you. I checked my environment and found jdk11 and jdk8. Uninstall and reinstall only jdk8 removed this error.