1

I'm using dagger in a multi module project. I'm working on a feature module (Gradle module) which is independent of the main App module.

The main app gradle module imports my module, so that classes in the main module is not accessible in my module.

See this diagram for better understanding.

enter image description here

Let's assume I'm working on the :feature module. and in the app module's build.gradle imports :feature

implementation project(':feature')

Now, I'm facing some issues with my dagger setup. The AppComponent is inside the :app module which is not accessible in :feature

I'm injecting dependencies into my activities and fragments in :feature with the help of AndroidInjector. So even though AppComponent is not accessible in :feature module, dagger will be able to resolve the graph ( with the help of @ContributesAndroidInjector ) which makes it easy for me to injecting into framework classes. (activities, services, fragments)

But I have a normal class Feature.java in my :feature module which is a launcher class for my feature (kind of Application class). I call Feature.init() where I init all required objects for my feature.

Here I need to make use of Dagger and do field injection here.

        appComponent.inject(this);

But the problem I'm facing here is , the AppComponent is not accessible here. I need to inject certain dependencies from AppComponent but since

AppComponent is a part of :app module and my feature doesn't import :app I'm not able to get an instance of AppComponent. Is there any way to solve this issue? Like, the way android injector injects to activities?

doe
  • 971
  • 2
  • 11
  • 27
  • i suggest you see how plaid app is implemented. they use core component. shared stuff. https://www.youtube.com/watch?v=NNWejxBORgc&ab_channel=AndroidDevelopers should help – Raghunandan Jan 27 '20 at 09:39
  • What dependencies from appcomponent do you you want in :feature? If you need some class from app in feature, then rethink your dependency arch. Maybe that should be in core, or feature or a separate common module – denvercoder9 Jan 27 '20 at 15:54

0 Answers0