[英]Dagger 2 error with kotlin missing provider
I am trying to fix bug when using new androidx with Dagger and kotlin. 我正在尝试将新的androidx与Dagger和Kotlin结合使用时修复错误。 It works fine when using in Java written. 在用Java编写的代码中使用时效果很好。 But, when I switch it to kotlin, I got error: error: java.util.Map,javax.inject.Provider>> cannot be provided without an @Provides-annotated method. 但是,当我将其切换到kotlin时,出现错误:错误:如果没有@Provides注释的方法,则无法提供java.util.Map,javax.inject.Provider >>。 I am using Android Studio 3.2.1 Here is my code: 我正在使用Android Studio 3.2.1,这是我的代码:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
implementation "com.google.dagger:dagger:2.13"
implementation "com.google.dagger:dagger-android:2.13"
implementation "com.google.dagger:dagger-android-support:2.13"
kapt "com.google.dagger:dagger-compiler:2.13"
kapt "com.google.dagger:dagger-android-processor:2.13"
My AppComponent: 我的AppComponent:
@Singleton
@Component(modules = [AppModule::class, BuildersModule::class, NetworkModule::class])
interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun application(app: BaseApp): Builder
fun build(): AppComponent
}
fun inject(app: BaseApp)
}
And my AppModule: 而我的AppModule:
@Module
class AppModule {
@Provides
@Singleton
internal fun provideDataManager(): DataManager {
return DataManager()
}
@Provides
fun provideContext(app: BaseApp) : Context {
return app.applicationContext
}
@Provides
fun provideRemoteData (remoteRepository: RemoteRepository): RemoteContract {
return remoteRepository
}
}
So, my DaggerAppComponent is not generated because of that code. 因此,由于该代码,未生成我的DaggerAppComponent。 I even tried version 2.19 but got some other bugs. 我什至尝试了2.19版,但还有其他错误。 I read that they are trying to fix it. 我读到他们正在尝试修复它。 I would appreciate if there is some way around or any suggestion. 如果有任何解决方法或任何建议,我将不胜感激。
Oh, one more thing. 哦,还有一件事。 In Java written, I had also AndroidSupportInjectionModule in AppComponent like and it worked fine: 用Java编写的时候,我在AppComponent中也有AndroidSupportInjectionModule,并且运行良好:
@Singleton
@Component(modules = {AndroidSupportInjectionModule.class, AppModule.class, BuildersModule.class, NetworkModule.class})
public interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(BaseApp app);
AppComponent build();
}
void inject(BaseApp app);
}
If I add this in Kotlin version like bellow, I do not get above error anymore, but instead error when building is: Unresolved reference: DaggerAppComponent 如果我在象下面这样的Kotlin版本中添加此代码,我将不再遇到上述错误,而是在构建时出现错误:未解析的引用:DaggerAppComponent
@Singleton
@Component(modules = [
AndroidSupportInjectionModule::class,
AppModule::class,
BuildersModule::class,
NetworkModule::class
])
interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun application(app: BaseApp): Builder
fun build(): AppComponent
}
fun inject(app: BaseApp)
}
Try to add InjectionModules to your AppComponent.kt 尝试将InjectionModules添加到您的AppComponent.kt
@Singleton
@Component(modules = [
AppModule::class,
BuildersModule::class,
NetworkModule::class
AndroidInjectionModule::class,
AndroidSupportInjectionModule::class
])
interface ApplicationComponent {
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.