Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hilt using in android library

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.

like image 923
kolboc Avatar asked Aug 04 '20 12:08

kolboc


People also ask

Can I use Hilt in Android library?

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.

What is the use of Hilt in Android?

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.

What is difference between Hilt and Dagger?

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.

Is KOIN better than Hilt?

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.


1 Answers

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.

like image 58
Bartek Lipinski Avatar answered Oct 02 '22 10:10

Bartek Lipinski