Cannot find symbol class "Generated" for Dagger 2

19,765

Solution 1

Read this for more info: https://github.com/google/dagger/issues/95

Basically, the solution is to do what you've already done which is include the glassfish javax annotation library.

Solution 2

TL;DR use Dagger >= 2.1

Alex is right, but it's better to add JSR250 dependency instead of GlassFish

provided 'javax.annotation:jsr250-api:1.0'

or for the latest gradle plugin:

compileOnly 'javax.annotation:jsr250-api:1.0'

Solution 3

This happens if your JAVA_HOME points to JAVA version 9 or 10. Switching JAVA_HOME to Java 8 fixes the problem and you will not need that extra dependency.

Solution 4

I downgraded my JVM to Java 8 and running gradle build was successful in my Android application using Dagger 2.

Share:
19,765

Related videos on Youtube

x-treme
Author by

x-treme

Updated on November 13, 2020

Comments

  • x-treme
    x-treme over 3 years

    I just started doing dependency injection using Dagger 2. When I spun up my modules, components and tried to build my application, gradle threw the error

    Error:(4, 24) error: cannot find symbol class Generated

    I dug into it and found that the error is in one of the classes Dagger generates to do DI. The particular class that's missing was javax.annotation.Generated and the line throwing the error is the line that anntotates a Dagger generated class as @Generated("dagger.internal.codegen.ComponentProcessor")

    This question helped in finding the solution which is to add the javax package as a dependency by adding the line compile 'org.glassfish:javax.annotation:10.0-b28' to my gradle build file. This led to a successful build.

    My question is, why is that not added as a transitive dependency for Dagger or why hasn't anyone else faced this particular issue (I assume so, since I couldn't find any question here regarding this?

    • FrozenCow
      FrozenCow about 9 years
      I had the same problem. I 'solved' it by adding: compile 'javax.annotation:jsr250-api:1.0' I'm not sure why this is still a problem nor whether this is the right solution.
    • Alex Fu
      Alex Fu about 9 years
      For more info on this matter, you can read the thread here: github.com/google/dagger/issues/95
    • x-treme
      x-treme about 9 years
      @AlexFu - Great!! Do you mind posting it as an answer ?
  • filthy_wizard
    filthy_wizard over 8 years
    still cant recognise my generated "Dagger" prefix component?. it was doing it a minute ago! lol
  • IgorGanapolsky
    IgorGanapolsky over 8 years
    Why is jsr better than glassfish annotation?
  • tomrozb
    tomrozb over 8 years
    @IgorGanapolsky GlassFish is an application server, so the annotation library is strictly for its purposes. JSR250 is just a standard and contains only some annotations for general purpose use.
  • IgorGanapolsky
    IgorGanapolsky over 8 years
    Then why are other android developers using Glassfish in their gradle builds? Is there evidence that it is inferior and android devs should stay away from it?
  • tomrozb
    tomrozb over 8 years
    @IgorGanapolsky because this is just an annotation library so you can use it in any project. JSR250 is just a transitive dependency in this library, you can check it here: central.maven.org/maven2/org/glassfish/javax.annotation/3.1.‌​1/… For sure you don't need javaee-api annotations in your Android project, but it's also the dependency of the GlassFish annotation library.
  • IgorGanapolsky
    IgorGanapolsky over 8 years
    @tomrozb So I should use both in my gradle build?
  • tomrozb
    tomrozb over 8 years
    @IgorGanapolsky definitely not. Just use one in Android projects.
  • fruqi
    fruqi almost 7 years
    This solution saves my time.
  • Achmad Naufal Syafiq
    Achmad Naufal Syafiq over 6 years
    got this issue in Dagger 2.14
  • Amir Ziarati
    Amir Ziarati almost 6 years
    Configuration 'provided' is obsolete and has been replaced with 'compileOnly'
  • Wahib Ul Haq
    Wahib Ul Haq over 5 years
    @AchmadNaufalSyafiq how did you fix it? I am facing same in Dagger 2.16
  • LiuWenbin_NO.
    LiuWenbin_NO. over 2 years
    This works for me.