【问题标题】:Dagger 2 error with kotlin missing providerkotlin 缺少提供程序的 Dagger 2 错误
【发布时间】:2019-04-11 16:53:30
【问题描述】:

我正在尝试修复在 Dagger 和 kotlin 中使用新的 androidx 时出现的错误。在 Java 中使用时它工作正常。但是,当我将它切换到 kotlin 时, 我得到了错误:错误:java.util.Map,javax.inject.Provider>> 不能在没有@Provides-annotated 方法的情况下提供。我正在使用 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)
}

【问题讨论】:

  • 我可以显示其他模块来源吗?

标签: android kotlin dagger-2 androidx


【解决方案1】:

尝试将 InjectionModules 添加到您的 AppComponent.kt

@Singleton
@Component(modules = [
AppModule::class,
BuildersModule::class,
NetworkModule::class
AndroidInjectionModule::class,
AndroidSupportInjectionModule::class
])
interface ApplicationComponent {
....

【讨论】:

    猜你喜欢
    • 2017-12-07
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 2018-03-08
    • 2017-12-08
    相关资源
    最近更新 更多