Hilt ‘A new way of Dependency Injection’ in Android Part-2
By: Sachin Rajput
Tags: Android, Android Hilt, Dagger Hilt, Android App Development, Kotlin

Dagger Hilt

This blog is in continuation of the previous post Part 1

Directly take me to code (app in action) Part 3

In this part, We will be seeing how to make a simple app using Dagger HILT.

Dive into code (using HILT)

well, mostly what task we do where we use Dependency of any class into another and create some tight coupling.

a simple answer can be When we try to make an API call, we create lots of references of OkHttp, then Gson then Retrofit Service, and then after creating a lot of dependencies we make a call :)

Let's take this approach and see how HILT can help us.

if you have used Dagger2 of heard about modules and components and a lot of code to write each one of those :) Hilt will help in overcoming all of that.

So The first point is how to make our app ready to use HILT. so let's do this by adding @HiltAndroidApp annotations into our Application class.

adding this annotation to our Application class will be the starting point, and now our app will be ready to create modules and inject into our AppComponent.

While using Dagger2 we used to create a main class i.e. like AppComponent and includes all our modules manually to into this AppComponent. Something like this

and then we used to inject this into the Application class’s onCreate() method.

but using HILT all this is not needed anymore.

because HILT provides us ApplicationComponent by default.

so we just need to create our dependency modules and we can directly inject into this provided ApplicationComponent class.

now How to create Dependency modules and add into HILT’s default ApplicationComponent?

Well, let's take our networking call making scenario to continue with, So we can create a Module like this below, for example, let's make a NetworkModule where we will define all the dependencies to make any API calls using Retrofit.

like this, we can create any module with any dependency.

and for creating a module use annotation — @Module

and to add this module into App’s default Dependency Graph Tree, we need to use annotation — @InstallIn(ApplicationComponent::class)

this is basically it, now we can see and understand how much HILT can help us over Dagger2.

HILT comes with Android Jetpack Components compatibility also.

to use Hilt with ViewModels. we can use these dependencies.

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

then now how we can use HILT to create ViewModels and use it in our Activity.

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

and then we can use it inside our views(activity/fragment) like this below:

First of all, add @AndroidEntryPoint annotation we need to add this annotation to make it accessible all dependencies from DI Graph created using HILT.

So this is some simple steps which we can follow to use HILT over Dagger2 to make our codebase much simple :)

In the next part , we will see one real app with the help of Hilt DI.

Next part — App in action Part 3

If you like this hit clap. Happy Coding.