问题描述
我已经简化了我的应用程序以找到问题的根源,这是简化的版本。我正在使用以下配置实现Dagger 2:
I have simplified my application to get the root of the problem and here is the simplified version. I'm implementing Dagger 2 using following configuration:
AppComponent
@Component(modules = [
AndroidSupportInjectionModule::class,
ActivityBindingModule::class
])
interface AppComponent: AndroidInjector<MyApp> {
@Component.Builder
interface Builder{
@BindsInstance
fun application(application: Application): Builder
fun build(): AppComponent
}
}
ActivityBindingModule
@Module
abstract class ActivityBindingModule {
@ContributesAndroidInjector
abstract fun mainActivity(): MainActivity
@Module
companion object{
@JvmStatic
@Provides
fun provideString(mainActivity: MainActivity): String{
return "Tent"
}
}
}
MainActivity
class MainActivity : DaggerAppCompatActivity() {
@Inject
lateinit var string: String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
println("meco simplest ${string}")
}
}
运行应用程序时,出现以下错误。我不明白的是 ContributesAndroidInjector
已经提供了 MainActivity
的实例,为什么Dagger仍会抱怨。 p>
When I run the application I get the following error. What I don't understand is ContributesAndroidInjector
is already providing an instace of MainActivity
why Dagger still complains about it.
编辑 @yavor
保持所有类不变,并单独保留 ActivityBindingModule 实现分为两类。现在,您可以看到提供了 MainActivity
的实例,而Dagger对此没有抱怨。
ActivityBindingModule
Keep all classes as is and separate ActivityBindingModule implementation into two classes. Now you can see that instance of the MainActivity
is provided and Dagger is not complaining about it.ActivityBindingModule
@Module
abstract class ActivityBindingModule {
@ContributesAndroidInjector(modulese [StringProviderModule::class])
abstract fun mainActivity(): MainActivity
}
StringProviderModule
@Module
class StringProviderModule {
@Module
companion object{
@JvmStatic
@Provides
fun provideString(mainActivity: MainActivity): String{
return "Tent"
}
}
}
推荐答案
忘记将应用程序注入 MyApp
。您应该具有类似的内容(您可能需要对其进行一些修改以适合您的 AppComponent
:
You probably forgot to inject your application in MyApp
. You should have something like this (you might need to modify it a bit to fit your AppComponent
:
DaggerAppComponent.builder()
.application(this)
.build()
.inject(this)
此外,Dagger实际上是通过 @ContributesAndroidInjector提供您的
带注释的方法,但这不是您要注入的方法。 MainActivity
Also, Dagger is actually providing your MainActivity
through your @ContributesAndroidInjector
annotated method but that's not what you're injecting.
您要注入字符串
所以Dagger正在使用您的 provideString
方法。由于此方法需要 MainActivity
实例才能正常工作,因此Dagger正在寻找这样的方法用 @Provides
注释的方法。您没有任何内容,而且Dagger不会查看您的 @ContributesAndroidInjector
方法,因为
You're injecting a string
so Dagger is using your provideString
method. Since this method requires a MainActivity
instance to work, Dagger is looking for such a method annotated with @Provides
. You don't have any and Dagger won't look at your @ContributesAndroidInjector
method since it does not have any reasons to do so.
如果希望它起作用,实际上必须定义您的 provideString
方法在单独的模块中并在insid中安装e您的 @ContributesAndroidInjector
:
If you want it to work, you actually have to define your provideString
method in a separate module and install it inside your @ContributesAndroidInjector
:
@Module
abstract class ActivityBindingModule {
@ContributesAndroidInjector(modules = [StringModule::class])
abstract fun mainActivity(): MainActivity
}
@Module
class StringModule {
@JvmStatic
@Provides
fun provideString(mainActivity: MainActivity): String = "Hehe"
}
这篇关于为什么@ContributesAndroidInjector不提供Android Framework类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!