A dependency injection library for Android built on top of Dagger.

Apache 2.0

Quick Start

Gradle (Kotlin DSL)
dependencies {
    implementation("com.google.dagger:hilt-android:2.44")
    kapt("com.google.dagger:hilt-compiler:2.44")
}
Gradle (Groovy)
dependencies {
    implementation 'com.google.dagger:hilt-android:2.44'
    kapt 'com.google.dagger:hilt-compiler:2.44'
}

Setup Steps

  1. Add the Hilt Gradle plugin to your project's root build.gradle.
  2. Apply the Hilt plugin in your app's build.gradle file.
  3. Annotate your Application class with @HiltAndroidApp.
  4. 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

Simplifies Dagger setup and configuration
Provides predefined components for Android lifecycle (e.g., Application, Activity, Fragment)
Reduces boilerplate code with annotations
Integrates seamlessly with Android Jetpack libraries like ViewModel and Room

Use Cases

Large Android applications with complex dependency graphs
Projects using MVVM, MVI, or other clean architecture patterns
Teams looking to standardize and simplify dependency injection across the codebase

When Not to Use

Small projects with minimal or no dependency injection needs
When using alternative DI frameworks like Koin or manual Dagger
For non-Android Kotlin or Java projects where Hilt's Android-specific features are not required

Usage Examples

Basic Hilt Setup in Application Class
@HiltAndroidApp
class MyApplication : Application()

Annotate the Application class to enable Hilt throughout the app.

Injecting Dependencies into an Activity
@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

ViewModelRoomRetrofitWorkManagerNavigation
  • 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.

Related libraries