question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using HiltAndroidApp annotation within the library module

See original GitHub issue

Hi, 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:open
  • Created 10 months ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
grkepcommented, Nov 30, 2022

@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 the build.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 of api "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!

0reactions
hubtworkcommented, Dec 2, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency injection with Hilt | Android Developers
... Dependency injection with Hilt · Hilt in multi-module apps · Use Hilt with other Jetpack libraries · Hilt testing guide · Hilt...
Read more >
Perfectus: Hilt in Modular Application - Victor Apoyan - Medium
When all dependencies are set, it's time for annotating Application class with @HiltAndroidApp annotation, this annotation will generate a base class for ...
Read more >
How to use Hilt in a Library : r/androiddev - Reddit
All apps using Hilt must contain an Application class annotated with @HiltAndroidApp. @HiltAndroidApp kicks off the code generation of the Hilt ...
Read more >
Quick Start Guide - Dagger
Further examples assume usage of the Hilt Gradle plugin. Hilt Application. All apps using Hilt must contain an Application class annotated with @HiltAndroidApp...
Read more >
Dagger hilt support for Library Application - android
All apps that use Hilt must contain an Application class that is annotated with @HiltAndroidApp. Also, Hilt currently supports the following ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found