Jetpack compose — Dependency injection with Dagger/HILT

Hardik P
Canopas
Published in
4 min readDec 24, 2021

--

Dependency injection (DI) is a technique widely used in programming and well suited to Android development, where dependencies are provided to a class instead of creating them itself.

HILT is a dependency injection library for Android that reduces the boilerplate of doing manual dependency injection in your project.

HILT provides a standard way to use DI in your application by providing containers for every Android class in your project and managing their lifecycles automatically. HILT is built on top of the popular DI library Dagger to benefit from the compile-time correctness, runtime performance, scalability, and Android Studio support that Dagger provides.

As HILT is built on top of Dagger dependency injection library. Compare to Dagger the goals of HILT are as follows:

To simplify Dagger-related infrastructure for Android apps.

To create a standard set of components and scopes to ease setup, readability, and code sharing between apps.

To provide an easy way to provision different bindings to various build types, such as testing, debug, or release.

HILT is integrated with Jetpack libraries and Android framework classes and removes most of the boilerplate to let you just focus on important parts.HILT automatically generates and provides:

Components for integrating Android framework classes with Dagger that you would otherwise need to create by hand.

Scope annotations for the components that HILT generates automatically.

Predefined bindings and qualifiers.

Integrating Dagger/HILT into project:

To setup HILT in the project, we need to add the following dependencies in app level build.gradle file,

Then as a next step, we need to apply the Dagger/HILT plugin at the top of the app level build.gradle file like this:

and finally, we need to add the following classpath at project level build , gradle like this:

This is the required setup to get started with Dagger/HILT.

Setting up Dagger/Hilt:

Step 1:

We will fist add Application class AppClass like this:

and we will update the Manifest file like this:

now, enable HILT in our app by annotating our application class with @HiltAndroidApp to trigger HILT’s code generation:

The above step is a mandatory one, it generates all the component classes which we have to do manually while using Dagger.

Step 2:

Now, we will add the dependency for retrofit as we want to fetch data from API. Coil is used as a Image Loading library
we are going to use this API , to fetch movie data.

Our data class for Movie will look like this:

Our ApiService will look like this:

Step 3:

Now, we will create Networkmodule object , here we need not to create ApplicationComponent as we are going to use the one provided by Dagger/HILT.

Here @Module annotation will help HILT to understand that this class is a module.

Now, we need to plug this module class in the specific component. In our case we need to add this at application level so we will install it in SingleComponent like this:

Here, we have used @InstallIn annotation to install it in SingletonComponent which is provided by Dagger/HILT.

To know more about HILT components and how they are used click here.

Step 4:
Now, inside out NetworkModule, we will provide all the dependencies one by one, our Network module will look like this:

Here, we have provided two dependencies, Retrofit (which will be used by provideApiService) and provideApiService.

Dependencies are provided using @Provides annotation. @Singleton annotation helps the instance to be created and used once across the whole app.

Step 5:
Now , as everything is setup we can inject the dependencies into the classes.

To make an Android class supported by Dagger-HILT we use,

Here, @AndroidEntryPoint means Dagger/HILT can now inject dependecies in this class.

@AndroidEntryPoint annotation can be used in Activity

  1. Activity
  2. Fragment
  3. View
  4. Service
  5. BroadcastReceiver

Inside our MainActivity we have used hiltViewModel, and instance of our viewModel can be created with the help of @AndroidEntryPoint

And Finally our mainViewModel will look like this:

Here, we can see we have used @Inject annotation in the ViewModel object’s constructor to provide ApiService Dependency. Here, HiltViewModel will be used to generate instance of viewModel.

This way we can Inject dependency using Dagger/HILT inside our app.

You can find complete Jetpack compose with Dagger/HILT project here.

Hope this will help you start with Dagger/HILT.

Thanks for your support!
If you like what you read, be sure to 👏it below — as a writer it means the world!
Follow Canopas Software to get updates on interesting tech articles!

Happy coding!

--

--