Hilt Dagger Android Tutorial

New Dependency Injection library for android

Image for post
Image for post
Photo by Yudiz.com

Dependency Injection(DI) — is a technique in which an object receives other objects that it depends on. DI can be implemented in any programming language. The general concept behind dependency injection is called inversion of control.

In android development, we integrate a lot of dependencies most of the time, e.g. objects etc. We use the Dependency Injection framework to manage those dependencies.

Over these past few years, a lot of dependency injection framework has been developed. One of the popular for Android is Dagger.

Image for post
Image for post
Photo by Tenor

Dagger — is arguably the most used dependency injection for android. Many android projects use it to simplify building and providing dependencies across the application but to set up requires a large amount of boilerplate code.

Currently, a new dependency injection library for android called Hilt Dagger was release as a part of the jetpack libraries. It reduces the boilerplate of doing manual dependency injection in your project. It provides a standard way to use DI in your application by providing containers for every android class in your project and managing their life cycles automatically. Currently hilt is on alpha stage release.

The goals of hilt:

  • To simplify dagger-related infrastructure for android apps.

Hilt current supports the following Android classes:

  • Application

Generated components for android classes

For each Android class that can perform field injection, there’s an associated Hilt component that you can refer to in the @InstallIn annotation. Each Hilt component is responsible for injecting its bindings into the corresponding Android class.

Hilt provides the following components:

Image for post
Image for post
Photo by Benidict Dulce

Since we already know what is Hilt, its purpose and its components. Let’s jump to the sample.

I’ll show you how to set up the Hilt dagger below.

First, we need to set up Hilt dependencies in our build gradle. Add these dependencies in your app build.gradle.

Then add this plugin at the top of your build.gradle file.

Next, we will add the following classpath in the project build.gradle file

Lastly, sync your gradle.

Once your gradle is successfully synced, let’s start the Hilt set up in your application. We will use clean architecture and MVVM architecture for this example.

First, create an Application class.

And annotate the @HiltAndroidApp in the application class, so your application class will look like this.

This is mandatory since it generates all the component classes which we need while using Hilt dagger.

Second, create a repository class that is also called data gateway and repository interface and create a sample function that returns a string.

Then implement this interface class into our data gateway class.

Once the gateway class was created, add a package module then create a GatewayModule class. Below will be the instance of your TestDataGateway class here.

Our GatewayModule class will look like in the above sample. Since we used ApplicationComponent, the scope of our TestDataGateway has the lifetime of the application. But if you want to limit the scope of our TestDataGateway to Activity, you can use ActivityComponent instead.

Third, create an interactor class which we sometimes call a use case class. The purpose of our use case class is to invoke the data from our data gateway class and consume into our viewmodel class. You can do your data manipulation in usecase or if you want, you can call the data gateway class directly into our viewmodel. In my case, I prefer to have a use case.

You can check out in this link. Why we need usecase/interactor.

Now, let’s create a usecase class.

Since we need to call the data gateway in our usecase class, we need to inject our gateway interface into the constructor of our LoadTestUseCase.

Hooray! We have now completed our set up for our data layer. We will proceed next to the view of our application.

In our Activity or Fragment class, annotate the @AndroidEntryPoint. It generates an individual Hilt component for each Android class in your project. These components receive dependencies from their respective parent classes as described in Component hierarchy.

Annotate entry point then inject our view model in our activity. But first, we need to create a viewmodel class.

Before we inject our viewmodel class into our activity/fragment, we will need to provide ViewModel Factory as a discussed here. There’s so many things that is needed to be set up, but with Hilt dagger everything is simplified.

Say Hi to @ViewModelInject

Check out the link below for the initial set up.

To use @ViewModelInject, we need to add two additional dependencies in our app build.gradle.

After adding those dependencies, in your viewmodel, just add @ViewModelInject to the constructor.

And finally, we will inject our view model into our activity.

As you can see, we don’t need to create a ViewModel Factory to inject our view model into activity.

Yehey! No ViewModel Factory needed!

Image for post
Image for post

You no longer need the ViewModel Factory, All you need to do is to use viewModels() directly into our view.

You can check the full source code below. It also has an api integration sample.

I HOPE THIS WILL HELP YOU GUYS, HAPPY CODING!

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store