Kotlin Multiplatform Academy · レッスン

デバイス情報のexpect/actual

プラットフォームごとにOSバージョンとモデルを返します

レッスン 2/413 ステップ

「デバイス情報のexpect/actual」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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. 🎉

無料で開始

AI チューターと学ぶ Kotlin — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
30
レッスン
120

よくある質問

「デバイス情報のexpect/actual」レッスンは無料ですか?

はい。「デバイス情報のexpect/actual」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。

「デバイス情報のexpect/actual」で何を学びますか?

プラットフォームごとにOSバージョンとモデルを返します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Kotlin Multiplatform Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「デバイス情報のexpect/actual」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?

はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 機能インターフェースを設計する
  2. デバイス情報のexpect/actual
  3. プラットフォームごとのファイルシステムとパス
  4. プラットフォーム実装を注入する
← Kotlin Multiplatform Academyに戻る