Hilt — An optimal dependency injector for Android Projects.

Nataraj KR
2 min readAug 11, 2021
Hilt — Dagger with android flavor

Dependency injection is one of the design patterns in software engineering. Using this is of course a choice, but using this will obviously give an edge in maintaining the projects. In programming, sometimes one class has to depend on one or many other classes (aka dependencies) to get the work done. The delegation of providing dependencies is known as Dependency Injection.

In Android even without knowing we use dependency injection. We provide instances of Room database, Retrofit, Shared preferences, etc., across activities or fragments by instantiating them in an Object class. With all the activity, fragment lifecycles around dependency injection have become an inherent part of Android projects. Before Hilt, the dagger was used as a dependency injection framework. Many android components like activity, fragment, service, etc., are initialized by the android system itself, and this makes dagger implementation a nightmare for Android developers. To handle this and reduce boilerplate code dagger.android was released with a few helper classes. Until ViewModel, Workmanagers, and other architecture components were released dagger.android seems to put an end to the nightmare of dagger implementation. With these architecture components being released boilerplate code again got the heads of developers. So to put an end to all of these Hilt, basically dagger but with android flavor, is released.

Hilt, for that matter like any other dependency injection, uses a graph hierarchy to provide the right dependency any at the right place. And all the dependencies have to be placed in the graph for the hilt to provide them. Now that we are a bit familiar with HILT and dependency injection (DI) let’s see how to get started off.

Add the following dependencies to the android project.

Annotations are used by HILT to autogenerate the required classes for DI. A custom implementation of the Application class annotated with ‘HiltAndroidApp’ will kick off the HILT.

All the activities, fragments, other android components have to be annotated with ‘AndroidEntryPoint’ and dependencies should be injected with ‘Inject’.

All other class has to be annotated with ‘Inject’.

All the ViewModels’ should be annotated with ‘HiltViewModel’ and Workers’ should be annotated with ‘HiltWorker’.

These are basic implementations of Hilt in android to get started off with.

Custom implemented dependencies can be injected using Modules.

SingletonComponent is a class generated by HILT. Every Module in HILT has to be annotated with ‘InstallIn’ and a component class has to be provided as its value. This SingletonComponent is added at the top level in the dependency graph so that all the dependencies in it are available throughout the project.

For more about HILT and other annotations, please check out the references.

References:

https://developer.android.com/training/dependency-injection/hilt-android

https://developer.android.com/codelabs/android-hilt#0

--

--