---
title: "Using Dagger Hilt In Android Applications"
description: "How to use dagger-hilt to improve your workflow when working on Android applications"
canonical: https://oozou.com/blog/using-dagger-hilt-in-android-applications-103
locale: en
date: 2020-11-14
author: "OOZOU Team"
---

# Using Dagger Hilt In Android Applications

> How to use dagger-hilt to improve your workflow when working on Android applications

Published: 2020-11-14 · Author: OOZOU Team (Engineering) · 3 min read

I'll be assuming the reader has a solid grasp of the basic knowledge of kotlin, dagger and dagger-android.

# The Current Situation With Dependency Injection

If you've been around long enough in android development, you've probably used tools like dagger and dagger-android to handle dependency injection in your applications. I'm pretty sure we are all too familiar with the amount of code we have to write to setup our dependencies. You've probably had to create an ApplicationComponent Interface, multiple dagger modules for your activities/fragments and yet you still needed to wire up factories so you can inject your view models. Apparently that is also not the last of your troubles, you also have to repeat all of this processes again in order to get any meaningful integration testing done. Clearly the dagger-android library can be begin to feel like a burden.

# Why Use Hilt?

The dagger-hilt library addresses virtually all the problems listed in the above section. The following are some of the benefits of using hilt:

- Reduced boilerplate
- Decoupled build dependencies
- Simplified configuration
- Improved testing
- Standardized components

Want to know more about the benefits of Hilt, checkout [Why Hilt on Dagger Dev](https://dagger.dev/hilt/benefits)

# Talk Is Cheap, Show Me The Code

To use Hilt, add the following build dependencies to the Android Gradle module’s build.gradle file:

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

dependencies {
  implementation 'com.google.dagger:hilt-android:<VERSION>'
  kapt 'com.google.dagger:hilt-android-compiler:<VERSION>'
  // For instrumentation tests
  androidTestImplementation 'com.google.dagger:hilt-android-testing:2.28-alpha'
  kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.28-alpha'
  // For local unit tests
  testImplementation 'com.google.dagger:hilt-android-testing:2.28-alpha'
  kaptTest 'com.google.dagger:hilt-android-compiler:2.28-alpha'
}

kapt {
  correctErrorTypes true
}
```

Add the following to your project's build.gradle

```kotlin
buildscript {
  repositories {
    <em>// other repositories...</em>
    mavenCentral()
  }
  dependencies {
    <em>// other plugins...</em>
    classpath 'com.google.dagger:hilt-android-gradle-plugin:<version>'
  }
}
```

**Hilt Implementation**
All apps using Hilt _must_ contain an [Application](https://developer.android.com/reference/android/app/Application.html) class annotated with [@HiltAndroidApp](https://dagger.dev/api/latest/dagger/hilt/android/HiltAndroidApp.html). @HiltAndroidApp kicks off the code generation of the [Hilt components](https://dagger.dev/hilt/components.html) and also generates a base class for your application that uses those generated components

```kotlin
@HiltAndroidApp
public final class MyApplication extends Application {}
```

To enable members injection in your activity, annotate your class with [@AndroidEntryPoint](https://dagger.dev/api/latest/dagger/hilt/android/AndroidEntryPoint.html).

```kotlin
@AndroidEntryPoint
public final class MainActivity extends MyBaseActivity {
  @Inject Foo foo; <em>// Bindings in SingletonComponent or ActivityComponent</em>

  @Override
  public void onCreate() {
    <em>// Injection happens in super.onCreate().</em>
    super.onCreate();

    <em>// Do something with foo ...</em>
  }
}
```

# Using Hilt With ViewModels

Hilt includes extensions for providing classes from other Jetpack libraries. Hilt currently supports the following Jetpack components: ViewModels and WorkManager
Add the following to app/build.gradle

```kotlin
dependencies {
  ...
  implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01'
  kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'
}
```

Provide a ViewModel using the @ViewModelInject annotation in the ViewModel object's constructor. You must also annotate the SavedStateHandle dependency with @Assisted:

```kotlin
class ExampleViewModel @ViewModelInject constructor(
  private val repository: ExampleRepository,
  @Assisted private val savedStateHandle: SavedStateHandle
) : ViewModel() {
  ...
}
```

Then, an activity or a fragment that is annotated with @AndroidEntryPoint can get the ViewModel instance as normal using ViewModelProvider or the by viewModels()

```kotlin
@AndroidEntryPoint
class ExampleActivity : AppCompatActivity() {
  private val exampleViewModel: ExampleViewModel by viewModels()
  ...
}
```

# Hilt Modules

Hilt modules are standard Dagger modules that have an additional [@InstallIn](https://dagger.dev/api/latest/dagger/hilt/InstallIn.html) annotation that determines which [Hilt component(s)](https://dagger.dev/hilt/components.html#hilt-components) to install the module into.

```kotlin
@Module
@InstallIn(SingletonComponent.class) <em>// Installs FooModule in the generate SingletonComponent.</em>
public final class FooModule {
  @Provides
  static Foo provideFoo() {...}
}
```

Hurray!!! You are ready to start using hilt in your android application. Personally, I felt the implementation of Hilt is easy when compared to dagger. If you feel the same after testing the above, please give it a try in your next project.

---

Contact OOZOU: hello@oozou.com · +66 2544 0000 · https://oozou.com/contact

More for AI agents: https://oozou.com/llms.txt · every page has a markdown twin at its URL plus ".md"
