AndroidとiOSでKoinを起動する
各プラットフォームのエントリーポイントから依存関係グラフを初期化します
「AndroidとiOSでKoinを起動する」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
A Module Needs Starting
Declaring modules is only half the job. Each platform must start Koin at launch so the container is ready before anything asks for a dependency.
A Shared Init Helper
Put the common startup in shared code with a small initKoin function. Both platforms call it, so the wiring never drifts apart.
fun initKoin() = startKoin {
modules(appModule())
}Pass Extra Platform Config
Give initKoin a parameter so each platform can append its own setup, like Android needing the application context.
fun initKoin(extra: KoinAppDeclaration = {}) =
startKoin { extra(); modules(appModule()) }Start Koin on Android
On Android, call initKoin from your Application class so the graph exists for the whole process lifetime.
class App : Application() {
override fun onCreate() {
super.onCreate()
initKoin()
}
}Give Android the Context
Android modules often need a Context. Pass it through the extra block using androidContext so definitions can resolve it.
initKoin { androidContext(this@App) }Register the Application Class
Remember to point your manifest at the custom Application class, or onCreate never runs and Koin never starts.
Start Koin on iOS
On iOS you call the same initKoin through the generated framework, typically from your SwiftUI App init or AppDelegate.
Call It from Swift
Koin exposes a tiny helper so Swift can launch the graph in one clean line at app startup. 🍎
// Swift
KoinKt.doInitKoin()Start Only Once
Call startKoin a single time per app launch. Starting it twice throws an error, so keep init in one obvious entry point.
Add Logging While Learning
Turn on printLogger during development so Koin prints what it builds, making missing-definition mistakes easy to spot.
startKoin {
printLogger()
modules(appModule())
}One Graph, Two Entry Points
The key idea: the same modules load from two platform entry points. Android uses its Application, iOS uses its App init, both share the wiring.
Quick Check
Where should startup logic live?
Recap
You exposed a shared initKoin, called it from Android's Application and iOS's App init, and learned to start it once with optional logging. The graph is live on both. ✅
AI チューターと学ぶ Kotlin — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 30
- レッスン
- 120
よくある質問
「AndroidとiOSでKoinを起動する」レッスンは無料ですか?
はい。「AndroidとiOSでKoinを起動する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「AndroidとiOSでKoinを起動する」で何を学びますか?
各プラットフォームのエントリーポイントから依存関係グラフを初期化します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「AndroidとiOSでKoinを起動する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 共有Koin Moduleを宣言する
- singleとfactoryのScopes
- AndroidとiOSでKoinを起動する
- ViewModelsとReposにInjectする