Android  

Introduction to Dagger Hilt for Android Development

In modern Android development, dependency injection (DI) is a critical design pattern that improves code reusability, scalability, and testability. While Dagger 2 is a powerful DI library, its steep learning curve can be a barrier. That’s where Dagger Hilt comes in — a library built on top of Dagger 2 to simplify DI in Android apps.

What is Dagger Hilt?

Dagger Hilt is a dependency injection framework tailored specifically for Android. It provides,

  • Automatic DI containers for common Android components (Activity, Fragment, ViewModel, etc.)
  • Reduced boilerplate compared to pure Dagger 2
  • Scoped components to control the lifespan of dependencies
  • Easy integration with Jetpack libraries like ViewModel and Navigation

Why Use Hilt?

  • Boilerplate reduction: No need to manually create component interfaces.
  • Lifecycle-aware scopes: It provides built-in component lifecycles tied to Android classes.
  • Built-in ViewModel support: Easier to inject dependencies into ViewModels.
  • Test support: Simplifies dependency replacement during unit or UI testing.

Key Annotations in Hilt

Annotation Purpose
@HiltAndroidApp Initializes Hilt and generates the application-level component
@AndroidEntryPoint Injects dependencies into Android components like Activities, Fragments
@Inject Tells Hilt how to provide a dependency
@Module & @InstallIn Defines how to provide dependencies manually
@Provides Supplies specific objects in modules
@Singleton Defines the scope/lifecycle of dependencies

How to Set Up Dagger Hilt in an Android Project?

Step 1. Add Dependencies.

In your project-level build.gradle.

classpath "com.google.dagger:hilt-android-gradle-plugin:2.48"
Kotlin

In your app-level build.gradle.

plugins {
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
}

dependencies {
    implementation "com.google.dagger:hilt-android:2.48"
    kapt "com.google.dagger:hilt-compiler:2.48"
}
Kotlin

Step 2. Initialize Hilt.

@HiltAndroidApp
class MyApplication : Application()
Kotlin

Step 3. Inject into Activities or Fragments.

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    @Inject lateinit var myRepository: MyRepository
}
Kotlin

Step 4. Provide Custom Dependencies.

@Module
@InstallIn(SingletonComponent::class)
object AppModule {

    @Provides
    @Singleton
    fun provideApiService(): ApiService {
        return Retrofit.Builder()
            .baseUrl("https://api.example.com/")
            .build()
            .create(ApiService::class.java)
    }
}
Kotlin

Hilt & ViewModel Integration

Inject dependencies in the ViewModel using Hilt.

@HiltViewModel
class MyViewModel @Inject constructor(
    private val repository: MyRepository
) : ViewModel()
Kotlin

And use it in a Fragment/Activity.

@AndroidEntryPoint
class MyFragment : Fragment() {
    private val viewModel: MyViewModel by viewModels()
}
Kotlin

Summary

Dagger Hilt is a powerful tool that simplifies dependency injection in Android. By abstracting much of the boilerplate and complexity of Dagger 2, Hilt makes your app easier to develop, maintain, and test. It integrates smoothly with the Android component lifecycle and Jetpack libraries, making it a must-have for modern Android development.

People also reading
  • Understanding the use case of Dagger Hilt in modern Android development.
    Dagger Hilt is a powerful library built on top of Dagger 2 to simplify dependency injection (DI) in Android apps. Its purpose is to harness the power of Dagger 2 while eliminating the steep learning curve. It automates DI containers for common Android components and reduces boilerplate, making it a popular choice amongst developers. This topic will delve deep into why Dagger Hilt is preferred in modern Android development and the problems it solves. Read more
  • How Dagger Hilt aids in reducing boilerplate and increasing code reusability.
    One of the significant advantages of Dagger Hilt is the reduction in boilerplate code. It eliminates the need to manually create component interfaces, leading to cleaner and more reusable code. This topic will discuss how Dagger Hilt can increase code scalability and reusability by reducing boilerplate, making it an integral part of modern Android development. Read more
  • Understanding the role of key annotations in Dagger Hilt.
    Dagger Hilt uses several annotations like @HiltAndroidApp, @AndroidEntryPoint, @Inject, @Module, @InstallIn, @Provides, and @Singleton, each with a unique purpose. Understanding the role of these annotations is crucial in effectively using Dagger Hilt for dependency injection in Android apps. This topic will provide an in-depth understanding of these key annotations and how they contribute to DI. Read more
  • Setting up Dagger Hilt in an Android Project: A Step-by-step Guide.
    Setting up Dagger Hilt in an Android project involves several steps from adding dependencies to initializing Hilt and injecting it into Activities or Fragments. This topic will guide developers through the steps involved in setting up Dagger Hilt in an Android project, making it easier for them to use this powerful tool for dependency injection. Read more
  • Integration of Dagger Hilt with ViewModel in Android development.
    Dagger Hilt provides built-in ViewModel support, making it easier to inject dependencies into ViewModels. This topic will explore how developers can integrate Dagger Hilt with ViewModel in Android development, discussing the process, benefits, and potential challenges. The discussion can also include practical examples to provide a hands-on understanding. Read more