26

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?

Community
  • 1
  • 1
x-treme
  • 1,606
  • 2
  • 21
  • 39
  • 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. – FrozenCow May 01 '15 at 13:11
  • For more info on this matter, you can read the thread here: https://github.com/google/dagger/issues/95 – Alex Fu May 05 '15 at 12:17
  • @AlexFu - Great!! Do you mind posting it as an answer ? – x-treme May 05 '15 at 17:33

5 Answers5

32

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'
tomrozb
  • 25,773
  • 31
  • 101
  • 122
  • 2
    still cant recognise my generated "Dagger" prefix component?. it was doing it a minute ago! lol – filthy_wizard Jan 20 '16 at 14:30
  • 1
    Why is jsr better than glassfish annotation? – IgorGanapolsky Feb 02 '16 at 22:31
  • 1
    @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. – tomrozb Feb 04 '16 at 06:35
  • 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? – IgorGanapolsky Feb 04 '16 at 13:50
  • 2
    @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: http://central.maven.org/maven2/org/glassfish/javax.annotation/3.1.1/javax.annotation-3.1.1.pom For sure you don't need javaee-api annotations in your Android project, but it's also the dependency of the GlassFish annotation library. – tomrozb Feb 04 '16 at 20:02
  • @tomrozb So I should use both in my gradle build? – IgorGanapolsky Feb 05 '16 at 14:35
  • 1
    @IgorGanapolsky definitely not. Just use one in Android projects. – tomrozb Feb 09 '16 at 09:30
  • This solution saves my time. – fruqi Aug 07 '17 at 09:09
  • 1
    Configuration 'provided' is obsolete and has been replaced with 'compileOnly' – Amir Ziarati Sep 06 '18 at 14:02
14

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.

Alex Fu
  • 5,509
  • 3
  • 31
  • 40
5

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.

Kairat
  • 698
  • 1
  • 11
  • 14
2

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

0

The right answer today is to use a version of dagger which is greater than 2.1 (because of the fix mentioned by @tomrozb is integrated in 2.1)

Soham
  • 4,940
  • 3
  • 31
  • 48