0Pricing
Android Academy · Lesson

Why Dependency Injection?

Make code testable and modular.

Why Dependency Injection? is a free Android 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 Android 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 (DI) means giving an object the things it needs from the outside, rather than letting it create them itself.

The Problem: Hard Dependencies

When a class builds its own dependencies, it becomes tightly coupled and hard to change or test.

class UserViewModel {
    private val repo = UserRepository(
        ApiClient(), AppDatabase.get()
    ) // tightly coupled
}

The Solution: Inject Instead

Pass dependencies through the constructor. The class no longer cares how they are built.

class UserViewModel(
    private val repo: UserRepository
) {
    // repo supplied from outside
}

Benefit 1: Testability

In tests you can pass a fake or mock repository, so you test the ViewModel without a real network or database.

val vm = UserViewModel(FakeUserRepository())

Benefit 2: Modularity

Because classes depend on abstractions, you can swap implementations — e.g. a real API vs an offline stub — without touching the consumer.

Depend on Interfaces

Have classes depend on an interface, not a concrete type. This is the key to easy substitution.

interface UserRepository {
    suspend fun getUsers(): List<User>
}

class NetworkUserRepository(
    private val api: ApiService
) : UserRepository { /* ... */ }

Benefit 3: Single Responsibility

Classes focus on their job, not on constructing their collaborators. Object creation moves to a dedicated place.

Manual DI Gets Painful

You can do DI by hand, but as the graph grows you end up wiring many objects together manually, threading them through constructors. This is tedious and error-prone.

val db = AppDatabase.get(context)
val api = ApiService.create()
val repo = NetworkUserRepository(api)
val vm = UserViewModel(repo)
// ...repeated everywhere

Enter a DI Framework

A framework automates this wiring. You declare how to build each dependency once, and it constructs the whole graph for you.

Why Hilt

Hilt is the recommended DI framework for Android. Built on Dagger, it provides standard components scoped to Android classes (Application, Activity, ViewModel) with far less boilerplate.

What Hilt Gives You

  • Automatic dependency graph generation.
  • Lifecycle-aware scopes.
  • Easy injection into Activities, Fragments, and ViewModels.

The next lessons set it up.

Quick Check

Test your understanding of dependency injection.

Recap

You learned:

  • DI supplies dependencies from outside instead of creating them internally.
  • It improves testability, modularity, and separation of concerns.
  • Depend on interfaces.
  • Manual DI scales poorly; Hilt automates it.

Frequently asked questions

Is the “Why Dependency Injection?” lesson free?

Yes — the full text of “Why Dependency Injection?” is free to read here on the web, and the Android 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 Android Academy course, upgrade to CoddyKit PRO.

What will I learn in “Why Dependency Injection?”?

Make code testable and modular. You practise Android 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 Android Academy?

No prior experience is required. Android 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 Dependency Injection?” 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 Android Academy lesson?

Yes. Every Android 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

  1. Why Dependency Injection?
  2. Setting Up Hilt
  3. Modules and Providers
  4. Injecting into ViewModels
← Back to Android Academy