在尝试将新的androidx与Dagger和Kotlin结合使用时,我正在尝试修复错误。在用Java编写的代码中使用时效果很好。但是,当我将其切换为Kotlin时, 我收到错误:错误:没有@Provides批注的方法,无法提供java.util.Map,javax.inject.Provider >>。我正在使用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"
我的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)
}
还有我的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
}
}
因此,由于该代码,未生成我的DaggerAppComponent。我什至尝试了2.19版,但还有其他错误。我读到他们正在尝试修复它。如果有任何解决方法或任何建议,我将不胜感激。
哦,还有一件事。用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);
}
如果我像下面这样在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)
}
答案 0 :(得分:0)
尝试将InjectionModules添加到您的AppComponent.kt
@Singleton
@Component(modules = [
AppModule::class,
BuildersModule::class,
NetworkModule::class
AndroidInjectionModule::class,
AndroidSupportInjectionModule::class
])
interface ApplicationComponent {
....