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"
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"
}
Step 2. Initialize Hilt.
@HiltAndroidApp
class MyApplication : Application()
Step 3. Inject into Activities or Fragments.
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject lateinit var myRepository: MyRepository
}
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)
}
}
Hilt & ViewModel Integration
Inject dependencies in the ViewModel using Hilt.
@HiltViewModel
class MyViewModel @Inject constructor(
private val repository: MyRepository
) : ViewModel()
And use it in a Fragment/Activity.
@AndroidEntryPoint
class MyFragment : Fragment() {
private val viewModel: MyViewModel by viewModels()
}
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.