修改一次,在两个应用中查看结果
编辑共享逻辑并观察两个平台同步更新
修改一次,在两个应用中查看结果 是 CoddyKit 上的免费 Kotlin Multiplatform Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Kotlin Multiplatform Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Kotlin Multiplatform Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
The Real Payoff
Now comes the magic of KMP: edit the shared function once and both apps change together. No copy-paste, no drift.
Tweak the Greeting
Open greet in commonMain and change the wording. This is the only place you touch, even though two apps depend on it.
fun greet(name: String): String {
return "Welcome, $name!"
}Rebuild Android
Run the Android app again. Because it links the shared module, it instantly shows the new Welcome text with zero extra edits.
Rebuild iOS
Rebuild the iOS app and the same new line appears. The shared framework was recompiled from the one source you changed.
Add Logic, Not Just Text
Change behavior too, like trimming spaces. Both platforms gain the fix at once, because the logic truly lives in one place.
fun greet(name: String): String {
return "Welcome, " + name.trim() + "!"
}One Bug, One Fix
If greet had a bug, you fix it once and both apps are healed. That is the consistency KMP is built to give you.
Why Builds Are Needed
Each platform compiles the shared code into its own artifact. So after an edit, both apps need a fresh build to pick it up.
Tests Guard the Logic
Write one test for greet in commonTest and it protects every platform. A single safety net covers all your apps.
Less Code to Maintain
Sharing logic shrinks your surface area. Fewer lines, fewer places for bugs, and behavior that can never disagree across apps.
Keep UI Native
The text changed everywhere, but each app still styles it with its own UI. You share the brain and keep platform-native looks. 💡
You Did It End to End
You wrote shared code, called it from both apps, and proved a single edit updates them together. That is the whole KMP loop. 🎉
Quick Check
What happens after you edit shared logic?
Recap
One edit in commonMain flowed to both apps after a rebuild. Share the logic, test it once, and keep each UI native. 🎉
常见问题解答
「修改一次,在两个应用中查看结果」课时是免费的吗?
是的 — 「修改一次,在两个应用中查看结果」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Kotlin Multiplatform Academy 课程的其余内容,请升级到 CoddyKit PRO。 Kotlin Multiplatform Academy 课程共包含 4 节课。
「修改一次,在两个应用中查看结果」这节课中我会学到什么?
编辑共享逻辑并观察两个平台同步更新 你通过在浏览器中直接运行的动手代码来练习 Kotlin Multiplatform Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Kotlin Multiplatform Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Kotlin Multiplatform Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「修改一次,在两个应用中查看结果」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Kotlin Multiplatform Academy 课中编写并运行代码吗?
能。每节 Kotlin Multiplatform Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- commonMain 中的问候函数
- 从 Android 应用调用共享代码
- 从 iOS 应用调用共享代码
- 修改一次,在两个应用中查看结果