试图找到Dagger 2并遇到命名提供程序的问题。我有一个简单的设置如下:
// 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
在重建我的项目时,我得到了:
Error:Gradle: android.net.Uri cannot be provided without an @Provides- or @Produces-annotated method.
我不明白为什么在这里添加Named限定符对我不起作用。如果我删除这些,我可以毫无问题地获得SharedPreferences的实例。
对我所做错的任何见解都将不胜感激!
每项建议的更改与上述结果相同。
// 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
答案 0 :(得分:24)
我想我发现了问题(至少我检查了你的项目并且它正确地生成了匕首类)。如果您需要注入带有@Named
或某些@Qualifier
注释的注释字段,则必须使用这种语法:
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:
(没有@
本身)。
从这个repo借来的想法。