0Pricing
Jetpack Compose Academy · Lektion

Warum DI: testbarer, entkoppelter Code

Sehen Sie, welche Probleme Hilt löst.

Warum DI: testbarer, entkoppelter Code ist eine kostenlose Jetpack Compose Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Jetpack Compose Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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 hand

Enter 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. 🎉

Häufig gestellte Fragen

Ist die Lektion „Warum DI: testbarer, entkoppelter Code“ kostenlos?

Ja — der vollständige Text von „Warum DI: testbarer, entkoppelter Code“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Jetpack Compose Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Warum DI: testbarer, entkoppelter Code“?

Sehen Sie, welche Probleme Hilt löst. Du übst Jetpack Compose Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Jetpack Compose Academy zu starten?

Keine Vorkenntnisse erforderlich. Jetpack Compose Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.

Wie lange dauert die Lektion „Warum DI: testbarer, entkoppelter Code“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Jetpack Compose Academy-Lektion Code schreiben und ausführen?

Ja. Jede Jetpack Compose Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Warum DI: testbarer, entkoppelter Code
  2. Module, @Provides und @Binds
  3. hiltViewModel und @HiltViewModel
  4. Scopes und Lebenszyklen von Komponenten
← Zurück zu Jetpack Compose Academy