我正在使用Dagger2 AndroidInjector和Kotlin。我有一个子组件,其模块以这种方式定义:
@Subcomponent(modules = arrayOf(
UIModule::class,
HomeActivitySubcomponent.ComponentModule::class
))
interface HomeActivitySubcomponent : AndroidInjector<HomeActivity> {
@Subcomponent.Builder
abstract class Builder : AndroidInjector.Builder<HomeActivity>()
@Module
abstract class ComponentModule {
@Binds
@IntoMap
@ActivityKey(HomeActivity::class)
internal abstract fun bindMainActivityInjectorFactory(builder: Builder): AndroidInjector.Factory<out Activity>
}
}
如果这是java,我可以在ComponentModule @Module中添加静态@Provides方法。它必须是静态的,因为如果我向使用@Binds的@Module类添加非静态方法,Dagger会抱怨:
错误:@Module可能不包含非静态@Provides方法和 抽象@Binds或@Multibinds声明
问题是:我怎么能用Kotlin做到这一点?
答案 0 :(得分:0)
我结束了这个:
@Subcomponent(modules = arrayOf(
UIModule::class,
HomeActivitySubcomponent.ComponentModuleForProviders::class,
HomeActivitySubcomponent.ComponentModule::class
))
interface HomeActivitySubcomponent : AndroidInjector<HomeActivity> {
@Subcomponent.Builder
abstract class Builder : AndroidInjector.Builder<HomeActivity>()
@Module
abstract class ComponentModule {
@Suppress("unused")
@Binds
@IntoMap
@ActivityKey(HomeActivity::class)
internal abstract fun bindMainActivityInjectorFactory(builder: Builder): AndroidInjector.Factory<out Activity>
}
@Module
open class ComponentModuleForProviders {
@Provides
open fun provideDrawerPresenter(): DrawerPresenter {...}
}
}
所以我为@Provides方法使用了第二个@Module类:ComponentModuleForProviders
。