Get the Platform Name per Target
A classic expect/actual function for Android and iOS.
Get the Platform Name per Target is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 2 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.
The Hello World of KMP
Returning the platform name is the classic first expect/actual you write. It proves your shared module really runs on both apps. 👋
Declare the expect
Start in commonMain with an expect function that returns a String. No body yet, just the shape both platforms agree on.
expect fun platform(): StringThe Android actual
In androidMain, provide the actual using Android's Build class to report the Android version at runtime.
actual fun platform(): String =
"Android " + Build.VERSION.SDK_INTThe iOS actual
In iosMain, the actual reaches into Apple's UIKit to read the device's system name and version.
actual fun platform(): String =
UIDevice.currentDevice.systemNameSignatures Must Match
Both actuals share the exact name, parameters, and return type of the expect. One mismatch and the compiler refuses to build.
Call It from Shared Code
Now any common function can call platform() freely. It reads like normal Kotlin while the right actual runs underneath.
fun greeting() = "Hi from " + platform()Android Sees Its Value
When the Android app calls greeting(), the compiler linked the androidMain body, so it shows the Android string.
iOS Sees Its Value
The very same call from Swift returns the iOS version instead, because the iosMain actual was compiled into that framework.
Platform Imports Stay Local
Notice that Build and UIDevice are imported only in their own source sets. Shared code never imports a platform type directly.
A Tiny Surface Area
Keep the expect small and focused. One function that returns a name is enough to bridge into each platform cleanly. ✨
Test It Early
Run both apps and print the result. Seeing two different strings confirms your expect/actual wiring works end to end.
Quick Check
Think about how the right value gets chosen.
Recap
You built the classic platform() function: one expect in commonMain, an actual in androidMain and iosMain, and matching signatures across all three. 🎉
Frequently asked questions
Is the “Get the Platform Name per Target” lesson free?
Yes — the full text of “Get the Platform Name per Target” 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 “Get the Platform Name per Target”?
A classic expect/actual function for Android and iOS. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Get the Platform Name per Target” 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
- Why expect/actual Exists
- Get the Platform Name per Target
- expect/actual Classes & Properties
- Common Mistakes & Compiler Errors