Why DI: Testable, Decoupled Code
See the problems Hilt solves.
Why DI: Testable, Decoupled Code is a free Jetpack Compose Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Jetpack Compose Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. 🎉
Frequently asked questions
Is the “Why DI: Testable, Decoupled Code” lesson free?
Yes — the full text of “Why DI: Testable, Decoupled Code” is free to read here on the web, and the Jetpack Compose Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Jetpack Compose Academy course, upgrade to CoddyKit PRO.
What will I learn in “Why DI: Testable, Decoupled Code”?
See the problems Hilt solves. You practise Jetpack Compose Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Jetpack Compose Academy?
No prior experience is required. Jetpack Compose Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Why DI: Testable, Decoupled Code” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Jetpack Compose Academy lesson?
Yes. Every Jetpack Compose Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why DI: Testable, Decoupled Code
- Modules, @Provides & @Binds
- hiltViewModel & @HiltViewModel
- Scopes & Component Lifetimes