Java/IntelliJ 无法识别 Dagger2 组件的 "Dagger" 关键字

Java/IntelliJ not recognising the "Dagger" keyword for Dagger2 Components

我可能在这里遗漏了一些非常基本的东西,所以希望这不是一个难题。

我相当于

CoffeeShop coffeeShop = DaggerCoffeeShop.create();

未被 IDE (IntelliJ) 识别。 这是原生 Java 项目。 “DaggerCoffeShop”部分全部为红色。

该组件正在使用此语法:

@Component(modules = <ModuleClassName>.class)
public interface CoffeeShop {
    // some methods.
    // does it matter what goes in here for it to recognise the component?
}

"ModuleClassName" 是用@Module 注释的模块名称,包括@Provides 方法。

为了获取我在 Gradle 中使用的库:

compile 'com.google.dagger:dagger:2.4'
compile 'com.google.dagger:dagger-compiler:2.4'

这就是我所需要的吗?我没有收到任何注释的错误,只是它无法识别这个 Dagger 关键字。我错过了什么?

感谢任何帮助或指导。我觉得文档对像我这样的初学者来说不是那么好。

您的注释处理可能未启用。

Settings->Compiler->Annotation Processor->Enable annotation processing

之后,Rebuild Project

如果它仍然认为文件不存在(尽管应用 运行 正确),您必须将生成的源文件夹添加为源文件夹:File -> Project Structure -> Modules -> select 你的项目 -> Sources -> 右键单击​​生成的包含 Dagger 东西的文件夹 -> Sources


并且 compile 'com.google.dagger:dagger-compiler:2.4' 应该是 apt 范围或 annotationProcessor 范围。如果你使用 Kotlin,那么 kapt scoped