问题描述
尝试使用Dagger 2并与命名提供者发生问题.我有一个简单的设置,如下所示:
Trying to grok Dagger 2 and having an issue with named providers. I have a simple setup as follows:
// Module
@Module
class AppModule(private val app: App) {
@Provides @AppScope fun providesApp() = app
@Provides @AppScope fun provideSharedPreferences(app: App) = PreferenceManager.getDefaultSharedPreferences(app)
@Provides @AppScope @Named("Uri1") fun providesUri1() = Uri.Builder().scheme("https").authority("authory1").build()
@Provides @AppScope @Named("Uri2") fun providesUri2() = Uri.Builder().scheme("https").authority("authory2").build()
}
// Component
@AppScope
@Component(modules = arrayOf(AppModule::class))
interface AppComponent {
fun inject(target: MainActivity)
}
// MainActivity
@Inject @AppScope lateinit var preferences: SharedPreferences
@Inject @AppScope @Named("Uri1") lateinit var uri1: Uri
@Inject @AppScope @Named("Uri2") lateinit var uri2: Uri
在重建项目时,我会得到:
When rebuilding my project I am given:
Error:Gradle: android.net.Uri cannot be provided without an @Provides- or @Produces-annotated method.
我不明白为什么添加命名的限定词对我来说不起作用.如果删除这些,则可以毫无问题地获取SharedPreferences实例.
I don't understand why adding the Named qualifier doesn't work for me here. If I remove these I can get an instance of SharedPreferences without issue.
任何对我正在做错事的见解都将受到赞赏!
Any insight into what I'm doing wrong would be appreciated!
每个建议的更改具有与上述相同的结果.
Changes per suggestions with the same results as above.
// New module
@Module
class AppModule(private val app: App) {
@Provides @AppScope fun providesApp() = app
@Provides @AppScope fun provideSharedPreferences(app: App) = PreferenceManager.getDefaultSharedPreferences(app)
@Provides @AppScope @Tag("Uri1") fun providesUri1(): Uri = Uri.Builder().scheme("https").authority("authority1").build()
@Provides @AppScope @Tag("Uri2") fun providesUri2(): Uri = Uri.Builder().scheme("https").authority("authority2").build()
}
// Tag annotation
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class Tag(val tag: String = "")
// MainActivity
@Inject @AppScope lateinit var preferences: SharedPreferences
@Inject @AppScope @Tag("Uri1") lateinit var uri1: Uri
@Inject @AppScope @Tag("Uri2") lateinit var uri2: Uri
推荐答案
我认为我发现了问题(至少我检查了您的项目并正确生成了dagger类).如果需要注入使用@Named
或某些@Qualifier
注释注释的字段,则必须使用这种语法:
I think I found the problem (at least I checked out your project and it generated dagger classes correctly). If you need to inject fields annotated with @Named
or some @Qualifier
annotation you have to use this kind of syntax:
class MainActivity : AppCompatActivity() {
@Inject lateinit var preferences: SharedPreferences
@Inject @field:[Named ("Uri1")] lateinit var uri1: Uri // for @Named annotation or...
@Inject @field:Uri2 lateinit var uri2: Uri // ...for @Qualifier annotation
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
app().component.inject(this)
println(uri1)
println(uri2)
}
}
注意@Named
/限定词注释如何进入@field:
(本身没有@
).
Notice how @Named
/ qualifier annotation goes inside @field:
(without @
itself).
从此回购.
这篇关于如果没有@Provides方法,则无法提供Dagger 2 Named的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!