Fast Lane from Dagger2 to Hilt — Dependency Injection

Akash Patel
Mindful Engineering
3 min readOct 19, 2020
By MindInventory

We have seen Dependency Injection using Dagger2 in our previous blog Fast Lane to Dagger2. Now Google has introduced Hilt, Which is built on top of the Dagger dependency injection library, providing a standard way to incorporate Dagger into an Android application, It is like dream come true for Android Developers, Let us get into details.

In this article, we will see what is Hilt and how to implement it.

Hilt is Jetpack’s recommended library for dependency injection in Android to reduce the boilerplate in your project. without dependency injection, you need to manage and construct classes manually. it’s a time-consuming and lengthy code process, also required more testing.

Using manual dependency

  • A lot of boilerplate code and code duplication are needed to create instances of classes.
  • Code execution should be in a fixed order. Like, Need to make sure that we initialize the repository before ViewModel.
  • It is difficult to maintain reusability, it makes writing test cases complicated.

Using Dagger-Hilt

Make same Dagger code easy.

Different set of bindings for different build type.

Just inject dependancies where needed and rest will be generated by Hilt itself by using annotations.

Makes code testable.

Let’s see the steps of using it:

Adding gradle dependencies

  1. Add the hilt-android-gradle-plugin plugin to your project's root build.gradle file:
buildscript {
...
dependencies {
...
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'
}
}

2. Then, apply the Gradle plugin and add these dependencies in your app/build.gradle file.

Hilt uses Java 8 features, enable Java 8 for your project:

...
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation "com.google.dagger:hilt-android:2.28-alpha"
kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
}
built-in set of components

Hilt Design APIs

  • Components
  • Hilt Application
  • Android entry points
  • Modules

Hilt Application

To use Hilt code generation in your app, first you need to enable Hilt by annotating Application class with the @HiltAndroidApp

@HiltAndroidApp
class HiltSampleApp : Application() {
}

Components

Unlike Dagger, Hilt comes with a built-in set of components that are automatically integrated into the various lifecycles of an Android application.

@HiltAndroidApp will generate the parent component of your app. it will be attached with the application lifecycle.

Android entry points

Above we setup Hilt in Application class to generate application-level components. now Hilt can provide dependencies to other Android classes that have the @AndroidEntryPoint annotation.

@AndroidEntryPoint
class MainActivity : AppCompatActivity() { ... }

Hilt supports the following Android classes

  • Application (by using @HiltAndroidApp)
  • Activity
  • Fragment
  • View
  • Service
  • BroadcastReceiver

We must have to annotate all dependent Android classes. Like, if we annotate Fragment with @AndroidEntryPoint then must annotate all activities where you use that fragment, same for views and other Android classes.

Modules

Hilt module is a class that is annotated with @Module. same as Dagger, it provides the instance of types.

Sometimes we can’t construct classes from external libraries (like Retrofit & Room database) or require fixed patterns to create instances.

Using @Provides annotation on function, we can tell Hilt how to provide instances of this type by creating a function inside a Hilt module and annotating that function with @Provides.

For each Android class in which you 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.

Here is the link for the example

https://github.com/Mindinventory/Hilt-Architecture-Sample

In the next kist, we will see more advanced details about Hilt. Stay tuned with us.

--

--

Akash Patel
Mindful Engineering

Android | ReactNative | ARCore | Team lead @mindinventory