设置您的环境(Kotlin 多平台)
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Kotlin Multiplatform (KMP) 支持跨不同平台共享 Kotlin 代码。在开始使用 KMP 构建应用之前,您需要按照本文档中的说明设置环境。您还可以参阅 JetBrain 的官方文档。
安装或更新所需的工具
- 安装或更新到最新的稳定版 Android Studio。
- 将与 Android Studio 捆绑的 Kotlin 插件更新到最新版本,以避免兼容性问题。
- (可选)如需进行 iOS 开发,请安装 Xcode 以构建界面,并根据需要添加 Swift 或 Objective-C 代码。
创建 Kotlin 多平台项目
您可以使用 JetBrains 提供的 Kotlin Multiplatform 向导创建新的 KMP 项目。请务必选择不共享界面选项,以保持界面为原生界面。
项目结构
KMP 项目采用与 Android 项目类似的项目结构。
KMP 项目包含平台专用模块以及共享模块。将平台专用代码添加到相关模块中。例如,在 androidApp 模块中添加 Android 应用界面,在 iosApp 中添加 iOS 应用界面。您希望在平台之间共享的所有代码都应放在 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 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):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."]]