0Pricing
Jetpack Compose Academy · Lektion

Module, @Provides und @Binds

Legen Sie für Hilt fest, wie Ihre Typen erstellt werden.

Module, @Provides und @Binds ist eine kostenlose Jetpack Compose Academy-Lektion auf CoddyKit. Dies ist Lektion 2 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.

When Constructor Injection Is Not Enough

You cannot add @Inject to a constructor you do not own, like a Retrofit or OkHttp instance. Hilt needs another way to learn how to build those.

Meet the Module

A module is a class that teaches Hilt how to create types it cannot construct on its own. Mark it with @Module to register it.

@Module
object NetworkModule { }

Install Into a Component

Every module declares where it lives with @InstallIn. SingletonComponent makes its bindings available app-wide for the whole process.

@Module
@InstallIn(SingletonComponent::class)
object NetworkModule

Provide a Type With @Provides

@Provides marks a function whose return value Hilt will use as a dependency. The function body shows exactly how to build it.

@Provides
fun provideApi(): ApiService = Retrofit.Builder().build().create(ApiService::class.java)

Provides Can Take Parameters

A @Provides function can ask for other dependencies as parameters. Hilt supplies them first, then runs your function to assemble the result.

@Provides
fun provideRepo(api: ApiService): Repo = Repo(api)

Binding Interfaces With @Binds

When you just need to map an interface to an implementation, @Binds is leaner than @Provides. It tells Hilt which class satisfies the interface.

@Binds
abstract fun bindRepo(impl: RepoImpl): Repo

@Binds Needs an Abstract Module

Because @Binds methods are abstract, their module must be an abstract class, not an object. The implementation must already be injectable.

@Module
@InstallIn(SingletonComponent::class)
abstract class RepoModule

Provides vs Binds

Use @Binds for a simple interface-to-class mapping, and @Provides when you must run code to build the object. Binds generates less code.

Mark Singletons

Add @Singleton to a provider so Hilt creates the object once and reuses it. Perfect for heavy clients like a shared OkHttp instance.

@Provides
@Singleton
fun provideClient(): OkHttpClient = OkHttpClient()

Disambiguate With Qualifiers

If two providers return the same type, a @Qualifier annotation tells them apart. Hilt then knows which binding each injection point wants.

@Qualifier
annotation class AuthClient

Modules Build the Graph

Together your modules form the dependency graph. Hilt reads them at compile time and wires every type into a connected, verified tree.

Quick Check

You need to bind a Repo interface to its single RepoImpl class. Which is the leanest choice?

Recap: Teaching Hilt to Build

You saw that modules teach Hilt how to create types it cannot construct, using @Provides for code and @Binds for interface mappings. 🧩

Häufig gestellte Fragen

Ist die Lektion „Module, @Provides und @Binds“ kostenlos?

Ja — der vollständige Text von „Module, @Provides und @Binds“ 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 „Module, @Provides und @Binds“?

Legen Sie für Hilt fest, wie Ihre Typen erstellt werden. 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 2 von 4.

Wie lange dauert die Lektion „Module, @Provides und @Binds“?

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