0Pricing
Kotlin Multiplatform Academy · Lesson

expect/actual for Device Info

Return OS version and model per platform.

expect/actual for Device Info 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.

From Interface to expect

Sometimes you skip an interface and use expect/actual directly. It declares device info once in commonMain and implements it per platform.

Declare the expect Class

In commonMain, write an expect class with the device facts you need. There is no body yet, only the shape both platforms must satisfy.

expect class DeviceInfo() {
  val model: String
  val osVersion: String
}

The Android actual

In androidMain, the actual reads Build.MODEL and the OS release to fill in real Android values at runtime.

actual class DeviceInfo {
  actual val model = Build.MODEL
  actual val osVersion = Build.VERSION.RELEASE
}

The iOS actual

In iosMain, the actual uses UIDevice to report Apple's model name and the running iOS version.

actual class DeviceInfo {
  actual val model = UIDevice.currentDevice.model
  actual val osVersion = UIDevice.currentDevice.systemVersion
}

Signatures Must Line Up

Every property on each actual must match the expect by name and type. A single mismatch and the compiler refuses to link the targets.

Use It From Shared Code

Common code creates DeviceInfo() and reads its properties like normal Kotlin. The correct actual is wired in at build time per target.

fun banner() = "Running on " + DeviceInfo().model

Android Gets Build Values

When the Android app builds, the androidMain actual is linked, so model returns the real hardware name from the Build class.

iOS Gets UIDevice Values

The identical call inside the iOS framework returns Apple's values, because the iosMain actual was compiled into that target instead.

Imports Stay In Source Sets

Build lives in androidMain and UIDevice in iosMain. CommonMain never imports a platform type, which is exactly what keeps it shared.

expect/actual vs Interface

An interface needs you to inject an implementation. expect/actual links automatically at compile time, so callers just construct the class.

Pick the Simpler Tool

For a single, stateless platform fact, expect/actual is the leaner choice. Reach for an interface when you also want to swap fakes freely.

Quick Check

Consider how the right device values reach each app.

Recap

You declared an expect DeviceInfo in commonMain and gave Android and iOS actuals, with matching signatures so each app reports its own values. 🎉

Frequently asked questions

Is the “expect/actual for Device Info” lesson free?

Yes — the full text of “expect/actual for Device Info” 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 “expect/actual for Device Info”?

Return OS version and model per platform. 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 “expect/actual for Device Info” 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

  1. Design a Capability Interface
  2. expect/actual for Device Info
  3. File System & Paths per Platform
  4. Inject Platform Implementations
← Back to Kotlin Multiplatform Academy