java 找不到 Dagger 2 的符号类“生成”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29785530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:56:34  来源:igfitidea点击:

Cannot find symbol class "Generated" for Dagger 2

javaandroiddagger-2

提问by x-treme

I just started doing dependency injection using Dagger 2. When I spun up my modules, componentsand tried to build my application, gradlethrew the error

我刚开始使用Dagger 2. 当我启动我的modulescomponents并尝试构建我的应用程序时,gradle抛出了错误

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

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

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

我深入研究,发现错误是在其中一个类中Dagger生成 do DI。缺少的特定类是javax.annotation.Generated,抛出错误的行是将Dagger生成的类注释为@Generated("dagger.internal.codegen.ComponentProcessor")

Thisquestion helped in finding the solution which is to add the javaxpackage 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.

这个问题有助于找到javax通过将行添加compile 'org.glassfish:javax.annotation:10.0-b28'到我的 gradle 构建文件来将包添加为依赖项的解决方案。这导致了成功的构建。

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

我的问题是,为什么没有将其添加为传递依赖项,Dagger或者为什么没有其他人面临这个特定问题(我认为是这样,因为我在这里找不到任何关于此的问题?

采纳答案by Alex Fu

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

阅读更多信息: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.

基本上,解决方案是做你已经做过的事情,包括 glassfish javax 注释库。

回答by tomrozb

TL;DR use Dagger >= 2.1

TL;DR使用匕首 >= 2.1

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

Alex 是对的,但最好添加 JSR250 依赖项而不是 GlassFish

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

or for the latest gradle plugin:

或者对于最新的 gradle 插件:

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

回答by Kairat Doshekenov

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.

如果您的 JAVA_HOME 指向 JAVA 版本 9 或 10,就会发生这种情况。将 JAVA_HOME 切换到 Java 8 可以解决此问题,您将不需要额外的依赖项。

回答by Juan Mendez

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

我将我的 JVM 降级到 Java 8,并且gradle build使用 Dagger 2 在我的 Android 应用程序中成功运行。

回答by Soham

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

今天正确的答案是使用一个版本的匕首,它大于2.1(因为的修复由@tomrozb提到的集成在2.1)