Androidアプリから共有コードを呼び出す
共有関数をJetpackの画面に接続します
「Androidアプリから共有コードを呼び出す」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Android Sees Shared Code
The Android app already depends on your shared module, so the greet function is just another Kotlin call from Android's point of view.
No Special Import
Because it is all Kotlin on the JVM, you simply import the shared package like any local class. There is no bridge or wrapper needed.
import com.example.shared.greetCall It Directly
From an Activity or composable you call greet like a normal function. The shared logic runs right inside the Android process.
val message = greet("Android")Show It On Screen
Feed the returned String into a Text composable. The user sees a greeting that was computed by your shared code.
Text(text = greet("Android"))Inside a Composable
Keep the call simple. The composable only displays the result; the wording itself lives safely in commonMain.
@Composable
fun Hello() {
Text(greet("Android"))
}Gradle Wires It Up
The Android module lists the shared module under implementation in its build file. That dependency is what makes the import resolve.
implementation(project(":shared"))Same JVM, Same Types
Android runs on the JVM, just like the shared module's android target. Strings and numbers map across with zero conversion.
Pass Real Data
You can pass any String, like a user's name from state. The shared function stays the same no matter who calls it.
val msg = greet(userName)Build and Run
Hit Run in Android Studio and pick an emulator. Your greeting appears, proving the shared module is fully connected. 🚀
If the Import Fails
An unresolved import usually means a sync is missing. Click sync in Gradle so Android Studio re-reads the dependency.
Logic Stays Shared
Notice you wrote zero greeting logic here. Android is just a consumer, which is exactly the role KMP wants the UI layer to play.
Quick Check
How does Android reach the shared function?
Recap
You wired the shared module into Android with a dependency, imported greet, and showed its result in Compose. No bridge required. 🎉
よくある質問
「Androidアプリから共有コードを呼び出す」レッスンは無料ですか?
はい。「Androidアプリから共有コードを呼び出す」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「Androidアプリから共有コードを呼び出す」で何を学びますか?
共有関数をJetpackの画面に接続します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「Androidアプリから共有コードを呼び出す」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- commonMainでGreeting関数を作る
- Androidアプリから共有コードを呼び出す
- iOSアプリから共有コードを呼び出す
- 一度変更して両方のアプリで確認する