Moduli, @Provides e @Binds
Indichi a Hilt come costruire i propri tipi.
Moduli, @Provides e @Binds è una lezione Jetpack Compose Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Jetpack Compose Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Jetpack Compose Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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. 🧩
Domande Frequenti
La lezione «Moduli, @Provides e @Binds» è gratuita?
Sì — il testo completo di «Moduli, @Provides e @Binds» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Jetpack Compose Academy, passa a CoddyKit PRO. Il corso Jetpack Compose Academy include 4 lezioni in totale.
Cosa imparerò in «Moduli, @Provides e @Binds»?
Indichi a Hilt come costruire i propri tipi. Eserciti Jetpack Compose Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Jetpack Compose Academy?
Non è richiesta alcuna esperienza precedente. Jetpack Compose Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «Moduli, @Provides e @Binds»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Jetpack Compose Academy?
Sì. Ogni lezione Jetpack Compose Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Perché la DI: codice testabile e disaccoppiato
- Moduli, @Provides e @Binds
- hiltViewModel e @HiltViewModel
- Scope e cicli di vita dei componenti