共有Koin Moduleを宣言する
commonMainで依存関係を登録します
「共有Koin Moduleを宣言する」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Why Wire Things Together
Your app is full of objects that need each other: a repository needs a client, a use case needs the repository. Dependency injection hands those pieces over for you.
Meet Koin
Koin is a lightweight DI library written in pure Kotlin, so it runs happily in commonMain and works on both Android and iOS. 🎉
Add the Dependency
You pull Koin into your shared module the same way you add any other library, listing the core artifact in your version catalog or build file.
implementation("io.insert-koin:koin-core:3.5.0")A Module Is a Recipe Book
A Koin module is just a list of recipes telling Koin how to build each dependency. You declare it once and reuse it everywhere.
Write Your First Module
The module { } builder collects your definitions. An empty one is valid; you fill it with recipes next.
val appModule = module {
}Register with single
Inside the module, single { } registers one shared instance Koin reuses for the whole app, perfect for a repository.
val appModule = module {
single { UserRepository() }
}Wire Dependencies with get
When one object needs another, call get() inside the recipe. Koin looks up the matching definition and passes it in automatically.
single { UserRepository(api = get()) }Order Does Not Matter
You can declare definitions in any order. Koin resolves the graph lazily, so a recipe can safely get() something defined further down.
Keep It in commonMain
Because Koin is multiplatform, place appModule in commonMain. Both your Android and iOS apps then share the exact same wiring.
Group Related Modules
Large apps split definitions into several modules, like a data module and a domain module, then combine them later when starting Koin.
val dataModule = module { single { ApiClient() } }A Function That Returns It
A common pattern exposes your module from a function, making it easy for each platform to grab the shared definition.
fun appModule() = module {
single { UserRepository(get()) }
}Quick Check
Let's lock in what a Koin module does.
Recap
You met Koin, added it to your shared module, and wrote a module that registers dependencies with single and links them using get. One recipe book, both platforms. 🚀
よくある質問
「共有Koin Moduleを宣言する」レッスンは無料ですか?
はい。「共有Koin Moduleを宣言する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「共有Koin Moduleを宣言する」で何を学びますか?
commonMainで依存関係を登録します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「共有Koin Moduleを宣言する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 共有Koin Moduleを宣言する
- singleとfactoryのScopes
- AndroidとiOSでKoinを起動する
- ViewModelsとReposにInjectする