Я использую Dagger 2.24. Когда я компилирую ниже
fun main() {
val myClass = MyClass()
}
class MyClass {
@Inject
lateinit var stringMe: String
init {
DaggerMyComponent.create().subComponent().inject(this)
println(stringMe)
}
}
@Component
interface MyComponent {
fun subComponent(): MySubcomponent
fun inject(a: MyClass)
}
@Subcomponent(modules = [MeSubModule::class])
interface MySubcomponent {
fun inject(a: MyClass)
}
@Module
class MeSubModule {
@Provides
fun stringMe(): String = "Hi here"
}
Это выдает ошибку с указанием
error: [Dagger/MissingBinding] java.lang.String cannot be provided without an @Inject constructor or an @Provides-annotated method.
public abstract interface MyComponent {
^
A binding with matching key exists in component: com.elyeproj.modular1bottombase.MySubcomponent
java.lang.String is injected at
com.elyeproj.modular1bottombase.MyClass.stringMe
com.elyeproj.modular1bottombase.MyClass is injected at
com.elyeproj.modular1bottombase.MyComponent.inject(com.elyeproj.modular1bottombase.MyClass)
Затем я закомментирую простую строку ниже, все скомпилируется нормально.
fun main() {
val myClass = MyClass()
}
class MyClass {
@Inject
lateinit var stringMe: String
init {
DaggerMyComponent.create().subComponent().inject(this)
println(stringMe)
}
}
@Component
interface MyComponent {
fun subComponent(): MySubcomponent
// fun inject(a: MyClass) // Comment this out.
}
@Subcomponent(modules = [MeSubModule::class])
interface MySubcomponent {
fun inject(a: MyClass)
}
@Module
class MeSubModule {
@Provides
fun stringMe(): String = "Hi here"
}
Подозреваюэто ошибка в Dagger 2, но просто напишите здесь на случай, если я что-то пропущу?