No Injector Factory Bound For Class<>
Solution 1:
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 {
funinject(pocketTreasureApplication: MyApplication)
@Component.FactoryinterfaceFactory {
funcreate(@BindsInstanceapplication: 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.
Post a Comment for "No Injector Factory Bound For Class<>"