简体   繁体   English

了解dagger2的用法

[英]Understand usage of dagger2

I downloaded the Coffee sample application and bring it into my IDE. 我下载了Coffee示例应用程序并将其带入我的IDE。 After I run mvn clean , my IDE reports compilation error as: 运行mvn clean ,IDE将编译错误报告为:

在此处输入图片说明

And it won't get fixed until I run mvn compile . 在我运行mvn compile之前它不会得到解决。 Obviously the sample code refers to some class that is generated by the tool. 显然,示例代码引用了该工具生成的某些类。

I am quite surprised by this design as before you get the project compiled, you won't have a project that is ready to compile (looks like), kind of like you have to eat your own egg to get born. 我对这种设计感到非常惊讶,因为在您编译项目之前,您将没有一个可以编译的项目(看起来像),有点像您必须吃自己的鸡蛋才能出生。

Question: is this the normal usage scenario of dagger2? 问题:这是dagger2的正常使用情况吗? Or did I miss something? 还是我错过了什么?

Yes. 是。 Dagger makes use of the Java Annotation Processing Tool , which hooks into the compilation process. Dagger使用了Java注释处理工具 ,它可以连接到编译过程。 During the annotation processing, the DaggerCoffeeApp_Coffee class is generated, after which the original and generated source code is compiled as a whole. 在注释处理期间,将生成DaggerCoffeeApp_Coffee类, 然后将原始和生成的源代码作为一个整体进行编译。

Needless to say, before compilation this class does not exist yet. 不用说,在编译之前,此类尚未存在。 That is why your IDE cannot find it, and marks it as an error. 这就是您的IDE无法找到它并将其标记为错误的原因。 After running mvn compile , it can find the class. 运行mvn compile ,可以找到该类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM