google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.45k stars 2.02k forks source link

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

Closed ayvazj closed 5 years ago

ayvazj commented 5 years ago

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 commented 5 years ago

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

YuraAAA commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

contrudar commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 4 years ago

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!

ericntd commented 4 years ago

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;
  2. 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 commented 4 years ago

@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 commented 4 years ago

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

tobenna-paystack commented 4 years ago

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

Having same issue with Canary 14

phillipsk commented 3 years ago

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 commented 3 years ago

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

sasfmlzr commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

this add in dependency.

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

miraboh commented 2 years ago

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

AndresBPaz commented 1 year ago

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 commented 1 year ago

Just add this dependency

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

pmatsinopoulos commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

I fixed the issue with

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