From Dagger to Hilt Dependency Injection
Recommended Dependency Injection by Google as Android Jetpack Component
Yesterday Google launched the Android 11 beta channel. There are many sessions that inform about new things about Android 11. Of the many sessions described, I am very concerned about what’s new Android Jetpack Component. During that session, hilt dependency injection was introduced.
Honestly, I was very surprised when I heard this, because, hilt dependency injection summarizes the code more concise than the dagger and this a recommended dependency injection by google.
In this article, I will address you later on a case study of using hilt dependency injection in a simple todo list application. Oh yeah, for more about what’s is new in android jectpack component you can watch this video
In this article I will not discuss dependency injection in general, but I will discuss about hilt dependecy injection and its case study.
Introduction
Hilt provides a standard way to do DI injection in your application by providing containers to every Android component in your project and managing the container’s lifecycle automatically for you. by Codelabs
So hilt dependency injection is a standard to use dependency injection in you android project. And hilt has similarities with dagger, because he built on dagger2, but hilt has more concise code than dagger.
Why you use it than dagger ?
In my view there are some things that make hilt better than a dagger. Firstly,
Maybe because hilt is a DI recommended by Google, why? because everything that is recommended by google its can increase productivity and develop android better. Secondly, Simple than dagger2, this is the most underlying reason why use it than dagger.
It should be noted, this is only my view but if there are other opinions that differ, NO Problem.
From earlier I said that hilt is simpler than a dagger. Okay, hilt reduces the boilerplate code that is involved in using Dagger in an Android application.Hilt automatically generates and provides the following:
- Components for integrating Android framework classes with Dagger that you would otherwise need to create by hand.
- Scope annotations to use with the components that Hilt generates automatically.
- Predefined bindings to represent Android classes such as
Application
orActivity
. - Predefined qualifiers to represent
@ApplicationContext
and@ActivityContext
.
If you still don’t understand, let’s take a case study that I’m going to explain
How to Hilt dependency Injection ?
Okay, i will show you to use Hilt DI. Firstly you must make an android project with Android Studio 4.0 version or higher, because we need build.gradle 4.0.0 version which only support in Android Studio 4.0. If you not have it, you can download in Android Studio Official Website in this link.
Note: I will not show to use room database, recycler view, and more. But I will Focused to describe how to use Hilt in this project and some comparison hilt and dagger
For complete source code of this project you can visit my github repository
Setup Project
After that add library Hilt, Hilt for ViewModel, Room database, and more, which we need it in this project.
Sync your project and wait until done. Make sure the arrangement of your project as follows
Application class
App
class is used and initialised, to add a container that is attached to the app's lifecycle,In application class add @HiltAndroidApp
annotation to triggers Hilt’s code generation including a base class for your application that can use dependency injection
Its very simple than dagger, you just add @HiltAndroidApp
annotation.
Database Module
this class placed in package di
the file called DatabaseModule.kt. In dagger if you want to create a module you must create AppComponent
and then you include must your module. But in Hilt, AppComponent will be auto generate and to use it you just add @InstallIn(ApplicationComponent::class)
in above like below
You can annotate a function with @Provides
in Hilt modules to tell Hilt how to provide types that cannot be constructor injected
Bind ViewModel
if you want to use viewmodel you must add library viewmodel for hilt (check it in Setup Project). In dagger, to add parameter in viewmodel, you must add ViewModelFactory
, ViewModelModule
, and ViewModelKey.
Now, it so much boilerplate. If you use Hilt dependency injection you will not find that. you just inject your viewmodel construtor with @ViewModelInject
annotation.
And don’t forget to add @Assisted private val savedStateHandle: SavedStateHandle .
I dont no why must add it but you can visit the official web documentation in this link, i just following that
MainActivity
Once Hilt is set up in your Application
class and an application-level component is available, Hilt can provide dependencies to other Android classes that have the @AndroidEntryPoint
annotation
From Hilt Documentation, Hilt currently supports the following Android classes:
Application
(by using@HiltAndroidApp
)Activity
Fragment
View
Service
BroadcastReceiver
So if you annotate @AndroidEntryPoint
an android classes, you must also add annotate in Android classes that depend on it. If you annotate the Fragment you must also annotate the activity where you use the fragment
Consclusion
So guys, that is very easy and simple more than dagger DI. we dont need AppComponent, ViewModelModule, and ViewModelFactory in this project. Because Hilt automatically generates and provides it.
Cheat Sheet
The Cheat Sheet will you help you if you want to use Hilt dependency injection. follow link to download the pdf file link
Learn More
For learn more about hilt dependency injection you can visit google codelab. In there will fully describe how to use hilt
Or you can visit the official documentation of hilt DI