モジュール、@Provides、@Binds
型の構築方法をHiltに伝えます。
「モジュール、@Provides、@Binds」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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 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. 🧩
よくある質問
「モジュール、@Provides、@Binds」レッスンは無料ですか?
はい。「モジュール、@Provides、@Binds」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。
「モジュール、@Provides、@Binds」で何を学びますか?
型の構築方法をHiltに伝えます。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Jetpack Compose Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「モジュール、@Provides、@Binds」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このJetpack Compose Academyレッスンでコードを書いて実行できますか?
はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- DIの理由:テスト可能で疎結合なコード
- モジュール、@Provides、@Binds
- hiltViewModelと@HiltViewModel
- スコープとコンポーネントのライフタイム