ตั้งค่าสภาพแวดล้อม (Kotlin Multiplatform)
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
Kotlin Multiplatform (KMP) ช่วยให้แชร์โค้ด Kotlin ข้ามแพลตฟอร์มได้ ก่อนเริ่มสร้างแอปด้วย KMP คุณจะต้องตั้งค่าสภาพแวดล้อมตามที่อธิบายไว้ในเอกสารนี้ นอกจากนี้ คุณยังดูเอกสารประกอบอย่างเป็นทางการของ JetBrain ได้ด้วย
ติดตั้งหรืออัปเดตเครื่องมือที่จำเป็น
- ติดตั้งหรืออัปเดต Android Studio เป็นเวอร์ชันเสถียรล่าสุด
- อัปเดตปลั๊กอิน Kotlin ที่มาพร้อมกับ Android Studio เป็นเวอร์ชันล่าสุดเพื่อหลีกเลี่ยงปัญหาความเข้ากันได้
- (ไม่บังคับ) สําหรับการพัฒนา iOS ให้ติดตั้ง Xcode เพื่อสร้าง UI และเพิ่มโค้ด Swift หรือ Objective-C ตามต้องการ
สร้างโปรเจ็กต์ Kotlin Multiplatform
คุณสามารถใช้วิซาร์ด Kotlin Multiplatform จาก JetBrains เพื่อสร้างโปรเจ็กต์ KMP ใหม่ อย่าลืมเลือกตัวเลือกอย่าแชร์ UI เพื่อคง UI เดิมไว้
โครงสร้างโปรเจ็กต์
โปรเจ็กต์ KMP มีโครงสร้างโปรเจ็กต์คล้ายกับโปรเจ็กต์ Android
โปรเจ็กต์ KMP มีโมดูลเฉพาะแพลตฟอร์มพร้อมกับโมดูลที่แชร์
เพิ่มโค้ดเฉพาะแพลตฟอร์มลงในโมดูลที่เกี่ยวข้อง เช่น เพิ่ม UI ของแอป Android ในโมดูล androidApp และ UI ของแอป iOS ใน iosApp
โค้ดที่ต้องการแชร์ระหว่างแพลตฟอร์มต่างๆ จะอยู่ในส่วนโมดูลที่แชร์
โมดูลที่แชร์ใช้ Gradle เป็นระบบบิลด์เช่นเดียวกับโปรเจ็กต์ส่วนอื่นๆ คุณสามารถประกาศการพึ่งพาทั่วไปและเฉพาะแพลตฟอร์มได้โดยใช้แหล่งที่มา ตัวอย่างเช่น หากแอปใช้ Ktor สำหรับเครือข่าย คุณจะต้องเพิ่มข้อกําหนดของ OkHttp สําหรับ Android และข้อกําหนดของ darwin สําหรับ iOS โปรดทราบว่าไลบรารีบางรายการต้องใช้เฉพาะไลบรารีที่ใช้ร่วมกันและไม่จำเป็นต้องใช้ไลบรารีเฉพาะแพลตฟอร์ม
sourceSets {
commonMain.dependencies {
//put your multiplatform dependencies here
//...
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.content.negotiation)
implementation(libs.ktor.serialization.kotlinx.json)
//...
}
androidMain.dependencies {
implementation(libs.ktor.client.okhttp)
}
iosMain.dependencies {
implementation(libs.ktor.client.darwin)
}
}
เมื่อเพิ่มไลบรารีใหม่ลงในโมดูลที่แชร์ของแอป โปรดตรวจสอบว่ามีแพลตฟอร์มใดบ้างที่ต้องพึ่งพาไลบรารีนั้น
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[null,null,["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Setup your environment (Kotlin Multiplatform)\n\n[Kotlin Multiplatform](https://kotlinlang.org/lp/mobile/) (KMP) enables sharing Kotlin code across\ndifferent platforms. Before you start building apps with KMP, you'll need to\nset up your environment as described in this document. You can also refer to\nJetBrain's [official documentation](https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-setup.html).\n\nInstall or update required tools\n--------------------------------\n\n- Install or update to the latest stable version of [Android Studio](/studio).\n- Update the [Kotlin plugin](https://kotlinlang.org/docs/releases.html#update-to-a-new-release) that is bundled with Android Studio to the latest version to avoid compatibility issues.\n- (Optional) For iOS development, install [Xcode](https://apps.apple.com/us/app/xcode/id497799835) to build the UI and add Swift or Objective-C code as needed.\n\nCreate a Kotlin Multiplatform project\n-------------------------------------\n\nYou can use the [Kotlin Multiplatform wizard](https://kmp.jetbrains.com/) from JetBrains to\ncreate a new KMP project. Make sure to choose the **Do not\nshare UI** option to keep the UI native.\n\n### Project structure\n\nKMP projects follow a project structure similar to Android projects.\n\nA KMP project contains platform-specific modules along with a shared module.\nAdd your platform-specific code to the relevant module. For example, add your\nAndroid app UI in the **androidApp** module and your iOS app UI in **iosApp** .\nAny code you want to share between platforms goes in the **shared** module.\n\nThe shared module uses Gradle as the build system just like the rest of the\nproject. You can declare common and platform-specific dependencies using\nsourcesets. For example, if your app uses Ktor for networking, you need to add\nan OkHttp dependency for Android and a darwin dependency for iOS. Note that some\nlibraries require only common dependencies and don't need platform-specific\ndependencies. \n\n sourceSets {\n commonMain.dependencies {\n //put your multiplatform dependencies here\n //...\n implementation(libs.ktor.client.core)\n implementation(libs.ktor.client.content.negotiation)\n implementation(libs.ktor.serialization.kotlinx.json)\n //...\n }\n androidMain.dependencies {\n implementation(libs.ktor.client.okhttp)\n }\n iosMain.dependencies {\n implementation(libs.ktor.client.darwin)\n }\n }\n\nWhen you add a new library to your app's shared module, make sure to check for\nthe required dependencies for each platform."]]