Platform APIs
Access native features.
Platform APIs is a free Kotlin 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 Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Accessing Native Features
Shared code sometimes needs device features — storage, settings, the clock, networking engine. KMM offers several ways to reach native APIs cleanly.
expect/actual for Primitives
For small platform values, use expect/actual. Common code stays clean while each platform fills in the detail.
// commonMain
expect fun currentTimeMillis(): Long
// androidMain
actual fun currentTimeMillis(): Long = System.currentTimeMillis()Dependency Injection of Platform Code
A more flexible approach: define an interface in common code and inject a platform implementation from each app.
// commonMain
interface Storage {
fun save(key: String, value: String)
fun load(key: String): String?
}Android Implementation
Android implements the interface with SharedPreferences and provides it to shared code.
// androidMain
import android.content.SharedPreferences
class AndroidStorage(private val prefs: SharedPreferences) : Storage {
override fun save(key: String, value: String) {
prefs.edit().putString(key, value).apply()
}
override fun load(key: String): String? = prefs.getString(key, null)
}iOS Implementation
iOS implements the same interface with NSUserDefaults from the Foundation framework.
// iosMain
import platform.Foundation.NSUserDefaults
class IosStorage : Storage {
private val defaults = NSUserDefaults.standardUserDefaults
override fun save(key: String, value: String) = defaults.setObject(value, key)
override fun load(key: String): String? = defaults.stringForKey(key)
}Calling Native iOS APIs
From iosMain you can call Apple frameworks directly because Kotlin/Native generates interop bindings for them.
import platform.UIKit.UIDevice
fun deviceModel(): String = UIDevice.currentDevice.modelCalling Android APIs
androidMain has the full Android SDK available, since it compiles against the Android platform.
import android.os.Build
fun deviceModel(): String = Build.MODELReady-Made Multiplatform Libraries
Often you don't need custom interop. Libraries already wrap common features:
- multiplatform-settings — key-value storage
- Ktor — networking
- SQLDelight — local database
Providing Implementations
Inject platform implementations at app startup, typically via a DI framework like Koin, so shared code receives the right one.
// Android: provide AndroidStorage(prefs)
// iOS: provide IosStorage()
// Shared Repository(storage) uses whichever was injectedChoosing an Approach
Guidelines:
- Tiny value →
expect/actual - Behavior with multiple methods → interface + injection
- Common need → use an existing multiplatform library
A Runnable Abstraction Demo
The interface idea is plain Kotlin. Here an in-memory implementation stands in for a platform store.
interface Storage {
fun save(key: String, value: String)
fun load(key: String): String?
}
class MemoryStorage : Storage {
private val map = mutableMapOf<String, String>()
override fun save(key: String, value: String) { map[key] = value }
override fun load(key: String): String? = map[key]
}
fun main() {
val s: Storage = MemoryStorage()
s.save("name", "Ada")
println(s.load("name"))
}Quick Check
You need a storage feature with several methods, implemented differently on each platform and easily testable. What is the recommended approach?
Recap
You can reach native features cleanly:
expect/actualfor primitives- Common interface + injected platform implementation for behavior
- Direct interop in
androidMain/iosMain - Reuse multiplatform libraries when possible
That completes the KMM Basics course.
Frequently asked questions
Is the “Platform APIs” lesson free?
Yes — the full text of “Platform APIs” is free to read here on the web, and the Kotlin 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 Academy course, upgrade to CoddyKit PRO.
What will I learn in “Platform APIs”?
Access native features. You practise Kotlin 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 Academy?
No prior experience is required. Kotlin 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 “Platform APIs” 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 Academy lesson?
Yes. Every Kotlin 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.