A Brief Intro to Dagger Hilt

Karlo Miguel
The Startup
Published in
3 min readJul 21, 2020
Photo by Jonathan Kemper on Unsplash

Dependency injection (DI), in a nutshell, is a technique whereby one object provides or supplies the dependencies of another instead of creating it by themselves. This will let you have good architecture for your application and better code reusability.

In Android, Dagger is the dependency injection library that is recommended by Google for dependency injection. But let’s face it, there are a lot of overheads when setting up Dagger that beginners might shy away from.

This is where Dagger Hilt for Android comes in.

What is Dagger Hilt?

Hilt, built on top of Dagger, is now the new recommended DI library from Google which would help to simplify setting up Dagger in your applications.

The previous Components that we created in our app, exists now in the predefined components that are being provided to us, out of the box, by Hilt. So we won’t need to define these directly. The built-in components are also automatically integrated into the lifecycle of your app, which is really neat.

To check out these components, take a look here.

The Setup

We first need to add the following to our project level build.gradle

classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'

Then in our module level build.gradle

apply plugin: 'dagger.hilt.android.plugin'

Finally, we can now add the required dependencies for us to use Hilt

implementation 'com.google.dagger:hilt-android:2.28-alpha'
kapt 'com.google.dagger:hilt-android-compiler:2.28-alpha'
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01'
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'

When everything is synced up, we now need to annotate our application class with @HiltAndroidApp, this will generate the different Hilt components along with the base class of our application.

For our Activity/Fragment we would annotate it with@AndroidEntryPoint, this will generate the required component that will receive the dependencies that are going to be injected in our Activity/Fragment.

In cases that we have a class that has a required constructor argument

class SampleClass (val anotherSampleClass: AnotherSampleClass) {}

We can use Hilt modules to inject the required argument. Take a look at the sample below.

@Module
@InstallIn(ActivityComponent::class)
class SampleModule {

@Provides
fun provideAnotherSampleClass(...: ...) = AnotherSampleClass(...)
}

Hilt reuses Dagger’s @Module annotation but in Hilt, instead of specifying which modules the component will use, we specify in each module that we create in which component it will be installed in, @InstallIn(...Component::class) and we would still use @Provides and @Binds annotation as to how we use to before.

ViewModel

Any ViewModel that is annotated with @ViewModelInject constructor() can be provided from the ViewModelFactory of Hilt. This will generate, behind the scenes, the respective factories. The ViewModel can also receive the SavedStateHandle using the annotation @Assisted, without any more configuration. Sweet!

class SampleViewModel @ViewModelInject constructor(
val argument: AnotherSampleClass,
@Assisted val savedStateHandle: SavedStateHandle
) : ViewModel() {}

And in our Activity/Fragment, we can get the of the view model by ktx extension function.

@AndroidEntryPoint
class SampleFragment : Fragment() {
val viewModel by viewModels<SampleViewModel>()
}

Conclusion

With what we’ve just seen above, we have set up our Android app with DI with the use of Dagger-Hilt. It surely reduced the boilerplate in setting up the whole dependency injection in Android which was quite cumbersome before Hilt came along.

It allowed us to be up and running the DI in no time and with less code. It hides the complexity from us, developers, to let us focus on building other important things.

--

--

Karlo Miguel
The Startup

android developer. Life’s an awesome journey, learn and have fun.