Thiết lập môi trường (Kotlin Multiplatform)
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Kotlin Multiplatform (KMP) cho phép chia sẻ đoạn mã Kotlin trên nhiều nền tảng. Trước khi bắt đầu tạo ứng dụng bằng KMP, bạn cần thiết lập môi trường như mô tả trong tài liệu này. Bạn cũng có thể tham khảo tài liệu chính thức của JetBrain.
Cài đặt hoặc cập nhật các công cụ bắt buộc
- Cài đặt hoặc cập nhật lên phiên bản ổn định mới nhất của Android Studio.
- Cập nhật trình bổ trợ Kotlin đi kèm với Android Studio lên phiên bản mới nhất để tránh các vấn đề về khả năng tương thích.
- (Không bắt buộc) Để phát triển iOS, hãy cài đặt Xcode để tạo giao diện người dùng và thêm mã Swift hoặc Objective-C nếu cần.
Tạo dự án Kotlin Multiplatform
Bạn có thể sử dụng trình hướng dẫn Kotlin Multiplatform của JetBrains để tạo một dự án KMP mới. Hãy nhớ chọn tuỳ chọn Do not share UI (Không chia sẻ giao diện người dùng) để giữ giao diện người dùng gốc.
Cấu trúc dự án
Các dự án KMP tuân theo cấu trúc dự án tương tự như các dự án Android.
Dự án KMP chứa các mô-đun dành riêng cho nền tảng cùng với một mô-đun dùng chung.
Thêm mã dành riêng cho nền tảng vào mô-đun có liên quan. Ví dụ: thêm giao diện người dùng ứng dụng Android trong mô-đun androidApp và giao diện người dùng ứng dụng iOS trong iosApp.
Mọi mã bạn muốn chia sẻ giữa các nền tảng đều nằm trong mô-đun shared (chia sẻ).
Mô-đun dùng chung sử dụng Gradle làm hệ thống xây dựng giống như phần còn lại của dự án. Bạn có thể khai báo các phần phụ thuộc chung và dành riêng cho nền tảng bằng cách sử dụng nhóm tài nguyên. Ví dụ: nếu ứng dụng của bạn sử dụng Ktor để kết nối mạng, bạn cần thêm phần phụ thuộc OkHttp cho Android và phần phụ thuộc darwin cho iOS. Xin lưu ý rằng một số thư viện chỉ yêu cầu các phần phụ thuộc phổ biến và không cần các phần phụ thuộc dành riêng cho nền tảng.
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)
}
}
Khi thêm một thư viện mới vào mô-đun dùng chung của ứng dụng, hãy nhớ kiểm tra các phần phụ thuộc bắt buộc cho từng nền tảng.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 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."]]