Plattformimplementierungen injizieren
Stellen Sie das passende actual über Koin oder Factorys bereit.
Plattformimplementierungen injizieren ist eine kostenlose Kotlin Multiplatform Academy-Lektion auf CoddyKit. Dies ist Lektion 4 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.
Who Supplies the Real Thing
An interface defines a capability, but something must hand shared code the real platform implementation. That wiring is dependency injection.
Avoid new in Shared Code
Shared logic should not construct platform classes itself. Instead it receives the implementation from outside, keeping it decoupled and testable.
Inject via Constructor
The simplest approach is a constructor parameter. Pass the capability in, and the class never knows which platform built it.
class Profile(private val info: DeviceInfo)Provide the Android Instance
On Android, the app module creates the real actual and passes it down when it builds the shared object.
val profile = Profile(DeviceInfo())Provide the iOS Instance
On iOS, Swift constructs the framework's DeviceInfo and injects it the same way, so shared code stays identical across platforms.
let profile = Profile(info: DeviceInfo())Scale Up With Koin
For many dependencies, a DI library like Koin declares each capability once in a shared module and resolves it on demand.
val appModule = module {
single<DeviceInfo> { DeviceInfo() }
}Bind Interface to Actual
In Koin you bind the interface type to the platform implementation, so callers ask for DeviceInfo and get the right concrete one.
single<Storage> { PlatformStorage() }Resolve Where You Need It
Anywhere in shared code you can pull a dependency from the graph by its type, no manual construction required.
val info: DeviceInfo by inject()Swap Fakes in Tests
Because everything is injected, a test simply provides a fake implementation. No platform, emulator, or device is involved. ✅
Profile(FakeDeviceInfo())One Wiring Point per App
Keep injection at each app's entry point. That single setup spot decides which actuals run, while shared code stays blissfully unaware.
Factories for Cheap Cases
If a DI library feels heavy, a plain factory function works too. It builds the platform object in one place and hands it to shared code.
fun deviceInfo(): DeviceInfo = DeviceInfo()Quick Check
Think about why shared code receives its dependencies.
Recap
You learned to inject platform implementations via the constructor or Koin, binding interfaces to actuals so shared code stays clean and testable. 🎉
Häufig gestellte Fragen
Ist die Lektion „Plattformimplementierungen injizieren“ kostenlos?
Ja — der vollständige Text von „Plattformimplementierungen injizieren“ 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 „Plattformimplementierungen injizieren“?
Stellen Sie das passende actual über Koin oder Factorys bereit. 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 4 von 4.
Wie lange dauert die Lektion „Plattformimplementierungen injizieren“?
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
- Eine Capability-Schnittstelle entwerfen
- expect/actual für Geräteinformationen
- Dateisystem und Pfade je Plattform
- Plattformimplementierungen injizieren