ရိုးရိုးလေး ရှင်းပြမယ်
Kotlin Multiplatform ကို ရိုးရိုးလေး ပြောရရင်
Kotlin Multiplatform (အတိုကောက် KMP) က codebase တစ်ခုတည်းကနေ platform အများအပြားအတွက် code ထုတ်ပေးပါတယ်: server အတွက် JVM library၊ Android module၊ iOS framework၊ JavaScript bundle တို့ပေါ့။ Validation, စျေးနှုန်း စည်းမျဉ်း, API model နဲ့ view-model state လို နေရာတိုင်းမှာ တူညီတဲ့ logic ကို shared module တစ်ခုထဲမှာ တစ်ကြိမ်ပဲ ရေးပြီး platform အလိုက်ကွဲတဲ့ အပိုင်းတွေကိုပဲ သီးခြားရေးပါတယ်။ ကုမ္ပဏီတစ်ခုက ကုန်ပစ္စည်းလက်စွဲကို ကြားနေဘာသာစကားနဲ့ တစ်စောင်ရေးပြီး နိုင်ငံတစ်ခုစီအတွက် နောက်ဆက်တွဲ တိုတိုလေးတွေ ထားသလိုပါပဲ: အူတိုင်ကို မျှဝေပြီး ဒေသတွင်း စည်းမျဉ်းတွေက နောက်ဆက်တွဲထဲမှာ ရှိပါတယ်။
KMP က logic ကို မျှဝေတာပါ၊ user interface ကို မဖြစ်မနေ မျှဝေတာ မဟုတ်ပါဘူး။ Platform တစ်ခုစီက native UI (Android မှာ Jetpack Compose၊ iOS မှာ SwiftUI) ကို ဆက်သုံးလို့ရသလို UI code ကိုပါ မျှဝေချင်ရင် Compose Multiplatform ကို ထပ်ထည့်လို့ရပါတယ်။ ဘယ်လောက်မျှဝေမလဲ ကိုယ်ဆုံးဖြတ်နိုင်ပြီး module သေးသေးလေးတစ်ခုနဲ့ စလို့ရပါတယ်။
Problem → Why → Solution
ပြဿနာ: စည်းမျဉ်းတူတူကို နှစ်ခါရေးရတာ
Team တစ်ခုက app တူတူကို Android နဲ့ iOS မှာ ထုတ်ပေးပါတယ်။ စျေးနှုန်းတွက်ချက်မှု၊ input validation နဲ့ API parsing တို့က Kotlin နဲ့ တစ်ခါ၊ Swift နဲ့ တစ်ခါ နှစ်ကြိမ်ရှိပါတယ်။ ကော်ပီတစ်ခုမှာ ပြင်ထားတဲ့ bug က တခြားတစ်ခုမှာ ရက်သတ္တပတ်ပေါင်းများစွာ ဆက်ရှိနေပြီး ကော်ပီနှစ်ခုက တဖြည်းဖြည်း ကွာလာပါတယ်: Android app က စုစုပေါင်းကို တစ်နည်း ဝိုင်း၊ iOS app က တစ်နည်း ဝိုင်းတဲ့အတွက် ဖုန်းနှစ်လုံးက စျေးမတူတာကို support က ရှင်းပြနေရပါတယ်။
ရှေးရိုးနည်းနဲ့ code မျှဝေတာလည်း ကိုယ်ပိုင်ကုန်ကျစရိတ် ရှိပါတယ်: JavaScript engine ဒါမှမဟုတ် platform တစ်ခုစီအတွက် သီးခြားစီ wrap လုပ်ထားတဲ့ C++ core က developer တိုင်း သင်ယူပြီး debug လုပ်ရမယ့် bridge တစ်ခု ထပ်ထည့်ပါတယ်။ အောက်က code နှစ်ခုက စည်းမျဉ်းတူတူကို နှစ်ခါ ရေးထားတာပါ။
// iOS app (Swift): the same rule, written a second time
func totalWithTax(cents: Int64, taxPercent: Int) -> Int64 {
return cents + (cents * Int64(taxPercent) + 50) / 100 // rounds
}// Android app (Kotlin)
fun totalWithTax(cents: Long, taxPercent: Int): Long =
cents + cents * taxPercent / 100 // truncatesစည်းမျဉ်းတစ်ခု၊ နေရာတစ်ခု
Logic ထပ်နေတဲ့ ကုန်ကျစရိတ်က နှစ်ခါရေးရတာ မဟုတ်ပါဘူး၊ ကော်ပီနှစ်ခုကို အမြဲမှန်နေအောင် ထိန်းရတာပါ။ စည်းမျဉ်းက shared module တစ်ခုထဲမှာ ရှိရင် fix တစ်ခု၊ test တစ်ခု၊ review တစ်ခုက platform အားလုံးကို တစ်ပြိုင်နက် ဖုံးလွှမ်းပြီး platform app တွေက တကယ်ကွဲတဲ့ အရာတွေဖြစ်တဲ့ screen, device API နဲ့ permission တွေအထိပဲ ကျန်ပါတော့တယ်။
Core concept: source set, expect/actual နဲ့ interface
Kotlin Multiplatform module တစ်ခုကို source set တွေအဖြစ် စီစဉ်ပါတယ်။ commonMain ထဲက code က target အားလုံးအတွက် compile ဖြစ်တာမို့ common Kotlin standard library နဲ့ kotlinx.coroutines လို multiplatform ကို ထောက်ပံ့တဲ့ library တွေကိုပဲ သုံးလို့ရပါတယ်။ jvmMain, androidMain နဲ့ iosMain လို platform source set တွေက target တစ်ခု (ဒါမှမဟုတ် target မိသားစုတစ်ခု) အတွက် code ကို ကိုင်ပြီး အဲဒီ platform ရဲ့ API တွေ (JVM မှာ java.io, Android မှာ android.*, iOS မှာ Apple framework တွေ) ကို သုံးလို့ရပါတယ်။ commonTest ကတော့ မျှဝေတဲ့ test တွေ ထားတဲ့နေရာပါ။ Target တွေကို Gradle မှာ jvm(), iosArm64(), iosSimulatorArm64() လို ကြေညာပြီး compiler က target တစ်ခုစီကို commonMain နဲ့ အဲဒီ target ရဲ့ ကိုယ်ပိုင် source set ပေါင်းပြီး build ပါတယ်။
| Source set | Compile လုပ်တဲ့နေရာ | သုံးခွင့်ရှိတာ |
|---|---|---|
| commonMain | target အားလုံး | common standard library နဲ့ multiplatform library တွေ |
| jvmMain | JVM target | Java library နဲ့ java.* API တွေ |
| iosMain | iOS target အားလုံး | Kotlin/Native interop ကနေ Apple framework တွေ |
| commonTest | target အားလုံးမှာ test | kotlin.test |
Common code က platform အလိုက် ကွဲတဲ့ အရာတစ်ခုကို လိုအပ်တဲ့အခါ expect နဲ့ ကြေညာပြီး target တစ်ခုစီကို ကိုယ်ပိုင် actual ပေးပါတယ်။ commonMain မှာ expect fun platformName(): String လို့ ရေးပြီး jvmMain နဲ့ iosMain တစ်ခုစီက ကိုယ်ပိုင် body နဲ့ actual fun platformName(): String ကို ပေးပါတယ်။ Compiler က target တိုင်းမှာ ကိုက်ညီတဲ့ actual ရှိမရှိ စစ်ပြီး မရှိတဲ့ target က compile မဖြစ်တဲ့အတွက် implementation ပျောက်နေတာ runtime အထိ မရောက်ပါဘူး။ expect နဲ့ actual က property တွေအတွက်လည်း အလုပ်လုပ်ပြီး class တွေအတွက်ကတော့ လက်ရှိ documentation မှာ စစ်သင့်တဲ့ သတိပေးချက်အချို့နဲ့ ရပါတယ်။
Platform နယ်နိမိတ်ကို ဖြတ်ဖို့ ဒုတိယနည်းလမ်း ရှိပြီး state ဒါမှမဟုတ် I/O ပါတဲ့ အရာတွေအတွက် များသောအားဖြင့် ဒါက ပိုကောင်းပါတယ်: common code ထဲမှာ ရိုးရိုး interface တစ်ခု သတ်မှတ်ပြီး implementation ကို shared logic ထဲ ထည့်ပေးတာပါ။ getString နဲ့ putString ပါတဲ့ Settings interface ကို Android ရဲ့ SharedPreferences၊ iOS ရဲ့ NSUserDefaults ဒါမှမဟုတ် in-memory map အပေါ် implement လုပ်လို့ရပြီး shared ThemeRepository က ဘယ်ဟာ ရလာတယ်ဆိုတာ ဘယ်တော့မှ မသိပါဘူး။ Test မှာတော့ fake တစ်ခု ပေးလိုက်ရုံပါပဲ။ Platform နာမည် ဒါမှမဟုတ် constant လို အသေးစား အချက်အလက်တွေအတွက် expect/actual ကိုသုံးပြီး service တွေအတွက် interface ကို သုံးပါ။
Common code က target တိုင်းမှာ ရှိတာကိုပဲ သုံးရပါမယ်။ java.io.File, java.util.UUID, Thread နဲ့ System.currentTimeMillis() တို့က JVM API တွေဖြစ်လို့ commonMain မှာ compile မဖြစ်ပါဘူး; common standard library, kotlinx-datetime, kotlinx-coroutines, Ktor လို multiplatform library တွေကို သုံးပါ၊ မဟုတ်ရင် expect/actual နဲ့ interface တွေကို သုံးပါ။ Build မှာလည်း platform ကန့်သတ်ချက်တွေ ရှိပါတယ်: Apple target တွေကို macOS နဲ့ Xcode ပေါ်မှာပဲ compile လုပ်နိုင်ပြီး target တစ်ခုစီမှာ ကိုယ်ပိုင် support အခြေအနေ ရှိတာမို့ အားကိုးမလုပ်ခင် official documentation ကို စစ်ပါ။
- စည်းမျဉ်းတွေကို commonMain မှာ တစ်ကြိမ်ရေး
- ကွဲတာကို ကြေညာ: expect ဒါမှမဟုတ် interface
- Target တစ်ခုစီက ကိုယ်ပိုင်အပိုင်း ပေး
- Compiler က common + target တစ်ခု ပေါင်း
Common code က Context ဒါမှမဟုတ် UIView လို platform type ကို လိုအပ်နေရင် နယ်နိမိတ်က နေရာမှားနေတာပါ: interface တစ်ခုကို ထည့်ပေးပါ။
အထွေထွေ ပုံစံ
// commonMain
expect fun platformFact(): String
fun shared(): String = "shared logic, running on " + platformFact()
// jvmMain
actual fun platformFact(): String = "JVM"
// iosMain
actual fun platformFact(): String = "iOS"commonMain ထဲက expect ကြေညာချက်က လက္ခဏာကိုပဲ ပေးပြီး body မပါပါဘူး။ target တစ်ခုစီက signature တူတူနဲ့ actual ကို ပေးရပါမယ်၊ shared() လို common function က platformFact() ကို ခေါ်ရုံပါပဲ။
Worked example: Settings interface နောက်ကွယ်က shared logic
ဒီဖိုင်က shared module သေးသေးလေးတစ်ခုရဲ့ အခန်းကဏ္ဍကို ကစားပြထားပါတယ်။ Settings interface နဲ့ ThemeRepository က 'common' အပိုင်းဖြစ်ပြီး platform တိုင်းမှာ ရှိတဲ့ အရာတွေကိုပဲ သုံးထားပါတယ်။ PrefixedSettings instance နှစ်ခုက platform implementation တွေကို ကိုယ်စားပြုပါတယ်၊ project အစစ်မှာတော့ တစ်ခုက Android ရဲ့ SharedPreferences ကို၊ တစ်ခုက iOS ရဲ့ NSUserDefaults ကို wrap လုပ်ပါလိမ့်မယ်။ main က shared test လို လုပ်ဆောင်ပါတယ်: logic တူတူကို နှစ်ခုလုံးမှာ run ပြီး ရလဒ်တွေကို check() နဲ့ အတည်ပြုပါတယ်။
ဒီမှာ expect/actual ကို ဘာကြောင့် မသုံးလဲ
Settings နောက်ကွယ်မှာ state နဲ့ I/O ရှိပြီး test က in-memory version နဲ့ လဲလှယ်ချင်ပါတယ်။ Interface က expect/actual class ကို အားမကိုးဘဲ နှစ်ခုလုံးကို လုပ်ပေးပါတယ်။ အသေးစား အချက်အလက်တစ်ခုအတွက် expect/actual ပုံစံက ဘယ်လိုလဲဆိုတာ နောက်ပိုင်းအပိုင်းမှာ ပြထားပါတယ်။
interface Settings {
fun getString(key: String): String?
fun putString(key: String, value: String)
}
class ThemeRepository(private val settings: Settings) {
private val allowed = listOf("light", "dark")
fun current(): String {
val stored = settings.getString("theme")
return stored?.takeIf { it in allowed } ?: "light"
}
fun toggle(): String {
val next = if (current() == "dark") "light" else "dark"
settings.putString("theme", next)
return next
}
}
class PrefixedSettings(
private val prefix: String,
val backing: MutableMap<String, String>,
) : Settings {
override fun getString(key: String): String? = backing[prefix + key]
override fun putString(key: String, value: String) {
backing[prefix + key] = value
}
}
fun main() {
val android = PrefixedSettings("prefs.", mutableMapOf())
val ios = PrefixedSettings("NSUserDefaults.", mutableMapOf())
for ((label, settings) in listOf("android-like" to android, "ios-like" to ios)) {
val repo = ThemeRepository(settings)
check(repo.current() == "light")
check(repo.toggle() == "dark")
check(ThemeRepository(settings).current() == "dark")
println("$label stores ${settings.backing}")
}
val corrupt = PrefixedSettings("x.", mutableMapOf("x.theme" to "neon"))
val fallback = ThemeRepository(corrupt).current()
check(fallback == "light")
println("a corrupt value falls back to $fallback")
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- The seam
interface Settings {Common code က ဒီ interface ကိုပဲ ပြောဆိုပြီး SharedPreferences ဒါမှမဟုတ် NSUserDefaults ကို ဘယ်တော့မှ တိုက်ရိုက် မကိုင်ပါဘူး။ ဒီနေရာက platform နယ်နိမိတ်ပါ။
- Shared validation
return stored?.takeIf { it in allowed } ?: "light"သိမ်းထားတဲ့ တန်ဖိုးက null ဖြစ်နေတာ ဒါမှမဟုတ် 'neon' လို မမှန်တဲ့ တန်ဖိုးဖြစ်နေရင် light ကို ပြန်ပါတယ်။ ဒီစည်းမျဉ်းကို တစ်ကြိမ်ပဲ ရေးထားလို့ platform အားလုံးမှာ တူညီပါတယ်။
- A platform implementation
class PrefixedSettings(ဒီ class က Settings ကို implement လုပ်တဲ့ 'platform' တစ်ခုကို ကိုယ်စားပြုပါတယ်။ Key တွေကို prefix နဲ့ သိမ်းတာက platform တစ်ခုစီရဲ့ သိမ်းဆည်းပုံ မတူတာကို ပုံဖော်ပါတယ်။
- Same logic, two platforms
for ((label, settings) in listOf("android-like" to android, "ios-like" to ios)) {Loop တစ်ခုတည်းက ThemeRepository ကို implementation နှစ်ခုနဲ့ run ပါတယ်။ Shared test ဆိုတာ ဒီပုံစံပါပဲ: platform တိုင်းမှာ ရလဒ်တူရမယ်လို့ အတည်ပြုတာပါ။
- State survives a restart
check(ThemeRepository(settings).current() == "dark")Repository အသစ်တစ်ခု ဖန်တီးပြီး Settings ထဲက သိမ်းထားတဲ့ တန်ဖိုးကို ဖတ်တာက app ကို ပိတ်ပြီး ပြန်ဖွင့်တာကို ဟန်ဆောင်တာပါ။ ရလဒ်က dark ဖြစ်နေသရွေ့ state ကို မှန်မှန်ကန်ကန် သိမ်းဆည်းနိုင်ပါတယ်။
android-like stores {prefs.theme=dark}
ios-like stores {NSUserDefaults.theme=dark}
a corrupt value falls back to light- commonMain က interface ဒါမှမဟုတ် expect function ကို ခေါ်
- Gradle က commonMain ကို target တစ်ခုရဲ့ source set နဲ့ compile
- Target ရဲ့ actual ဒါမှမဟုတ် implementation ကို ချိတ်ဆက်
- Shared logic တူတူက platform တိုင်းမှာ run
အစစ်အမှန် module ထဲက expect နဲ့ actual
ဖိုင်တစ်ခုတည်း program ထဲမှာ source set တွေ မရှိလို့ expect/actual ကို ဒီမှာ run မပြနိုင်ပါဘူး၊ ဒါပေမဲ့ Gradle module အစစ်မှာ ဖိုင်သုံးခုနဲ့ ဒီလို ပုံစံပါ။ greeting() ကို တစ်ကြိမ်ပဲ ရေးထားပြီး JVM target မှာ run ရင် Hello from JVM ကို ပြန်ပေးပါတယ်။ iOS actual က Apple ရဲ့ platform.UIKit framework ကို သုံးပါတယ်၊ ဒါက iosMain ထဲမှာပဲ ရေးလို့ရပါတယ်။
// shared/src/commonMain/kotlin/Platform.kt
expect fun platformName(): String
fun greeting(): String = "Hello from " + platformName()
// shared/src/jvmMain/kotlin/Platform.jvm.kt
actual fun platformName(): String = "JVM"
// shared/src/iosMain/kotlin/Platform.ios.kt
import platform.UIKit.UIDevice
actual fun platformName(): String = UIDevice.currentDevice.systemName()expect/actual class တွေအကြောင်း
expect နဲ့ actual ကို function တွေအတွက် သုံးတာက တည်ငြိမ်ပါတယ်၊ ဒါပေမဲ့ class တွေအတွက်ကတော့ လက်ရှိ Kotlin documentation မှာ အခြေအနေကို စစ်ပါ: compiler က သတိပေးတာ ဒါမှမဟုတ် opt-in flag တောင်းတာ ဖြစ်နိုင်ပါတယ်။ ဒါကြောင့် အကြောင်းမရှိရင် interface ကို ဦးစားပေးပါ။
Gradle setup နဲ့ folder layout
Multiplatform module က kotlin("multiplatform") plugin ကို သုံးပြီး target တွေကို kotlin { } block ထဲမှာ ကြေညာပါတယ်။ commonMain.dependencies နဲ့ commonTest.dependencies က shared code ရဲ့ dependency တွေကို ထိန်းပါတယ်။ iOS target နှစ်ခု ကြေညာတာနဲ့ iosMain source set ကို Kotlin က ကိုယ်တိုင် ထည့်ပေးပါတယ်။ Version တွေကို official release page မှာ စစ်ပြီး ထည့်ပါ။ Apple target တွေက macOS မှာပဲ build လုပ်လို့ရတာကို သတိရပါ။
plugins {
kotlin("multiplatform") version "2.1.21" // check the current Kotlin version
}
repositories {
mavenCentral()
}
kotlin {
jvm()
iosArm64()
iosSimulatorArm64()
sourceSets {
commonMain.dependencies {
// replace <version> with the current kotlinx.coroutines release
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:<version>")
}
commonTest.dependencies {
implementation(kotlin("test"))
}
}
}shared/
build.gradle.kts
src/
commonMain/kotlin/ shared logic, interfaces, expect declarations
commonTest/kotlin/ shared tests, run on every target
jvmMain/kotlin/ actual declarations for the JVM
iosMain/kotlin/ actual declarations for all iOS targetsInterface လား expect/actual လား
နှစ်ခုလုံးက platform နယ်နိမိတ်ကို ဖြတ်ပေမဲ့ ကောင်းတဲ့ အခြေအနေ မတူပါဘူး။ အောက်က table က လက်တွေ့ အခြေအနေအလိုက် ရွေးနည်းကို အကျဉ်းချုပ်ထားပါတယ်။
| အခြေအနေ | ဦးစားပေး | ဘာကြောင့်လဲ |
|---|---|---|
| အသေးစား platform အချက်အလက် (နာမည်, constant) | expect fun / expect val | target တစ်ခုစီမှာ actual တိုတိုလေး တစ်ခု၊ compiler က အားလုံးကို စစ်တယ် |
| state ဒါမှမဟုတ် I/O ပါတဲ့ service (storage, HTTP, logging) | interface နဲ့ injection | test မှာ fake လုပ်ရလွယ်ပြီး expect class ကို အားမကိုးရ |
| API တစ်ခုအောက်မှာ native SDK မတူတာတွေ | common မှာ interface၊ platform တစ်ခုစီမှာ implementation | platform app တွေက startup မှာ implementation အစစ်ကို ချိတ်ပေး |
Common code ထဲ ဘာတွေ သုံးလို့ရလဲ
JVM မှာ အကျင့်ပါနေတဲ့ developer တွေက java.* API ကို အလိုလို ခေါ်မိတတ်ပါတယ်။ commonMain မှာ အဲဒါတွေက compile မဖြစ်တာမို့ common standard library ပေါ်မှာပဲ တည်ဆောက်ဖို့ လိုပါတယ်။ အောက်က နှိုင်းယှဉ်ချက်မှာ ပထမ အုပ်စုက JVM-only ဖြစ်ပြီး ဒုတိယအုပ်စုက common standard library နဲ့ ရေးထားလို့ target တိုင်းမှာ compile ဖြစ်ပါတယ်။
// JVM-only: does NOT compile in commonMain
import java.util.UUID
val id = UUID.randomUUID().toString()
val now = System.currentTimeMillis()
// Common: compiles on every target
val cents = 1250L
val text = "${cents / 100}.${(cents % 100).toString().padStart(2, '0')}"လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ KMP ကို ဘယ်မှာ တွေ့ရလဲ
Team အများစုက KMP ကို အကုန်ပြန်ရေးတာနဲ့ မစဘဲ အစိတ်အပိုင်းလေးတစ်ခုနဲ့ စပါတယ်။ ရှိပြီးသား Android နဲ့ iOS app တွေဟာ shared module ကို dependency တစ်ခုအနေနဲ့ သုံးလို့ရတာမို့ တဖြည်းဖြည်း ပြောင်းလို့ရပါတယ်။
- မျှဝေလေ့ရှိတဲ့ အပိုင်းတွေ: networking, serialization, local database, validation, စျေးနှုန်း စည်းမျဉ်း၊ view-model state လို UI မကိုင်တဲ့ business logic တွေပါ။
- Multiplatform library တွေ: Ktor client (HTTP), kotlinx.serialization (JSON), SQLDelight (database), Koin (dependency injection) တို့က common code ထဲမှာ သုံးလို့ရပြီး platform တစ်ခုစီမှာ သင့်တော်တဲ့ engine ကို နောက်ကွယ်က ရွေးပေးပါတယ်။
- UI: platform တစ်ခုစီက native UI ကို ဆက်သုံးနိုင်သလို Compose Multiplatform နဲ့ UI code ကိုပါ မျှဝေနိုင်ပါတယ်၊ ဘယ်လောက်မျှဝေမလဲက team ရဲ့ ရွေးချယ်မှုပါ။
- CI: JVM နဲ့ Android test တွေကို Linux runner မှာ run နိုင်ပြီး iOS target တွေကိုတော့ macOS runner မှာ build နဲ့ test လုပ်ရပါတယ်။ JetBrains က case study တွေ ထုတ်ပြန်ထားလို့ လက်ရှိ အသုံးပြုပုံတွေကို အဲဒီမှာ ကြည့်နိုင်ပါတယ်။
အမှားများ
KMP နဲ့ ဖြစ်လေ့ရှိတဲ့ အမှားများ
commonMain ထဲမှာ JVM-only API သုံးခြင်း
java.io.File, java.util.UUID ဒါမှမဟုတ် System.currentTimeMillis() ကို အကျင့်အတိုင်း ရေးမိရင် commonMain က compile မဖြစ်ပါဘူး။ Platform-specific လိုအပ်ချက်ဆိုရင် interface ကို သတ်မှတ်ပြီး ပေးပါ၊ ဒါမှမဟုတ် kotlinx-datetime လို multiplatform library ကို သုံးပါ။
အရာအားလုံးကို expect/actual နဲ့ လုပ်ခြင်း
expect class အများကြီးရှိရင် test မှာ fake နဲ့ အစားထိုးရခက်ပြီး class တွေအတွက် expect/actual မှာ လက်ရှိ documentation နဲ့ ပြန်စစ်ရမယ့် သတိပေးချက် ပါပါတယ်။ State ဒါမှမဟုတ် I/O ပါတဲ့ အရာတွေအတွက် interface နဲ့ injection ကို ဦးစားပေးပါ။
Platform type တွေကို shared API ထဲ ယိုစီးစေခြင်း
Shared function တစ်ခုက Android ရဲ့ Context ဒါမှမဟုတ် iOS ရဲ့ UIView ကို parameter အဖြစ် လက်ခံရင် common code က compile မဖြစ်တော့ပါဘူး၊ ဒါမှမဟုတ် platform တစ်ခုနဲ့ ချည်နှောင်သွားပါတယ်။ လိုအပ်တဲ့ အရာကို Settings လို အသေးစား interface တစ်ခုအဖြစ် ဖော်ပြပြီး platform ဘက်မှာ implement လုပ်ပါ။
JVM test စိမ်းတာနဲ့ iOS မှာလည်း မှန်တယ်လို့ ထင်ခြင်း
Platform တစ်ခုစီမှာ runtime၊ standard library အစိတ်အပိုင်းနဲ့ threading စည်းမျဉ်း မတူတာတွေ ရှိပါတယ်။ commonTest ထဲက test တွေကို target တစ်ခုစီမှာ run ပါ (ဥပမာ jvmTest နဲ့ iOS simulator test) ၊ CI မှာ macOS runner ပါ ထည့်ပါ။
Best Practices
Best practices
- Validation နဲ့ model လို သေးသေးလေး module တစ်ခုနဲ့ စပါ၊ networking လို ပိုကြီးတာတွေကို နောက်မှ ရွှေ့ပါ။
- Platform အစွန်းတွေကို ပါးပါးထားပါ: expect ကြေညာချက် နဲ့ interface တွေက သေးသေးလေး၊ logic အားလုံးက commonMain မှာ ရှိပါစေ။
- Shared code ကို commonTest မှာ test လုပ်ပြီး fake တွေနဲ့ platform ကို အစားထိုးပါ။
- Target တစ်ခုစီရဲ့ support အခြေအနေနဲ့ library version တွေကို official documentation မှာ စစ်ပြီးမှ အားကိုးပါ။
Mini challenge
Mini challenge: fake clock နဲ့ အချိန်ကို သိတဲ့ နှုတ်ဆက်စကား
Shared code အတွက် Clock interface (hourOfDay(): Int) ကို သတ်မှတ်ပြီး greeting(clock, name) ကို ရေးပါ။ ၁၂ နာရီမတိုင်ခင် 'Good morning'၊ ၁၈ နာရီမတိုင်ခင် 'Good afternoon'၊ ကျန်တာက 'Good evening' ကို ပြန်ပေးရပါမယ်။ Platform အစစ်ကို ကိုယ်စားပြုတဲ့ Clock ကို test မှာ FixedClock နဲ့ အစားထိုးပါ။
- Clock interface နဲ့ hour ကို သိမ်းထားတဲ့ FixedClock class ကို ရေးပါ
- greeting က နာရီကို require(hour in 0..23) နဲ့ စစ်ပြီး when နဲ့ နှုတ်ဆက်စကားကို ရွေးရမယ်
- main ထဲမှာ 0, 11, 12, 17, 18, 23 နာရီတွေကို check() နဲ့ စစ်ပြီး print ထုတ်ပါ
- နာရီ 24 က IllegalArgumentException ကို message နဲ့ ပစ်ကြောင်း အတည်ပြုပါ
// "commonMain": shared code only knows this interface
interface Clock {
// TODO: declare hourOfDay(): Int
}
// "test code": a fake platform that always reports the same hour
// TODO: class FixedClock(...) : Clock
// TODO: require the hour to be in 0..23, then use when { } on the hour:
// before 12 -> Good morning, before 18 -> Good afternoon, else Good evening
fun greeting(clock: Clock, name: String): String {
TODO()
}
fun main() {
// TODO: check the boundary hours 0, 11, 12, 17, 18 and 23, printing each result
// TODO: check that hour 24 is rejected with the message
// "hour must be 0..23 but was 24"
}ရလဒ်: 0 -> Good morning, Mya၊ 11 -> Good morning, Mya၊ 12 -> Good afternoon, Mya၊ 17 -> Good afternoon, Mya၊ 18 -> Good evening, Mya၊ 23 -> Good evening, Mya နဲ့ hour 24 rejected: hour must be 0..23 but was 24 ဆိုပြီး print ထုတ်ပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
when { hour < 12 -> ...; hour < 18 -> ...; else -> ... } လို့ ရေးရင် အစီအစဉ်အလိုက် စစ်တာမို့ boundary က မှန်ပါတယ်။ boundary အတွက် Map<Int, String> တစ်ခုထဲမှာ expected တွေကို စုပြီး loop လုပ်ပါ။ IllegalArgumentException ကို ဖမ်းဖို့ try/catch ကို expression အဖြစ် သုံးပြီး message ကို ပြန်ယူပါ။
အဖြေ ကြည့်ရန်
interface Clock {
fun hourOfDay(): Int
}
class FixedClock(private val hour: Int) : Clock {
override fun hourOfDay(): Int = hour
}
fun greeting(clock: Clock, name: String): String {
val hour = clock.hourOfDay()
require(hour in 0..23) { "hour must be 0..23 but was $hour" }
val phrase = when {
hour < 12 -> "Good morning"
hour < 18 -> "Good afternoon"
else -> "Good evening"
}
return "$phrase, $name"
}
fun main() {
val expected = mapOf(
0 to "Good morning, Mya",
11 to "Good morning, Mya",
12 to "Good afternoon, Mya",
17 to "Good afternoon, Mya",
18 to "Good evening, Mya",
23 to "Good evening, Mya",
)
for ((hour, text) in expected) {
val actual = greeting(FixedClock(hour), "Mya")
check(actual == text) { "hour $hour: expected '$text' but was '$actual'" }
println("$hour -> $actual")
}
val failure = try {
greeting(FixedClock(24), "Mya")
null
} catch (e: IllegalArgumentException) {
e.message
}
check(failure == "hour must be 0..23 but was 24")
println("hour 24 rejected: $failure")
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- KMP က logic ကို commonMain မှာ တစ်ကြိမ်ရေးပြီး platform အလိုက်ကွဲတာတွေကို jvmMain, iosMain လို platform source set တွေမှာ ထားပါတယ်။
- expect ကြေညာချက်တိုင်းအတွက် target တစ်ခုစီမှာ actual လိုပြီး ပျောက်ရင် အဲဒီ target က compile မဖြစ်ပါဘူး။
- State ဒါမှမဟုတ် I/O ပါတဲ့ service တွေအတွက် interface နဲ့ injection ကို ဦးစားပေးပြီး expect/actual ကို အသေးစား အချက်အလက်တွေအတွက် သုံးပါ။
- Common code က java.* လို JVM-only API ကို မသုံးရဘူး၊ ပြီးတော့ iOS ကို macOS ပေါ်မှာပဲ build လုပ်နိုင်ပါတယ်။
What is Kotlin Multiplatform — Kotlin