0Pricing
Jetpack Compose Academy · Lesson

Modules, @Provides & @Binds

Tell Hilt how to build your types.

Modules, @Provides & @Binds is a free Jetpack Compose Academy lesson on CoddyKit — lesson 2 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 Jetpack Compose Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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. 🧩

Frequently asked questions

Is the “Modules, @Provides & @Binds” lesson free?

Yes — the full text of “Modules, @Provides & @Binds” is free to read here on the web, and the Jetpack Compose 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 Jetpack Compose Academy course, upgrade to CoddyKit PRO.

What will I learn in “Modules, @Provides & @Binds”?

Tell Hilt how to build your types. You practise Jetpack Compose 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 Jetpack Compose Academy?

No prior experience is required. Jetpack Compose Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Modules, @Provides & @Binds” 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 Jetpack Compose Academy lesson?

Yes. Every Jetpack Compose 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 DI: Testable, Decoupled Code
  2. Modules, @Provides & @Binds
  3. hiltViewModel & @HiltViewModel
  4. Scopes & Component Lifetimes
← Back to Jetpack Compose Academy