为何使用 DI:可测试、低耦合的代码
了解 Hilt 解决的问题。
为何使用 DI:可测试、低耦合的代码 是 CoddyKit 上的免费 Jetpack Compose Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Jetpack Compose Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Jetpack Compose Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What Is Dependency Injection?
Dependency injection means a class receives the objects it needs from the outside, instead of building them itself. Less wiring, more focus. 🔌
The Problem: new Everywhere
When a class calls new on its own dependencies, it gets locked to those exact types. Changing one thing forces you to edit many files.
class Repo {
private val api = ApiService() // hard-wired
}Inject Instead of Construct
Pass dependencies through the constructor and let something else create them. Now your class only states what it needs, not how to build it.
class Repo(private val api: ApiService) {
// api is given to us
}Decoupling Made Real
With injection your code depends on an interface, not a concrete class. You can swap the real one for another without touching the consumer.
interface ApiService { fun load(): String }
class Repo(private val api: ApiService)Testability Is the Big Win
In tests you simply pass a fake dependency. No network, no database, no surprises, just fast and predictable checks. ✅
val fake = object : ApiService { override fun load() = "hi" }
val repo = Repo(fake)Manual DI Gets Tedious
You can inject by hand, but wiring deep graphs means writing lots of boilerplate. Every new dependency ripples up through the constructors.
val api = ApiService()
val repo = Repo(api)
val vm = MyViewModel(repo) // by handEnter Hilt
Hilt is Android's recommended DI library. It generates the wiring for you at compile time, so you describe needs and Hilt builds the graph.
Hilt Sits on Top of Dagger
Hilt is built on Dagger but adds Android-aware defaults. You get compile-time safety with far less setup than raw Dagger.
Mark Your Application
Hilt starts at the app root. Annotate your Application class with @HiltAndroidApp to generate the top-level dependency container.
@HiltAndroidApp
class MyApp : Application()Inject With @Inject
Add @Inject to a constructor and Hilt learns how to create that type. From then on it can supply it anywhere it is needed.
class Repo @Inject constructor(private val api: ApiService)Compile-Time Safety
If a dependency is missing, Hilt fails at compile time, not at runtime. You catch wiring mistakes before the app ever ships. 🛡️
Quick Check
Which benefit best captures why we use dependency injection?
Recap: Why DI Matters
You learned that DI hands a class its dependencies, decoupling code and making tests trivial. Hilt automates that wiring at compile time. 🎉
常见问题解答
「为何使用 DI:可测试、低耦合的代码」课时是免费的吗?
是的 — 「为何使用 DI:可测试、低耦合的代码」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Jetpack Compose Academy 课程的其余内容,请升级到 CoddyKit PRO。 Jetpack Compose Academy 课程共包含 4 节课。
「为何使用 DI:可测试、低耦合的代码」这节课中我会学到什么?
了解 Hilt 解决的问题。 你通过在浏览器中直接运行的动手代码来练习 Jetpack Compose Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Jetpack Compose Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Jetpack Compose Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「为何使用 DI:可测试、低耦合的代码」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Jetpack Compose Academy 课中编写并运行代码吗?
能。每节 Jetpack Compose Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 为何使用 DI:可测试、低耦合的代码
- 模块、@Provides 与 @Binds
- hiltViewModel 与 @HiltViewModel
- 作用域与组件生命周期