Dagger-Hilt Dependency Injection in Android App: A Beginner’s Guide

Blair Fernandes
3 min readApr 30, 2023

--

a dagger
Photo by Jason Jarrach on Unsplash

Dependency injection (DI) is a powerful concept in software engineering that helps to reduce tight coupling between different components in an application. Dagger-Hilt is a popular DI library for Android that can help simplify the implementation of DI in your Android apps.

In this guide, we will learn the basics of Dagger-Hilt and provide a step-by-step tutorial on how to implement it in an Android Studio project.

What is Dagger-Hilt?

Dagger-Hilt is a dependency injection library built on top of Dagger 2, the popular Java-based DI library. It is designed specifically for Android and is optimized for the Android platform, making it a great choice for developers looking to implement DI in their Android apps. It simplifies the process of dependency injection by reducing boilerplate code and providing a set of annotations that can be used to declare dependencies. Some of the key features of Dagger-Hilt include:

  • Integration with Android framework classes and components
  • Simplified setup and configuration
  • Support for different scopes and lifecycles

Basics of Dagger-Hilt

Dagger-Hilt provides several key annotations that you can use to set up your dependency injection hierarchy. These annotations include:

  • @HiltAndroidApp: This annotation is used to identify the Application class of your app as the entry point for Dagger-Hilt.
  • @Inject: This annotation is used to mark fields, methods, or constructors that should be injected by Dagger-Hilt.
  • @Module: This annotation is used to define a module that provides dependencies to be injected by Dagger-Hilt.
  • @Provides: This annotation is used in conjunction with @Module to define a method that provides a dependency.

We will take an example of a simple app that fetches a list of to-dos from an API and displays them. We will use Dagger-Hilt to inject an instance of the API service in our MainActivity class.

Setting up Dagger-Hilt

To use Dagger-Hilt in our Android app, we first need to add the required dependencies to the project. First, add the following dependency to your app-level build.gradle file:

// Dagger-Hilt
implementation 'com.google.dagger:hilt-android:2.46'
kapt 'com.google.dagger:hilt-compiler:2.46'

Next, add the following lines to your project-level build.gradle file:

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.46'
}
}

Note: The versions of the dependencies may change. Check the latest versions here.

Using Dagger-Hilt in your App

  1. Annotate the application class with the @HiltAndroidApp annotation to enable Dagger-Hilt for the app. If you don’t already have an application class you can create a new Kotlin class and annotate it as follows. This will generate the necessary code for Dagger-Hilt to work.
@HiltAndroidApp
class MyApplication: Application()

2. Create a data class called to represent a single to-do item and an interface with a method to fetch data from the API.

//Todo.kt
data class Todo(
val userId: Int,
val id: Int,
val title: String,
val completed: Boolean
)

//ApiService.kt
interface ApiService {
@GET("/todos")
fun getTodos(): Call<List<Todo>>
}

3. To use Dagger-Hilt in our MainActivity, we can annotate it with the @AndroidEntryPoint annotation. This enables Dagger-Hilt to inject dependencies into the activity. The @Inject annotation is used to mark the property that needs to be injected.

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject
lateinit var apiService: ApiService
...

4. Next, create a module to define the dependencies. Here we create a module called NetworkModule and annotate it with @Module .This module provides the ApiService dependency required by our MainActivity. We use the @Provides annotation to denote the methods providing a dependency.

@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@Provides
fun provideRetrofit(): Retrofit {
return Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com")
.addConverterFactory(GsonConverterFactory.create())
.build()
}

@Provides
fun provideApiService(retrofit: Retrofit): ApiService {
return retrofit.create(ApiService::class.java)
}
}

The @InstallIn annotation specifies the scope where the module is installed, which in this case is the SingletonComponent indicating that these dependencies will available throughout the application lifecycle.

And that’s it! You now have a basic understanding of how to implement dependency injection using Dagger-Hilt in your Android app. You can find the complete code here.

Conclusion

In this tutorial, we covered the basics of Dagger-Hilt and how it can be used in an Android app. We demonstrated how to set up Dagger-Hilt, declare dependencies, and use them in an activity.

--

--

Blair Fernandes

I am a passionate programmer 💻, geek 🤓 & an avid gamer🎮