使用 Kotlin 在 Dagger2 中进行多重绑定
Multi Binding In Dagger2 with Kotlin
所以,我有这个问题,我想我明白发生了什么,但我不知道解决办法。
注意:我是 Dagger 2 的新手...我来自 SpringBoot,它在那里很懒惰,所以请放轻松。
I have a module that provides three methods, the last method depends on the first two methods like below:
@Module
class CoreModule {
@Provides
@Named("appName")
fun appName() : String{
return "Uburu Store"
}
@Provides
@Named("appVersion")
fun appVersion() : String {
return "0.0.1"
}
@Provides
@Named("appInfo")
fun appInfo(appName: String, appVersion: String) : String {
return "$appName Version:: $appVersion"
}
}
I'm getting an error that:
[Dagger/MissingBinding] java.lang.String cannot be provided without an @Inject constructor or an @Provides-annotated method.
My ApplicationComponent class:
@Component(modules = [(CoreModule::class), (AndroidSupportInjectionModule::class), (ActivityBuildersModule::class)])
interface ApplicationComponent : AndroidInjector<ApplicationClass> {
fun inject(application: Application)
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: Application?): Builder?
fun build(): ApplicationComponent?
}
}
My activity class:
class MainActivity : DaggerAppCompatActivity() {
@Inject @Named("appInfo") lateinit var appInfo : String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Toast.makeText(this, appInfo, Toast.LENGTH_LONG).show()
}
}
I managed to make it work if I don't use @Named annotation in any of the two dependencies i.e if I delete one of the Two provided dependencies from CoreModule, and remove the @Named annotation from the only dependency left, it will work fine like below:
@Module
class CoreModule {
@Provides
fun appName() : String{
return "Uburu Store"
}
@Provides
@Named("appInfo")
fun appInfo(appName: String) : String {
return "$appName Version:: $"
}
}
//This will work just fine, which made me realize that the @Named must be making it difficult for Dagger to understand something.
这种方法的问题是我不能没有@Named,因为它需要相同数据类型的多重绑定,例如:String。
My research so far made it known to me that I have to use @JvmSuppressWildcards somewhere but I'm not sure where.
Dagger 提供了基于图中 class 类型的依赖项,在您的 CoreModule 的第一个代码片段中,它无法向 appInfo(String, String)
方法提供依赖项,因为它无法确定属于哪种 String 类型在哪里。修复很简单;在模块中需要它们的方法中注释您已经使用 @Named
限定符提供的依赖项:
@Module
class CoreModule {
@Provides
@Named("appName")
fun appName() : String{
return "Uburu Store"
}
@Provides
@Named("appVersion")
fun appVersion() : String {
return "0.0.1"
}
@Provides
@Named("appInfo")
fun appInfo(@Named("appName") appName: String, @Named("appVersion") appVersion: String) : String {
return "$appName Version:: $appVersion"
}
}
您的第二个示例有效,因为图表中只提供了一种字符串类型。
有关详细信息,请转到 documentation 并滚动至限定符部分
所以,我有这个问题,我想我明白发生了什么,但我不知道解决办法。 注意:我是 Dagger 2 的新手...我来自 SpringBoot,它在那里很懒惰,所以请放轻松。
I have a module that provides three methods, the last method depends on the first two methods like below:
@Module
class CoreModule {
@Provides
@Named("appName")
fun appName() : String{
return "Uburu Store"
}
@Provides
@Named("appVersion")
fun appVersion() : String {
return "0.0.1"
}
@Provides
@Named("appInfo")
fun appInfo(appName: String, appVersion: String) : String {
return "$appName Version:: $appVersion"
}
}
I'm getting an error that:
[Dagger/MissingBinding] java.lang.String cannot be provided without an @Inject constructor or an @Provides-annotated method.
My ApplicationComponent class:
@Component(modules = [(CoreModule::class), (AndroidSupportInjectionModule::class), (ActivityBuildersModule::class)])
interface ApplicationComponent : AndroidInjector<ApplicationClass> {
fun inject(application: Application)
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: Application?): Builder?
fun build(): ApplicationComponent?
}
}
My activity class:
class MainActivity : DaggerAppCompatActivity() {
@Inject @Named("appInfo") lateinit var appInfo : String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Toast.makeText(this, appInfo, Toast.LENGTH_LONG).show()
}
}
I managed to make it work if I don't use @Named annotation in any of the two dependencies i.e if I delete one of the Two provided dependencies from CoreModule, and remove the @Named annotation from the only dependency left, it will work fine like below:
@Module
class CoreModule {
@Provides
fun appName() : String{
return "Uburu Store"
}
@Provides
@Named("appInfo")
fun appInfo(appName: String) : String {
return "$appName Version:: $"
}
}
//This will work just fine, which made me realize that the @Named must be making it difficult for Dagger to understand something.
这种方法的问题是我不能没有@Named,因为它需要相同数据类型的多重绑定,例如:String。
My research so far made it known to me that I have to use @JvmSuppressWildcards somewhere but I'm not sure where.
Dagger 提供了基于图中 class 类型的依赖项,在您的 CoreModule 的第一个代码片段中,它无法向 appInfo(String, String)
方法提供依赖项,因为它无法确定属于哪种 String 类型在哪里。修复很简单;在模块中需要它们的方法中注释您已经使用 @Named
限定符提供的依赖项:
@Module
class CoreModule {
@Provides
@Named("appName")
fun appName() : String{
return "Uburu Store"
}
@Provides
@Named("appVersion")
fun appVersion() : String {
return "0.0.1"
}
@Provides
@Named("appInfo")
fun appInfo(@Named("appName") appName: String, @Named("appVersion") appVersion: String) : String {
return "$appName Version:: $appVersion"
}
}
您的第二个示例有效,因为图表中只提供了一种字符串类型。
有关详细信息,请转到 documentation 并滚动至限定符部分