0Pricing
Jetpack Compose Academy · 课时

模块、@Provides 与 @Binds

告诉 Hilt 如何构建你的类型。

模块、@Provides 与 @Binds 是 CoddyKit 上的免费 Jetpack Compose Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Jetpack Compose Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Jetpack Compose Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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

常见问题解答

「模块、@Provides 与 @Binds」课时是免费的吗?

是的 — 「模块、@Provides 与 @Binds」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Jetpack Compose Academy 课程的其余内容,请升级到 CoddyKit PRO。 Jetpack Compose Academy 课程共包含 4 节课。

「模块、@Provides 与 @Binds」这节课中我会学到什么?

告诉 Hilt 如何构建你的类型。 你通过在浏览器中直接运行的动手代码来练习 Jetpack Compose Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Jetpack Compose Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Jetpack Compose Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「模块、@Provides 与 @Binds」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Jetpack Compose Academy 课中编写并运行代码吗?

能。每节 Jetpack Compose Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 为何使用 DI:可测试、低耦合的代码
  2. 模块、@Provides 与 @Binds
  3. hiltViewModel 与 @HiltViewModel
  4. 作用域与组件生命周期
← 返回 Jetpack Compose Academy