源代码
我正在构建一个 Kotlin 和 Dagger2 的 gradle 项目,半年前在 macOS 中运行良好:
你可以通过
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 s.r.o 的 OpenJDK 64 位服务器 VM
- 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 */ }
}
原因
dagger 注释 不工作导致的错误。
我该如何修复这个错误?
最佳答案
错误操作
我用Ctrl+F9编译出现这个错误。
正确操作
我们应该点击View -> Tool Windows -> Gradle打开Gradle projects窗口
然后点击Tasks -> build -> build
原因
Ctrl+F9 未调用 gradle build
,我们应该在 Gradle 项目 窗口中使用 gradle build
关于java - Dagger2 错误 : Kotlin: Unresolved reference: DaggerSOMETHINGComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44412774/