Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsSep 12, 2026·3 min de lecture

Hilt — Modern Dependency Injection for Android

Hilt is Google's recommended dependency injection library for Android, built on top of Dagger. It simplifies DI setup with predefined components and scopes, reducing boilerplate while keeping compile-time safety.

Prêt pour agents

Installation agent prête

Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
Hilt DI for Android
Commande d'installation directe
npx -y tokrepo@latest install 9177116c-ae4a-11f1-9bc6-00163e2b0d79 --target codex

À exécuter après confirmation du plan en dry-run.

Introduction

Hilt is built on top of Dagger and provides a standard way to incorporate dependency injection into Android applications. It eliminates much of the manual component and scope management that raw Dagger requires, while preserving compile-time verification of the dependency graph.

What Hilt Does

  • Generates Dagger components tied to Android lifecycle classes automatically
  • Provides predefined scopes like SingletonComponent, ActivityComponent, and ViewModelComponent
  • Integrates with Jetpack libraries including ViewModel, WorkManager, and Navigation
  • Validates the entire dependency graph at compile time, catching errors before runtime
  • Supports constructor injection, field injection, and method injection patterns

Architecture Overview

Hilt generates a set of Dagger components that mirror the Android class hierarchy. At the top sits SingletonComponent, scoped to the Application lifecycle. Below it, ActivityRetainedComponent survives configuration changes, while ActivityComponent, FragmentComponent, and ViewComponent align with their respective Android classes. The annotation processor reads @Module and @InstallIn declarations at compile time, wiring providers into the correct component. This means no reflection at runtime and minimal startup overhead.

Self-Hosting & Configuration

  • Add the Hilt Gradle plugin and dependencies to your module-level build file
  • Annotate your Application class with @HiltAndroidApp to trigger component generation
  • Mark each Activity or Fragment with @AndroidEntryPoint to enable injection
  • Use @InstallIn to specify which component scope a module belongs to
  • Configure custom entry points with @EntryPoint for classes that Hilt cannot directly inject

Key Features

  • Zero-reflection DI with full compile-time graph validation
  • Predefined Android-aware scopes that follow lifecycle best practices
  • First-class ViewModel injection via @HiltViewModel annotation
  • Testing support with @HiltAndroidTest and @UninstallModules for swapping dependencies
  • Incremental annotation processing for faster builds on large projects

Comparison with Similar Tools

  • Dagger (raw) — Hilt wraps Dagger, removing manual component setup; use raw Dagger only for non-Android JVM projects
  • Koin — Runtime service locator; simpler setup but no compile-time safety and slower on large graphs
  • Kodein — Kotlin-first container; flexible but lacks Android lifecycle integration
  • Manual DI — No library overhead, but scales poorly and duplicates boilerplate across features
  • Spring (server-side) — Reflection-based; powerful for backends, unsuitable for Android constraints

FAQ

Q: Can I use Hilt with Jetpack Compose? A: Yes. Use hiltViewModel() inside composables to obtain injected ViewModels scoped to the navigation graph.

Q: Does Hilt work with multi-module projects? A: Yes. Each Gradle module can declare its own @Module classes; Hilt merges them into the generated component hierarchy.

Q: What is the performance overhead? A: Hilt generates plain Dagger code at compile time. There is no runtime reflection, so the injection overhead at startup is negligible.

Q: How do I migrate from raw Dagger to Hilt? A: Replace manual component builders with Hilt annotations incrementally. Hilt and raw Dagger can coexist in the same project during migration.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires