Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Generated not found *javax.annotation.processing.Generated* #1339

Closed
ayvazj opened this issue Nov 19, 2018 · 40 comments
Closed

@Generated not found *javax.annotation.processing.Generated* #1339

ayvazj opened this issue Nov 19, 2018 · 40 comments

Comments

@ayvazj
Copy link

ayvazj commented Nov 19, 2018

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.

@ronshapiro
Copy link

Are you using -source 9 in your compilation (perhaps implicitly)? What do you get if you run javac -version?

@YuraAAA
Copy link

YuraAAA commented Nov 19, 2018

Are you using -source 9 in your compilation (perhaps implicitly)? What do you get if you run javac -version?

Same issue on my teamcity CI server (clean assembleDebug).

javac -version
javac 1.8.0_181

But on local PC works fine

$ javac -version
javac 1.8.0_162

Dependencies:

implementation "com.google.dagger:dagger:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
kapt "com.google.dagger:dagger-android-processor:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version"

where
dagger_version = '2.16'

Error

[Step 1/2] :app:compileDebugJavaWithJavac

package javax.annotation.processing does not exist
[19:54:12][:app:compileDebugJavaWithJavac] import javax.annotation.processing.Generated;
...
 error: cannot find symbol
[19:54:12][:app:compileDebugJavaWithJavac] @Generated

@ayvazj
Copy link
Author

ayvazj commented Nov 19, 2018

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.

@ayvazj
Copy link
Author

ayvazj commented Nov 20, 2018

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

@YuraAAA
Copy link

YuraAAA commented Nov 20, 2018

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.

@morymmm
Copy link

morymmm commented Nov 21, 2018

I am seeing the same issue when trying to compile with JDK10:

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

@tbroyer
Copy link

tbroyer commented Nov 21, 2018

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.

@morymmm
Copy link

morymmm commented Nov 27, 2018

I have created a simple android application written in kotlin with an empty dagger component:

@Component
interface AppComponent { }
apply plugin: 'kotlin-kapt'

dependencies {
    implementation 'com.google.dagger:dagger:2.19'
    kapt 'com.google.dagger:dagger-compiler:2.19'
}

When trying to compile with dagger 2.19 and JDK10 dagger adds javax.annotation.processing.Generated annotation to DaggerAppComponent class and compilation fails with:

error: package javax.annotation.processing does not exist
error: cannot find symbol @Generated

Compiling with JDK8 works fine. There is no @generated annotation added. When I switch to dagger 2.13 it does not add @generated for both JDK10 and JDK8.

@contrudar
Copy link

there is no way to make it work with Java 11?

@contrudar
Copy link

contrudar commented Dec 18, 2018

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.

@almozavr
Copy link

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

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

@pengrad
Copy link

pengrad commented Feb 10, 2019

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'

@ronshapiro
Copy link

Let's converge on #1449 which seems to be the same, and advanced pretty quickly. In particular it seems like there's a kapt bug that will be linked there.

@pvthiendeveloper
Copy link

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'

It work for me. Thank you so much!

AlexMiniApps referenced this issue in AlexMiniApps/cordova-plugin-video-editor Sep 15, 2020
@ericntd
Copy link

ericntd commented Sep 28, 2020

Thanks a lot @pengrad , I think I'm very close.
Having the same issue with Intellij 2020.* and Android Studio 4.2 Preview and Dagger 2.22

My team doesn't approve the external dependency of com.github.pengrad:jdk9-deps:1.0 so I went ahead and create a "Android Library" project called "javax-generated" with 1 single class like the one in your repo:

package javax.annotation.processing;
public @interface Generated {
    String[] value();

    String date() default "";

    String comments() default "";
}

It's all well and good in my library modules, however, it's not working for my :app module:

  1. implementation(project(':javax-generated')) gives
e: /Users/ericn/AndroidStudioProjects/Folder/Project/app/build/generated/not_namespaced_r_class_sources/debug/processDebugResources/r/javax/annotation/processing/R.java:7: error: package exists in another module: java.compiler
package javax.annotation.processing;
  1. compileOnly(project(':javax-generated')) gives
Android dependency 'project :javax-generated' is set to compileOnly/provided which is not supported

Any idea what I'm missing?

@MichalDanielDobrzanski
Copy link

@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.

@ericntd
Copy link

ericntd commented Oct 21, 2020

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

@tobenna-paystack
Copy link

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

Having same issue with Canary 14

jongerrish added a commit to jongerrish/rules_kotlin that referenced this issue Nov 23, 2020
processors generate source that matches the target version.

For example see: google/dagger#1339
restingbull pushed a commit to bazelbuild/rules_kotlin that referenced this issue Nov 24, 2020
* Fix breakages in experimental_use_java_builder flow.
Make examples/android use custom toolchain with both experimental_use_abi_jars and experimental_use_java_builder to validate this flow.

* Pass Java source version for KAPT. This is required such that annotation
processors generate source that matches the target version.

For example see: google/dagger#1339

* ABI classes should be written to a separate folder to avoid polluting the ABI jar with non-ABI classes

* Maintain mnemonic to distinguish between KotlinKapt + KotlinCompile (Both belong to the same worker group)

* Add default java + kotlin compiler options
@phillipsk
Copy link

Also having issues with the Canary release, instead installed the stable AS release v4.1 at the time of writing and since have no issues with the stable AS

@techker
Copy link

techker commented Apr 21, 2021

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

@sasfmlzr
Copy link

sasfmlzr commented Apr 22, 2021

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"

@pandelisgreen13
Copy link

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

@rathorerahul586
Copy link

rathorerahul586 commented May 5, 2021

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

@pandelisgreen13 are you using the room library in the project?

@appscore-ahmed
Copy link

Updating the dagger version doesnt work for me. Is there any other way to resolve this issue? I have explicitly selected Source Compatibility and Target Compatibility as 1_8, while my mac has java 9 installed.

@pandelisgreen13
Copy link

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

@pandelisgreen13 are you using the room library in the project?

Yes, we have Room
With the modules that they had the problem with dagger, I had to set Java 8 at gradle

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

@rathorerahul586
Copy link

rathorerahul586 commented May 5, 2021

me too using the same but getting issue

Execution failed for task ':app:kaptAppNameKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
   > java.lang.reflect.InvocationTargetException (no error message)

at last, downloaded the previous version 4.1.3 after wasting too much time.

@eagskunst
Copy link

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.

@VuXuanBach
Copy link

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

@lucianbc
Copy link
Member

lucianbc commented May 8, 2021

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'

@danielwilson1702
Copy link

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.

@laperezh
Copy link

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.

@Sanaebadi97
Copy link

I was using Java 11 and I change it to 1.8 and this issue fixed.

  compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

@joaodevsantos
Copy link

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.

@swetab2
Copy link

swetab2 commented Apr 3, 2022

this add in dependency.

// implementation 'javax.annotation:javax.annotation-api:1.3.2'

richmowd411 added a commit to richmowd411/Rules-Kotlin that referenced this issue Apr 12, 2022
* Fix breakages in experimental_use_java_builder flow.
Make examples/android use custom toolchain with both experimental_use_abi_jars and experimental_use_java_builder to validate this flow.

* Pass Java source version for KAPT. This is required such that annotation
processors generate source that matches the target version.

For example see: google/dagger#1339

* ABI classes should be written to a separate folder to avoid polluting the ABI jar with non-ABI classes

* Maintain mnemonic to distinguish between KotlinKapt + KotlinCompile (Both belong to the same worker group)

* Add default java + kotlin compiler options
@miraboh
Copy link

miraboh commented Apr 20, 2022

Close ide, navigate to project folder, delete build folder(contains generated file etc), then launch project in ide again, that solve the issue.

@AndresBPaz
Copy link

Thanks a lot @pengrad , I think I'm very close. Having the same issue with Intellij 2020.* and Android Studio 4.2 Preview and Dagger 2.22

My team doesn't approve the external dependency of com.github.pengrad:jdk9-deps:1.0 so I went ahead and create a "Android Library" project called "javax-generated" with 1 single class like the one in your repo:

package javax.annotation.processing;
public @interface Generated {
    String[] value();

    String date() default "";

    String comments() default "";
}

It's all well and good in my library modules, however, it's not working for my :app module:

  1. implementation(project(':javax-generated')) gives
e: /Users/ericn/AndroidStudioProjects/Folder/Project/app/build/generated/not_namespaced_r_class_sources/debug/processDebugResources/r/javax/annotation/processing/R.java:7: error: package exists in another module: java.compiler
package javax.annotation.processing;
  1. compileOnly(project(':javax-generated')) gives
Android dependency 'project :javax-generated' is set to compileOnly/provided which is not supported

Any idea what I'm missing?

I tried it in spring boot with java 1.8 and it doesn't let me run the project after including it in my clashpath

@muneeb-saadii
Copy link

Just add this dependency

implementation 'javax.annotation:javax.annotation-api:1.3.2'

@pmatsinopoulos
Copy link

me too using the same but getting issue

Execution failed for task ':app:kaptAppNameKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
   > java.lang.reflect.InvocationTargetException (no error message)

at last, downloaded the previous version 4.1.3 after wasting too much time.

@rathorerahul586 .... previous version of what?

@rathorerahul586
Copy link

me too using the same but getting issue

Execution failed for task ':app:kaptAppNameKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
   > java.lang.reflect.InvocationTargetException (no error message)

at last, downloaded the previous version 4.1.3 after wasting too much time.

@rathorerahul586 .... previous version of what?

Android Studio

@baderkhane
Copy link

I fixed the issue with

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests