I would like to try Hilt DI in the android library.
It is a dependency on another project, with its own submodule. The very first problem I've encountered is the requirement of marking Application
with @HiltAndroidApp
. Now I do not have anything that extends Application
in my library ofc but would like to utilize Hilt and its predefined components.
Is it possible or should I go with Dagger only in such a case? I've found a solution for Dagger, where library dependency injection is made totally independently (the client is unaware of the library's DI): Dagger solution, would love to hear any opinion on that, maybe someone already put a great effort into that issue and can share his insights.
The Gradle module that compiles your Application class needs to have all Hilt modules and constructor-injected classes in its transitive dependencies. If your multi-module project is composed of regular Gradle modules, then you can use Hilt as described in Dependency injection with Hilt.
Hilt is an opinionated dependency injection library for Android that reduces the boilerplate of using manual DI in your project. Doing manual dependency injection requires constructing every class and its dependencies by hand and using containers to reuse and manage dependencies.
In Dagger, we create scope annotations such as ActivityScope, FragmentScope to specify the lifecycle, but hilt provides us with core components such as Application, Activity, Fragment, Service, and View.
As I mentioned before Dagger/Hilt has a significant impact on build time due to code generation. On the other hand, Koin also affects time, but not build, but runtime. Koin has slightly worse runtime performance, because it resolves dependencies at runtime.
If you're trying to include Hilt in an android library, then you should expect the android app (client of your library) to mark its Application
with @HiltAndroidApp
.
You should include your whole setup (entry points, modules, dependencies, ... whatever you want to have in your library) in the library module, and make the requirement for the client of the library to use the @HiltAndroidApp
to use your library correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With