0Pricing
Android Academy · Lesson

Modules and Providers

Tell Hilt how to create dependencies.

Modules and Providers is a free Android Academy lesson on CoddyKit — lesson 3 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 Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Telling Hilt How to Build Things

Hilt can construct a class automatically if its constructor is annotated @Inject. But for interfaces or types you do not own, you must teach Hilt with a Module.

Constructor Injection

The simplest case: annotate the constructor and Hilt knows how to build it.

class Analytics @Inject constructor(
    private val api: ApiService
)

When Constructor Injection Is Not Enough

You cannot annotate the constructor of:

  • An interface (no constructor).
  • A class from a library (e.g. Retrofit, Room).

These need a module.

Defining a Module

A @Module tells Hilt how to provide types. @InstallIn declares which component (scope) it belongs to.

import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent

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

@Provides for Library Types

Use @Provides on a function that builds and returns the dependency. Hilt calls it when that type is needed.

@Provides
@Singleton
fun provideRetrofit(): Retrofit =
    Retrofit.Builder()
        .baseUrl("https://api.example.com/")
        .build()

Providers Can Depend on Each Other

A @Provides function can take parameters; Hilt supplies them from the graph.

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

@Binds for Interfaces

To bind an interface to an implementation, use @Binds in an abstract module. It is more efficient than @Provides for simple binding.

@Module
@InstallIn(SingletonComponent::class)
abstract class RepoModule {
    @Binds
    abstract fun bindRepo(
        impl: NetworkUserRepository
    ): UserRepository
}

@Provides vs @Binds

  • @Binds — when you just map an interface to an implementation that has constructor injection. Abstract function.
  • @Provides — when you must run code to build the object (e.g. a builder).

Scoping with @Singleton

Annotate a provider with a scope like @Singleton so Hilt reuses one instance for the whole app instead of building a new one each time.

Matching Scope to Component

The scope must match the component: @Singleton with SingletonComponent, @ActivityScoped with ActivityComponent, and so on.

Module Summary

  • @Module + @InstallIn defines where bindings live.
  • @Provides builds library/complex types.
  • @Binds maps interfaces to implementations.

Quick Check

Test your understanding of modules and providers.

Recap

You learned:

  • Constructor injection works when you own the class.
  • @Module + @InstallIn teaches Hilt to provide other types.
  • @Provides builds library/complex objects; @Binds maps interfaces.
  • Match scopes like @Singleton to their component.

Frequently asked questions

Is the “Modules and Providers” lesson free?

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

What will I learn in “Modules and Providers”?

Tell Hilt how to create dependencies. You practise Android 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 Android Academy?

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

How long does the “Modules and Providers” 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 Android Academy lesson?

Yes. Every Android 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 Dependency Injection?
  2. Setting Up Hilt
  3. Modules and Providers
  4. Injecting into ViewModels
← Back to Android Academy