为什么@Named依赖项被忽略?

时间:2018-11-05 23:04:21

标签: android kotlin dagger-2

我有一个提供Boolean值的模块:

@Module
class TestModule(private val isTesting: Boolean = false) {

    @Provides
    @Singleton
    @Named(nameIsTesting)
    fun provideIsTesting(): Boolean = isTesting

    // Companion
    companion object {
        const val nameIsTesting = "is_testing"
    }

}

AppComponent

@Component(modules = [AppModule::class, TestModule::class])
@Singleton
interface AppComponent {

    @Named(TestModule.nameIsTesting)
    val isTesting: Boolean

    // ...
}

构建失败:

[Dagger/MissingBinding] java.lang.Boolean cannot be provided without an @Inject constructor or an @Provides-annotated method.
public abstract interface AppComponent {
                ^
      java.lang.Boolean is provided at
          com.app.app.di.component.AppComponent.isTesting()

它适用于其他依赖项,但不适用于此依赖项。为什么?

1 个答案:

答案 0 :(得分:1)

尝试使用

@Named(TestModule.nameIsTesting)
fun isTesting(): Boolean

代替

@Named(TestModule.nameIsTesting)
val isTesting: Boolean