Android 依赖组件中的Dagger2和限定符
我有一个应用程序组件和一个从属组件。应用程序组件声明显式依赖项,依赖组件可以注入这些依赖项。但是,当我有一个依赖项时,我必须用@Qualifier来消除歧义,依赖项组件不能注入该依赖项 这是应用程序组件Android 依赖组件中的Dagger2和限定符,android,kotlin,dagger-2,Android,Kotlin,Dagger 2,我有一个应用程序组件和一个从属组件。应用程序组件声明显式依赖项,依赖组件可以注入这些依赖项。但是,当我有一个依赖项时,我必须用@Qualifier来消除歧义,依赖项组件不能注入该依赖项 这是应用程序组件 @Component(modules = [AppModule::class, SchedulersModule::class, StorageModule::class]) @ApplicationScope interface AppComponent { fun inject(a
@Component(modules = [AppModule::class, SchedulersModule::class, StorageModule::class])
@ApplicationScope
interface AppComponent {
fun inject(app: Application)
/* other stuff omitted for brevity */
val bitmapCache: BitmapCache
@UiScheduler fun uiScheduler(): Scheduler
}
这是调度程序模块:
@Module
class SchedulersModule {
@ApplicationScope
@Provides
@IoScheduler
fun provideIoScheduler(): Scheduler = Schedulers.io()
@ApplicationScope
@Provides
@UiScheduler
fun provideMainThreadScheduler(): Scheduler = AndroidSchedulers.mainThread()
}
这是限定符:
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class UiScheduler
这是从属组件:
@Component(
dependencies = [AppComponent::class],
modules = [EditEntryActivityModule::class, ViewModelModule::class]
)
@ActivityScope
interface EditEntryActivityComponent {
fun inject(editEntryActivity: EditEntryActivity)
fun inject(editEntryFragment: EditEntryFragment)
}
以下是调度程序在片段中的注入方式:
class EditEntryFragment : Fragment() {
@Inject @UiScheduler lateinit var uiScheduler: Scheduler
/* other stuff */
}
那么,为什么依赖组件可以注入位图缓存(在父组件中声明),而不是UI调度程序?这是我得到的错误:
error: io.reactivex.Scheduler cannot be provided without an @Provides- or @Produces-annotated method.
io.reactivex.Scheduler is injected at
com.test.edit.EditEntryFragment.uiScheduler
com.test.edit.EditEntryFragment is injected at
com.test.edit.EditEntryActivityComponent.inject(arg0)
1 error
尝试
@Named
注释
@Inject @field:Named("UiScheduler") lateinit var uiScheduler: Scheduler
使用类EditEntryFragment中的@field:UiScheduler签出,这在语法上是不正确的(您缺少UiScheduler上的引号),但无论如何,这是针对名为的注释的,我目前正在使用限定符。相同的解决方案不适用,或者至少据我所知,不适用于相同的方式。是否有此
@字段的变体
使用@限定符修复依赖项
?我不知道。也许你可以试试“@Named”。谢谢,但这个问题是针对限定符的。