A dependency injection library for Android built on top of Dagger.
Quick Start
dependencies {
implementation("com.google.dagger:hilt-android:2.44")
kapt("com.google.dagger:hilt-compiler:2.44")
}dependencies {
implementation 'com.google.dagger:hilt-android:2.44'
kapt 'com.google.dagger:hilt-compiler:2.44'
}Setup Steps
- Add the Hilt Gradle plugin to your project's root build.gradle.
- Apply the Hilt plugin in your app's build.gradle file.
- Annotate your Application class with @HiltAndroidApp.
- Use @AndroidEntryPoint for activities, fragments, or other Android components.
Overview
Hilt is a dependency injection library for Android that reduces the boilerplate of doing manual dependency injection in your project. Built on top of Dagger, it provides a standard way to incorporate Dagger dependency injection into an Android application, offering predefined components and seamless integration with Android Jetpack.
Features
Use Cases
When Not to Use
Usage Examples
@HiltAndroidApp
class MyApplication : Application()Annotate the Application class to enable Hilt throughout the app.
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject lateinit var repository: MyRepository
}Use @AndroidEntryPoint for Android components and @Inject to provide dependencies.
Common Pitfalls
- Forgetting to apply the Hilt Gradle plugin or add kapt dependencies
- Mixing Hilt with manual Dagger components without proper configuration
- Not understanding Hilt's scope and lifecycle can lead to memory leaks or incorrect injections
Integration
Works well with
- Hilt provides built-in support for many Android Jetpack libraries, making it easy to inject dependencies into ViewModels, databases, and more.
Alternatives
Koin
A lightweight dependency injection framework that uses a pure Kotlin DSL and doesn't require code generation, suitable for simpler projects.
Dagger
The underlying library that Hilt is built on; use Dagger directly for more control and customization in complex scenarios.
Maintenance & Health
- Latest version, Maven coordinates, repository URL, docs URL, and website URL are set to null as the exact details may change; please refer to official Android developer resources for up-to-date information.
- SDK versions (min_sdk, compile_sdk, target_sdk) are set to null as they depend on the specific project configuration and Hilt version.