How to declare activities and fragments for non-base feature modules?
As we had only a base feature module and a single feature module, we could declare the App class in the feature module and had our graph be loaded there. This meant being able to use ContributesAndroidInjector and the standard dagger approach for android.
Now, adding more feature modules, we can't do the same. The application class must stay in the base feature module, which means it can't declare, for example, activities belonging to features.
My thoughts:
- Since - AndroidInjection.inject(this)will look for the activity injector in the app class, we can't use it. In other words, no- DaggerAppCompatActivity.
- So the idea is that activities belonging to feature modules should, instead, create their own component and inject themselves. 
- Still, it should be possible for fragments to use the - @ContributesAndroidInjectorthing. Right? The- AndroidInjectionclass will get the injector from the parent activity, so if we fix our activities, they will expose the correct injector so that fragment code can be left as is.
- For this reason, the feature activities must implement - HasFragmentInjectorand have a- @Injectannotated- DispatchingAndroidInjectorfor fragments.
But two things don't work here.
- Feature fragments and activities still need some - @Singletonannotated objects from the base feature component graph, so our- SpecialFeatureComponentmust somehow be linked to the- BaseFeatureComponent.- It seems that the only way of doing so is by using the - dependenciesparameter:- @Component( dependencies = [BaseFeatureComponent::class], modules = [SpecialFeatureModule::class] // @contributes fragment ) interface SpecialFeatureComponent- The - SpecialActivitycreates this component, passes the- BaseFeatureComponentto its builder, and injects itself.- However, compilation fails due to - MissingBindingerrors. Some of the objects in the special feature module need- @Provide,- @Singletonannotated objects from the base feature component, and dagger doesn't seem to find them properly. (these objects are not in- BaseFeatureComponent, but rather in its attached modules)- How to fix this?- I have read that exposing them directly in - BaseFeatureComponent, rather than in its dependency modules, should fix the issue, but it's not something we would like to do, as there are lots of them and it would be yet another list to be maintained.
- As is, the unscoped - SpecialFeatureComponentdepends on the- @Singletonscoped- BaseFeatureComponent. This is not possible, so we have to add a- ActivityScopeannotation.- @ActivityScope @Component( dependencies = [BaseFeatureComponent::class], modules = [SpecialFeatureModule::class] // @contributes fragment ) interface SpecialFeatureComponent- Now we are told that the subcomponents generated by - @ContributesAndroidInjectorfor fragments, unscoped, may not reference scoped bindings. So we add a- @FragmentScopeannotation there.- @Module abstract class SpecialFeatureModule { @FragmentScope @ContributesAndroidInjector internal abstract fun specialFragment(): SpecialFragment }- Now we are told that these subcomponents may not reference bindings with a different scope! Which are - @Singletonobjects provided by the base feature graph.
