設定環境 (Kotlin Multiplatform)
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Kotlin Multiplatform (KMP) 可跨平台共用 Kotlin 程式碼。開始使用 KMP 建構應用程式前,您必須按照本文件所述設定環境。您也可以參考 JetBrain 的官方說明文件。
安裝或更新必要工具
建立 Kotlin Multiplatform 專案
您可以使用 JetBrains 的 Kotlin Multiplatform 精靈建立新的 KMP 專案。請務必選擇「Do not share UI」選項,保留原生 UI。
專案架構
KMP 專案採用類似 Android 專案的專案結構。
KMP 專案包含特定平台的模組和共用模組。將平台專屬程式碼新增至相關模組。舉例來說,請在 androidApp 模組中新增 Android 應用程式 UI,並在 iosApp 中新增 iOS 應用程式 UI。您想在不同平台之間共用的任何程式碼,都應放入「shared」模組。
共用模組會像專案的其他部分一樣,使用 Gradle 做為建構系統。您可以使用來源集宣告常見和平台專屬的依附元件。舉例來說,如果您的應用程式使用 Ktor 進行網路連線,就需要為 Android 新增 OkHttp 依附元件,並為 iOS 新增 darwin 依附元件。請注意,有些程式庫只需要一般依附元件,不需要平台專屬依附元件。
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 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],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."]]