【问题标题】:Dagger2 error: Kotlin: Unresolved reference: DaggerSOMETHINGComponentDagger2 错误:Kotlin:未解决的参考:DaggerSOMETHINGComponent
【发布时间】:2017-06-07 12:34:37
【问题描述】:

源代码

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

GitHub:Kotlin-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 s.r.o 的 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 */ }
}

原因

匕首注释不工作导致的错误。

我该如何解决这个错误?

【问题讨论】:

  • 通常Dagger编译器的输出比较多,可以帮助定位问题。对了,你是用kapt做注解处理的吗?
  • @Egor 是的,我正在使用 kapt。你可以看到 gradle config here 。不幸的是,没有更多的错误输出。
  • 我实际上遇到了这个问题,这是因为 ButterKnife 没有正确处理可空类型以供查看,不得不将其替换为 lateinit

标签: java android intellij-idea kotlin dagger-2


【解决方案1】:

错误操作

我用Ctrl+F9编译出现这个错误。

正确操作

我们应该点击View -> Tool Windows -> Gradle 打开Gradle projects Window

然后点击Tasks -> build -> build

原因

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

【讨论】:

  • 谢谢!我在 IntelliJ 中遇到了同样的问题,尽管它不是一个 android 项目。我只需要运行一个 gradle 构建而不是重建整个项目。
猜你喜欢
  • 2017-08-08
  • 2021-09-27
  • 1970-01-01
  • 1970-01-01
  • 2016-03-12
  • 2018-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多