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.
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?