Cannot find symbol class “Generated” for Dagger 2

2019-04-03 08:25发布

问题:

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?

回答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.



回答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'


回答3:

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)