Inject Platform Implementations
Provide the right actual via Koin or factories.
Inject Platform Implementations is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. 🎉
Frequently asked questions
Is the “Inject Platform Implementations” lesson free?
Yes — the full text of “Inject Platform Implementations” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.
What will I learn in “Inject Platform Implementations”?
Provide the right actual via Koin or factories. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Multiplatform Academy?
No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Inject Platform Implementations” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Multiplatform Academy lesson?
Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Design a Capability Interface
- expect/actual for Device Info
- File System & Paths per Platform
- Inject Platform Implementations