【发布时间】:2020-11-06 16:34:10
【问题描述】:
在执行 dagger-2 以阻止迁移时,模块出现此错误
错误:
FooModule.Companion is listed as a module, but it is a companion
object class.
Add @Module to the enclosing class
and reference that instead.
刀柄之前
@Module
abstract class FooModule {
@Binds
@FooScope
abstract fun bindsManager(impl: FooManagerImpl): FooManager
@Module
companion object {
@Provides
@FooScope
@JvmStatic
fun providesConfig(prefs: SharedPreferences): FooConfig = FooConfigImpl(prefs)
}
}
剑柄之后
@InstallIn(ApplicationComponent::class)
@Module
abstract class FooModule {
@Binds
@FooScope
abstract fun bindsManager(impl: FooManagerImpl): FooManager
@InstallIn(ApplicationComponent::class)
@Module
companion object {
@Provides
@FooScope
@JvmStatic
fun providesConfig(prefs: SharedPreferences): FooConfig = FooConfigImpl(prefs)
}
}
迁移文档参考:https://developer.android.com/codelabs/android-dagger-to-hilt#4
【问题讨论】:
-
了解 Jake Wharton 关于使用
objects而不是 github.com/google/dagger/issues/900#issuecomment-410041915 的评论
标签: android kotlin dagger-2 dagger-hilt