commonMain、androidMain、iosMain
毎日使うことになる3つのソースセットを学びます
「commonMain、androidMain、iosMain」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Source Sets, Not Folders
Inside shared, code lives in source sets. Each one targets a different platform, and Kotlin compiles them accordingly.
Meet commonMain
commonMain is where shared Kotlin lives. Code here compiles for every target, so it must stay platform-neutral.
Meet androidMain
androidMain holds Android-only code in the shared module. Here you can touch JVM and Android APIs freely.
Meet iosMain
iosMain holds iOS-only Kotlin. It can reach Apple frameworks through Kotlin/Native interop.
A Place for Everything
Put cross-platform logic in commonMain. Drop into androidMain or iosMain only when you truly need that platform.
What commonMain Looks Like
Plain Kotlin with no platform imports belongs in commonMain, like this small greeting function.
fun greet(name: String): String {
return "Hello, " + name
}Platform Code in androidMain
An Android API call belongs in androidMain, never in commonMain, because iOS has no such class.
import android.os.Build
fun osName(): String = Build.MODELThey Share One Name
commonMain can declare an API and each platform set fills it in. That handshake is the expect/actual pattern you will meet soon.
Folder Layout
On disk the sets sit under src: src/commonMain, src/androidMain, src/iosMain, each with its own kotlin folder.
Compilation Merges Sets
For Android the compiler merges commonMain with androidMain. For iOS it merges commonMain with iosMain.
Start in Common
A good habit: write in commonMain first, and only move code outward when the compiler tells you a platform API is missing. 🙂
Quick Check
Where should this code go?
Recap
Three homes: commonMain for shared logic, androidMain for Android-only, iosMain for iOS-only. Default to common.
よくある質問
「commonMain、androidMain、iosMain」レッスンは無料ですか?
はい。「commonMain、androidMain、iosMain」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「commonMain、androidMain、iosMain」で何を学びますか?
毎日使うことになる3つのソースセットを学びます ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「commonMain、androidMain、iosMain」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- shared ModuleとApp Modules
- commonMain、androidMain、iosMain
- 共有build.gradle.ktsを読む
- テストはどこにあるか:commonTestと仲間たち