Scripts2026年9月12日·1 分钟阅读

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.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Hilt DI for Android
直接安装命令
npx -y tokrepo@latest install 9177116c-ae4a-11f1-9bc6-00163e2b0d79 --target codex

先 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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产