javaandroidkotlindagger-2model-view

No injector factory bound for Class<>


I saw the same question in stack but they try to fix the error with the @ContributesAndroidInjector but in Dagger documentation says use @ContributesAndroidInjector is only optional, so here are my classes:

My MainActivityComponent:

@Subcomponent(modules = [
    MainBuilder::class
])
@ActivityScope
interface MainComponent: AndroidInjector<MainActivity>{

    @Subcomponent.Factory
    interface Factory: AndroidInjector.Factory<MainActivity>
}

My AplicationBinder:

@Module(subcomponents = [
    MainComponent::class
])
abstract class AppBuilder {

    @Binds
    @IntoMap
    @ClassKey(MainActivity::class)
    abstract fun mainActivityFactoryBind(factory: MainComponent.Factory): AndroidInjector.Factory<out Activity>

}

And my BaseActivity who extends my MainActivity:

abstract class BaseActivity: HasSupportFragmentInjector, AppCompatActivity() {

    @Inject
    lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Fragment>

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
    }

    override fun supportFragmentInjector(): AndroidInjector<Fragment> {
        return dispatchingAndroidInjector
    }
}

How i can solve this issue?


Solution

  • Actually the dagger documentation says:

    Pro-tip: If your subcomponent and its factory have no other methods or supertypes other than the ones mentioned in step #2, you can use @ContributesAndroidInjector to generate them for you. Instead of steps 2 and 3, add an abstract module method that returns your activity, annotate it with @ContributesAndroidInjector, and specify the modules you want to install into the subcomponent. If the subcomponent needs scopes, apply the scope annotations to the method as well.

    So basically the @ContributesAndroidInjector will generate that subcomponent thing you are doing manually. Since your case matches the Daggers documentation on this step, you can freely use @ContributesAndroidInjector.

    Example:

    @Singleton
    @Component(
        modules = [AndroidInjectionModule::class, ActivityModule::class, BroadCastReceiversModule::class,...]
    )
    interface AppComponent {
        fun inject(pocketTreasureApplication: MyApplication)
    
        @Component.Factory
        interface Factory {
            fun create(@BindsInstance application: Application): AppComponent
        }
    }
    

    The AndroidInjectionModule is free from Dagger. In that case it tells Dagger: Hey we have Android components to deal with and than Dagger knows how to generate them.

    Than, you should use your modules, likeActivityModule to generate your classes that extend Activities, Fragments, Services, BroadCastReceivers etc.

    So the ActivityModule hold the@ContributesAndroidInjector`:

        @Singleton
        @ContributesAndroidInjector(modules = [FragmentModule::class])
        abstract fun contributeMainactivity(): MainActivity
    

    And the now Dagger knows that you may magically inject dependencies on MainActivity. Same works for the FragmentModule inside it.

    And than in your MainActivity you can do:

    AndroidInjection.inject(this) and inject your dependencies.

    That's all. You may check more into my personal article for Dagger-Android here.