android - kotlin 缺少提供程序的 Dagger 2 错误
问题描述
我正在尝试修复将新的 androidx 与 Dagger 和 kotlin 一起使用时的错误。在 Java 中使用时它工作正常。但是,当我将它切换到 kotlin 时,我得到了错误:error: java.util.Map,javax.inject.Provider>> cannot be provided without an @Provides-annotated method。我正在使用 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"
我的应用组件:
@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)
}
解决方案
尝试将 InjectionModules 添加到您的 AppComponent.kt
@Singleton
@Component(modules = [
AppModule::class,
BuildersModule::class,
NetworkModule::class
AndroidInjectionModule::class,
AndroidSupportInjectionModule::class
])
interface ApplicationComponent {
....
推荐阅读
- xml - Follow Up on question that got solvend using Muenchian Grouping
- javascript - V8之后是JS解释还是编译?
- excel - How to chose specific rows in worksheet
- angularjs - AngularJS - 如何将数据从视图(HTML)传递到控制器(JS)
- php - Mysql query order by desc limit in codeigniter
- android - 是否可以将股票 UI 用于来电,而将自定义 UI 用于拨出电话?
- android - Flutter - How to pass profile picture and name data to drawer from login screen
- python-3.x - 如何处理“OSError: [Errno -9999] Unexpected host error”?
- batch-file - Registry value data not working as expected through 'REG ADD'
- tinymce - Tinymce 4.5.5 不在编辑器中可视化图像