Dagger: DaggerAppCompatActivity with support fragments only

Created on 25 Mar 2017  Â·  5Comments  Â·  Source: google/dagger

I'm extending DaggerAppCompatActivity, but the compilation fails, because I only have support library fragments, and the multibind map for android.app.Fragments isn't generated (I guess).

The error is the following:

Error:(18, 8) error: [dagger.android.AndroidInjector.inject(T)] java.util.Map<java.lang.Class<? extends android.app.Fragment>,javax.inject.Provider<dagger.android.AndroidInjector.Factory<? extends android.app.Fragment>>> cannot be provided without an @Provides-annotated method.
java.util.Map<java.lang.Class<? extends android.app.Fragment>,javax.inject.Provider<dagger.android.AndroidInjector.Factory<? extends android.app.Fragment>>> is injected at
dagger.android.DispatchingAndroidInjector.<init>(injectorFactories)
dagger.android.DispatchingAndroidInjector<android.app.Fragment> is injected at
dagger.android.support.DaggerAppCompatActivity.frameworkFragmentInjector
???.MainActivity is injected at
dagger.android.AndroidInjector.inject(arg0)

I can create my own base class that doesn't implement HasDispatchingFragmentInjector, but I would like to keep using the library provided classes. What could be a workaround for this?

Most helpful comment

I just realized I missed AndroidInjectionModule from my component, declaring the multibinds is exactly its purpose.

All 5 comments

Workaround can be found under "Declare Multibindings" on this page https://google.github.io/dagger/multibindings.html

you need to add an @Multibinds in an appropriate module to get around this.

Thank you very much, it works well:

@Multibinds
abstract Map<Class<? extends android.app.Fragment>, AndroidInjector.Factory<? extends android.app.Fragment>> bindNativeFragments();

If I may ask an other off-topic question, I can't seem to find the DaggerApplication mentioned in the docs, where is it?

there is no base type. If you read through the android page of the github pages here: https://google.github.io/dagger/android.html

under the section "Injecting Activity objects" it goes over what to do in your application class and component.

I see. In this case, it might be worth removing the mentioning.

Dagger also provides a [DaggerApplication] for the same purpose — all you need to do is to extend it and override the applicationInjector() method to return the component that should inject the Application.

I just realized I missed AndroidInjectionModule from my component, declaring the multibinds is exactly its purpose.

Was this page helpful?
0 / 5 - 0 ratings