Moduły, @Provides i @Binds
Określ dla Hilt, jak tworzyć typy
Moduły, @Provides i @Binds to bezpłatna lekcja Jetpack Compose Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Jetpack Compose Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Jetpack Compose Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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 NetworkModuleProvide 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 RepoModuleProvides 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 AuthClientModules 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. 🧩
Często zadawane pytania
Czy lekcja „Moduły, @Provides i @Binds” jest bezpłatna?
Tak — pełny tekst „Moduły, @Provides i @Binds” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Jetpack Compose Academy, przejdź na CoddyKit PRO. Kurs Jetpack Compose Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Moduły, @Provides i @Binds”?
Określ dla Hilt, jak tworzyć typy Ćwiczysz Jetpack Compose Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Jetpack Compose Academy?
Nie wymagamy żadnego doświadczenia. Jetpack Compose Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.
Ile czasu zajmuje lekcja „Moduły, @Provides i @Binds”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Jetpack Compose Academy?
Tak. Każda lekcja Jetpack Compose Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Dlaczego DI: testowalny, luźno powiązany kod
- Moduły, @Provides i @Binds
- hiltViewModel i @HiltViewModel
- Zakresy i cykle życia komponentów