Guide to Understand HILT In Android

Rajith Prasath
5 min readMay 24, 2021

Hello there reader! looks like you landed on my page seeking answers for your questions and doubts regarding ‘Dependency Injection’. Here I have answered most of the frequently asked questions and answers which is going to help you understand Dependency Injection specially in Android.

Q: What is Dependency handling?

Dependency Injection is a common technique used in software projects where there are dependencies among the classes. This helps in decoupling the usage of an object from its creation point, that is the point of creation and point of usage are separate. The main goal here is to improve the reusability, lay a strong architecture ground for the app and decrease the frequency of the changing required for the usage of the classes.

Q: Manual handling of Dependency injection, yay or nay?

In a Manual handling dependency from different classes are created, provided, and managed, without relying on a library. Handling dependency injection manually, even with many of the existing dependency injection libraries that are available today, requires a lot of boilerplate code as the scope of the project becomes larger, since each class and its dependencies must be constructed by hand, and create containers to reuse and manage dependencies, manual handling is not the most viable option. Therefore, similar to any other techniques related to Software. Dependency Injection for Android too has a history of improvement and changes in the approach.

Q: What possible options are there to handle Dependency Injection ?

The below are the main libraries present in the current for Dependency Injection;

  • Dagger — Created by Square.
  • Dagger2 — Build on top of Dagger, created by Google Team
  • Koin — it’s becoming popular as well completely written in Kotlin.
  • Hilt — build on top of Dagger, completely focused on Android.

Q: What is the best approach to handle Dependency Injection in Android?

There are two major ways to handle Dependency Injection in Android:

Constructor Injection : Dependencies of a class will be passed to its constructor.

Field Injection (or Setter Injection): Certain Android framework classes such as activities and fragments are instantiated by the system, so constructor injection is not possible. With field injection, dependencies are instantiated after the class is created.

When Dagger was introduced Modules, Components, Scopes were created on own and then the android components must be added to these components. In an instance when a dependency is required, modules must be created and passed to the components and then at required places and utilize when required.

Q: Why HILT ?

On the other hand, with HILT(now which is a standard way for android developers to implement Dependency Injection), the manual creation of Components and Modules is prevented. HILT will take care of creating all required Components or dependencies, and by doing this it will make Dependency Injection implementation easier, and then when required dependencies can be utilized via Annotations.

Q: How to setup HILT?

First, add the hilt-android-gradle-plugin plugin to your project's root build.gradle file:

buildscript {
...
ext.hilt_version = '2.33-beta'
dependencies {
...
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
}
}

Then, apply the Gradle plugin and add these dependencies in your app/build.gradle file:

...
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
android {
...
}
dependencies {
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"
}

Hilt uses Java 8 features. To enable Java 8 in your project, add the following to the app/build.gradle file:

android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

Q: Can you give me HILT Annotations please?

Below are some of the sample Annotations provided for Hilt:-

@HiltAndroidApp: This annotation enables member injection field and method injection in the Application class. It will trigger the Hilt code generation and in the process will create our App Component. When Dagger was still in place the main class must be created and the other components must be added manually. Hilt makes life so much easier.

@AndroidEntryPoint: This annotation will help Hilt generate a DI Container for each Android Component so respective components can add their dependencies.

@Inject: This annotation will help to do a Constructor & Field Injection or we can inject many other method. Dependencies injected with this annotation will get added into respective @AndroidEntryPoint.However the fields can not be private.

@ViewModelInject: This will create a dependency for ViewModel and Hilt will return it later.

@ Module: This is used in a similar way it is used in Dagger Modules, when we want to create an object for any component dependency, mostly used for the third party or where we can not do constructor or field injection(classes which we don’t own)

@Singleton: This will create a singleton instance for a dependency, which will be shared across the application.

@ApplicationContext: This is used to access predefined Application Context, the same way we can also use @ActivityContext to access activity context.

Q: I understand the Annotations, but can you help me to use HILT in the code?

Mostly we tend to use, Dependency of any class into another and create some tight coupling which leads to code chaos.

This is where HILT Magic happens,

As the initial step we will have to prepare our app to use HILT. Below code shows adding @HiltAndroidApp annotations into the Application class;

And now the app will be ready to create modules and inject into the AppComponent.

Q: I have now completed the pre configurations, how can I create Dependency modules and add into HILT’s default AppComponent?

Let’s take networking call making as a sample scenario and make a NetworkModule where we will define all the dependencies to make any API calls using Retrofit.

Similar to the above we can create any Module with any Dependency.

  • For creating a module use annotation — @Module
  • To add this module into App’s default Dependency Graph Tree, use annotation — @InstallIn(ApplicationComponent::class)

Q: What dependencies can we use to use HILT with ViewModels?

after adding these dependencies, sync the Gradle file and build the project.

Q: How can we use HILT to create ViewModels and use it in the Activity?

@ViewModelInject annotation can be used while writing constructor for a view model and this annotation will provide its instance and will add it into Dependency Graph View.

Add @AndroidEntryPoint annotation since we need to add this annotation to make it accessible all dependencies from DI Graph created using HILT, And then we can use it inside our views(activity/fragment) like this below:

Voila!!! this is how we can use HILT to make our lives easier, Hope this helps you. HAPPY CODNG!!!

--

--