Using HiltAndroidApp annotation within the library module
See original GitHub issueHi, I’m trying to integrate Hilt in a multi-module android project(we don’t use feature modules). Here is a current structure of our project
- App Module
- Lib Module 1 (This library has Application class
MyApplication1
for app 1) - Lib Module 2 (This library has Application class
MyApplication2
for app 2) - Lib module 3 (common stuff)
- Up to Lib module 100
If I put @HiltAndroidApp
annotation on MyApplication1
I get this build error:
Application class annotated with @HiltAndroidApp has to be defined in an android application project
[Hilt] Processing did not complete. See error above for details.
To address this issue, if I move the application classes MyApplication1
and MyApplication2
to the App module, I’ll start getting circular dependency errors(because a library project cannot depend on App project). I’ve dozens of libraries and they have code(e.g. lateinit var app: MyApplication1
, access app specific properties, etc.) which needs a reference to the Application object, hence I cannot have independent Application classes. If you are suggesting to refactor our code base to have independent Application module, then that would likely be a year long project(given how large our code base is)
I also tried creating a separate app module(not library module) holding a base Application and made my MyApplication1
and MyApplication2
extend it, but I can’t do that successfully, because Android Studio doesn’t like a library-module
depend on an application-module
. And having a library-module depend on app-module doesn’t sound like a good pattern, and going to cause other issues anyway.
I came across this issue but the suggested solution on the issue won’t work in my case(due to reasons mentioned above).
Will Hilt ever support annotating an Application class with @HiltAndroidApp within the library modules?
Issue Analytics
- State:
- Created 10 months ago
- Comments:5
Top GitHub Comments
@hubtwork thanks for taking time to respond. I get that refactoring is a correct next step. I tried some quick refactoring, and the errors disappeared, and I was able to get some basic injection working. One gotcha, I would like to share w.r.t. multi-module setup is the hilt dependency. Such that, I had to add hilt dependencies
implementation "com.google.dagger:hilt-android:$hilt_version" kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
to thebuild.gradle
of each module(where I wanted injection), in addition to adding the dependencies in the root build.gradle. A common library with dependencies in the form ofapi "com.google.dagger:hilt-android:$hilt_version" kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
didn’t work(Got null references for dependency in this case, no compile time errors). If someone has any idea about not repeating the dependencies, please do share!@grkep hmm… in my knowledge, it isn’t bug. Hilt is android-wrapper for dagger, and dagger designed with some inconvenience ( as you said, repeating dependencies, etc ) but with best convenience for Dependency Injection by source-auto generating processors.