0Pricing
Kotlin Multiplatform Academy · Lektion

Koin auf Android und iOS starten

Den Abhängigkeitsgraphen vom jeweiligen Plattform-Einstiegspunkt aus initialisieren

Koin auf Android und iOS starten ist eine kostenlose Kotlin Multiplatform Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Kotlin Multiplatform Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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

Häufig gestellte Fragen

Ist die Lektion „Koin auf Android und iOS starten“ kostenlos?

Ja — der vollständige Text von „Koin auf Android und iOS starten“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Kotlin Multiplatform Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Koin auf Android und iOS starten“?

Den Abhängigkeitsgraphen vom jeweiligen Plattform-Einstiegspunkt aus initialisieren Du übst Kotlin Multiplatform Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Kotlin Multiplatform Academy zu starten?

Keine Vorkenntnisse erforderlich. Kotlin Multiplatform Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „Koin auf Android und iOS starten“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Kotlin Multiplatform Academy-Lektion Code schreiben und ausführen?

Ja. Jede Kotlin Multiplatform Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Ein gemeinsames Koin-Modul deklarieren
  2. single- vs. factory-Scopes
  3. Koin auf Android und iOS starten
  4. In ViewModels und Repos injizieren
← Zurück zu Kotlin Multiplatform Academy