commonMainでGreeting関数を作る
最初のクロスプラットフォームKotlin関数を書きます
「commonMainでGreeting関数を作る」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Meet commonMain
Code you put in commonMain is shared by every platform. It is the heart of KMP, so this is where your first function will live.
Pure Kotlin Only
Inside commonMain you write pure Kotlin with no Android or iOS imports. If it compiles here, it runs the same on both apps.
Pick a Tiny Goal
Your first shared piece is a small greeting function. It takes a name and returns a friendly line that both apps can show. 👋
Write the Function
Declare it with fun and give it one parameter. This single line of Kotlin is now available to Android and iOS alike.
fun greet(name: String): String {
return "Hello, " + name + "!"
}String Templates
Kotlin string templates read cleaner than joining with plus. The dollar sign drops a value straight into the text.
fun greet(name: String): String {
return "Hello, $name!"
}Return Types Matter
The : String after the parentheses promises a String comes back. Both platforms rely on that contract being honored.
Keep It Side-Effect Free
A good shared function is pure: same input, same output, no logging or UI. That is exactly what makes it safe everywhere.
Default Arguments
Kotlin lets you set a default value, so callers can skip the argument. Both apps then get a sensible greeting for free.
fun greet(name: String = "there"): String {
return "Hello, $name!"
}Where the File Lives
Save it under commonMain/kotlin in your shared module. The folder is the signal to KMP that this code targets every platform.
Top-Level Functions
You did not need a class. A top-level function in a file is perfectly fine and keeps tiny shared utilities simple.
One Source of Truth
This single greet function is the one source of truth. Change the wording once and every platform speaks the same way. ✨
Quick Check
Let us check where shared code belongs.
Recap
You wrote your first shared function in commonMain: pure Kotlin, a clear return type, and one source of truth for both apps. 🎉
よくある質問
「commonMainでGreeting関数を作る」レッスンは無料ですか?
はい。「commonMainでGreeting関数を作る」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「commonMainでGreeting関数を作る」で何を学びますか?
最初のクロスプラットフォームKotlin関数を書きます ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「commonMainでGreeting関数を作る」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- commonMainでGreeting関数を作る
- Androidアプリから共有コードを呼び出す
- iOSアプリから共有コードを呼び出す
- 一度変更して両方のアプリで確認する