Dagger2错误:Kotlin:未解决的参考:DaggerSOMETHINGComponent

源代码

我正在构建一个半年前在macOS上运行良好的Kotlin和Dagger2的gradle项目:

GitHub的:科特林-Tutorials13

你可以得到它

git clone https://github.com/enbandari/Kotlin-Tutorials.git cd Kotlin-Tutorials/code/Kt13 

你可以在IntelliJ IDEA中打开它

建筑环境

我的建筑环境如下:

  • IntelliJ IDEA 2017.1.3
  • JRE:1.8.0_112-release-736-b21 amd64
  • JVM:由JetBrains sro提供的OpenJDK 64位服务器虚拟机
  • Windows 10 10.0

建设成果

它调出一个错误:

 C:\Users\xiang\IdeaProjects\Kotlin-Tutorials-master\code\Kt13\src\main\kotlin\net\println\kt13\Service.kt Error:(3, 25) Kotlin: Unresolved reference: DaggerRESTFulComponent Error:(19, 9) Kotlin: Unresolved reference: DaggerRESTFulComponent 

反编译

所以,我反编译RESTFulComponent.class

它的源文件是RESTFulComponent.kt

 package net.println.kt13 import dagger.Component import net.println.kt13.module.RetrofitModule import retrofit2.Retrofit import javax.inject.Singleton /** * Created by benny on 12/11/16. */ @Singleton @Component(modules = arrayOf(RetrofitModule::class)) interface RESTFulComponent { fun retrofit(): Retrofit } 

反编译我们得到的RESTFulComponent.class

 // IntelliJ API Decompiler stub source generated from a class file // Implementation of methods is not available package net.println.kt13.module @dagger.Module public final class RxAdapterModule public constructor() { @javax.inject.Singleton @dagger.Provides public final fun adapter(): retrofit2.CallAdapter.Factory { /* compiled code */ } } 

原因

匕首注解不起作用的错误结果。

我该如何解决这个错误?

错误的操作

我使用Ctrl + F9进行编译,并出现此错误。

正确的操作

我们应该点击查看 – > 工具窗口 – > Gradle打开Gradle项目窗口

然后点击任务 – >生成 – >生成

正确的构建

原因

Ctrl + F9不会调用gradle build ,我们应该在Gradle项目窗口中使用gradle build