21st Sep 2020

Hilt- A new and easy way to use Dagger

Mobile App

Written By, Jignesh Shakya

What is Hilt?

The Hilt is a Dependency injection library. It helps to reduce boilerplate code. In Hilt, we have containers for every class in android. Hilt is built on dagger which means it has advantages of Dagger like compile-time correctness, runtime performance, scalability, and studio support. The hilt is a pure DI, it’s not a Service locator like Koin. Don’t know the difference between DI and Service locator? So here we go.

Difference Between service Locator and DI

DI ContainerService locator
We have to use a container to create all of objectsHere the client code request the dependencies (Objects)
DI Container is located somewhere outside and it takes services from some storage and pushes them to the consumerComputation fees on the Ethereum network.

Getting Started

Firstly we need to add dependency for adding hilt in our android project. So for that just add this in project level build.gradle.

Now add this in your app-level build.gradle

And the last thing, don’t forget to enable Java 8 in your project ashilt use java 8 features

Scoping Instance to Container

Sometimes the scope of an instance matters a lot, so hlit provides scoping annotation to manage the scope of instance. Hlit provides scoping based on its components. Hlit components define the scope for any instance. So here we have a component hierarchy.

scoping-container

So here we can see if we define any instances as singleton scope, then it creates a single object for the whole application, if we define activityScope then it creates a single object per activity and the same conditions apply to fragment and viewscope.

Hilt and Jetpack Support

Yes, you heard it right, Hilt can be used with jetpack components and currently Viewmodel and Workmanger is supported by Hilt. We can use Hilt’s extensions for using Viewmodel and Workmanger with hilt.

Yes! We have successfully completed the setup.

Let’s jump into code

Simple Class Injection

Defining an injectable class is very easy in hlit, You just need to annotate it constructor with @inject annotation.

If you want access to the object you have to create a simple variable and add @Inject annotation, then it will automatically initialize the object when you will use it for the first time. This is called lazy initialization. Don’t forget to add @AndroidEntryPoint at the top of your activity.

That’s it!, we are done with simple class creation and accessing its object with Hlit.

Module Creation and Injection

Let’s say you want to inject an object from an external lib or want to create an object of the interface. How can we use Hilt in this case? For that, we have something called Hilt module. It helps to inject interfaces and classes from external libraries. As you can see we also have @InstallIn annotation to deploy the module.

Now, just simply inject the object using @Inject annotation. It’s damn easy.

ViewModel Demo

Firstly we will create viewmodel and annotate it with @ViewModelInject

Here we are done with ViewModel creation, Now we will create an object of it.
For that will use viewModels() extensions function, and if you want a shared Viewmodel then use activityViewModel(), this both the functions create the object lazily.

Conclusion

Hlit is definitely more powerful and easy to use DI solutions for Android Developers. It resolves dependencies at compile time. So, no more runtime exceptions. Just try it out and have fun.

Written By,

Android Developer at Yudiz Solutions Pvt. Ltd