Kotlin 中文文档 v2.0.10 Help

添加对跨平台库的依赖

每个程序都需要一组库才能成功运行。 一个 Kotlin 跨平台项目可以依赖于适用于所有目标平台的多平台库、特定平台的库以及其他多平台项目。

要添加对库的依赖,请更新项目中包含共享代码的目录下的 build.gradle(.kts) 文件。 在 dependencies {} 块中,设置所需的依赖 类型 (例如, implementation):

kotlin { sourceSets { commonMain.dependencies { implementation("com.example:my-library:1.0") // 共享给所有源代码集的库 } } }
kotlin { sourceSets { commonMain { dependencies { implementation 'com.example:my-library:1.0' // 共享给所有源代码集的库 } } } }

或者,你可以 在顶层设置依赖

对 Kotlin 库的依赖

标准库

每个源代码集中的标准库(stdlib )依赖都会自动添加。 标准库的版本与 kotlin-multiplatform 插件的版本相同。

对于特定平台的源代码集,将使用相应平台的库变体,而其余部分将添加通用标准库。 Kotlin Gradle 插件将根据 Gradle 构建脚本中的 compilerOptions.jvmTarget 编译器选项 选择适当的 JVM 标准库。

了解如何 更改默认行为

测试库

对于跨平台测试,可以使用 kotlin.test API。 当你创建一个跨平台项目时,可以在 commonTest 中使用单一依赖来为所有源代码集添加测试依赖:

kotlin { sourceSets { commonTest.dependencies { implementation(kotlin("test")) // 自动引入所有平台依赖 } } }
kotlin { sourceSets { commonTest { dependencies { implementation kotlin("test") // 自动引入所有平台依赖 } } } }

kotlinx 库

如果你使用多平台库并且需要 依赖共享代码 ,只需在共享源代码集中设置一次依赖。 使用库的基础构件名称,例如 kotlinx-coroutines-core

kotlin { sourceSets { commonMain.dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1") } } }
kotlin { sourceSets { commonMain { dependencies { implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1' } } } }

如果你使用 kotlinx 库并且需要 特定平台依赖, 可以使用带有后缀如 -jvm-js 的平台特定库变体, 例如 kotlinx-coroutines-core-jvm

kotlin { sourceSets { jvmMain.dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.8.1") } } }
kotlin { sourceSets { jvmMain { dependencies { implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.8.1' } } } }

对 Kotlin 跨平台库的依赖

你可以添加对已经采用 Kotlin 跨平台技术的库的依赖,例如 SQLDelight。 这些库的作者通常会提供将其依赖项添加到项目中的指南。

查看这个 社区维护的 Kotlin 跨平台库列表

共享给所有源代码集的库

如果你想在所有源代码集中使用某个库,可以仅将其添加到通用源代码集中。 Kotlin 跨平台移动插件将自动将相应部分添加到其他源代码集中。

kotlin { sourceSets { commonMain.dependencies { implementation("io.ktor:ktor-client-core:2.3.12") } androidMain.dependencies { // ktor-client 的平台部分依赖将被自动添加 } } }
kotlin { sourceSets { commonMain { dependencies { implementation 'io.ktor:ktor-client-core:2.3.12' } } androidMain { dependencies { // ktor-client 的平台部分依赖将被自动添加 } } } }

用于特定源代码集的库

如果你只想在特定的源代码集中使用某个多平台库,可以将其专门添加到这些源代码集中。 指定的库声明将仅在这些源代码集中可用。

kotlin { sourceSets { commonMain.dependencies { // kotlinx.coroutines 将在所有源代码集中可用 implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1") } androidMain.dependencies { } iosMain.dependencies { // SQLDelight 将仅在 iOS 源代码集中可用,而在 Android 或通用源代码集中不可用 implementation("com.squareup.sqldelight:native-driver:2.0.0") } } }
kotlin { sourceSets { commonMain { dependencies { // kotlinx.coroutines 将在所有源代码集中可用 implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1' } } androidMain { dependencies {} } iosMain { dependencies { // SQLDelight 将仅在 iOS 源代码集中可用,而在 Android 或通用源代码集中不可用 implementation 'com.squareup.sqldelight:native-driver:2.0.0' } } } }

对另一个多平台项目的依赖

你可以将一个多平台项目作为依赖连接到另一个项目。为此,只需将项目依赖添加到需要它的源代码集中。 如果你想在所有源代码集中使用该依赖,请将其添加到通用源代码集中。 在这种情况下,其他源代码集将自动获取它们的版本。

kotlin { sourceSets { commonMain.dependencies { implementation(project(":some-other-multiplatform-module")) } androidMain.dependencies { // :some-other-multiplatform-module 的平台部分将被自动添加 } } }
kotlin { sourceSets { commonMain { dependencies { implementation project(':some-other-multiplatform-module') } } androidMain { dependencies { // :some-other-multiplatform-module 的平台部分将被自动添加 } } } }

接下来做什么?

查看有关在多平台项目中添加依赖的其他资源,并了解更多内容:

Last modified: 08 九月 2024