Dagger 2 error with kotlin missing provider

I am trying to fix bug when using new androidx with Dagger and kotlin. It works fine when using in Java written. 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. I am using Android Studio 3.2.1 Here is my code:

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:

@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:

@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. I even tried version 2.19 but got some other bugs. 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:

@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

@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)
}

1 answer

  • answered 2018-11-08 16:14 eurosecom

    Try to add InjectionModules to your AppComponent.kt

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