Edit Page

添加跨平台库依赖项

最终更新: 2024/03/21

每个应用程序都需要一组库才能正常工作. 一个 Kotlin Multiplatform 项目可以依赖于可以在所有平台工作的跨平台库, 平台相关的库, 还可以依赖于其他跨平台项目.

要在一个库中添加依赖项, 需要更新你的项目包含共用代码的目录中的 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 库的依赖项

标准库

对每个源代码集(Source Set), 会自动添加对标准库 (stdlib) 的依赖项. 标准库的版本与 kotlin-multiplatform 版本相同.

对于与平台相关的源代码集, 会使用针对这个平台的标准库, 同时, 对其他源代码集会添加共通的标准库. Kotlin Gradle plugin 会根据你的 Gradle 构建脚本的 compilerOptions.jvmTarget 编译器选项 设置, 选择适当的 JVM 标准库.

详情请参见 如何改变默认设置.

测试库

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

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

kotlinx 库

如果使用跨平台的库, 并且需要 依赖共用代码, 只需要在共用源代码集中一次性设置依赖项. 请使用库的基本 artifact 名(base artifact name) – 比如 kotlinx-coroutines-core.

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

如果使用 kotlinx 库, 并且需要 与平台相关的依赖项, 那么可以通过 -jvm-js 之类的后缀, 来指定与平台相关的库版本, 比如, kotlinx-coroutines-core-jvm.

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

kotlin {
    sourceSets {
        jvmMain {
            dependencies {
                implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.7.3'
            }
        }
    }
}

对 Kotlin 跨平台库的依赖项

对于使用了 Kotlin Multiplatform 技术的库, 比如 SQLDelight, 你可以将它添加为依赖项. 关于在你的项目中如何添加这些依赖项, 这些库的作者通常会提供指南.

参见 由社区维护的 Kotlin Multiplatform 库列表.

对所有源代码集共用的库

如果你想要在所有的源代码集中使用一个库, 你可以只在共通源代码集中添加它. Kotlin Multiplatform Mobile plugin 会对所有其他源代码集自动添加对应的依赖项.

在共通源代码集中, 不可以设置对平台相关库的依赖项.

kotlin {
    sourceSets {
        commonMain.dependencies {
            implementation("io.ktor:ktor-client-core:2.3.5")
        }
        androidMain.dependencies {
            // 对 ktor-client 库的平台相关部分的依赖项, 会自动添加
        }
    }
}
kotlin {
    sourceSets {
        commonMain {
            dependencies {
                implementation 'io.ktor:ktor-client-core:2.3.5'
            }
        }
        androidMain {
            dependencies {
                // 对 ktor-client 库的平台相关部分的依赖项, 会自动添加
            }
        }
    }
}

在特定源代码集中使用的库

如果你只想对特定的源代码集使用一个跨平台库, 你可以只在这些源代码集中添加它的依赖项. 特定库中的声明, 将只能在这些源代码集中使用.

这种情况下请使用库的通用名称, 而不要使用平台相关的名称, 比如对下面示例中的 SQLDelight, 请使用 native-driver, 而不要使用 native-driver-iosx64. 请到库的文档中查找确切的名称.

kotlin {
    sourceSets {
        commonMain.dependencies {
            // kotlinx.coroutines 可以在所有源代码集中使用
            implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
        }
        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.7.3'
            }
        }
        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 的平台相关部分, 会自动添加
            }
        }
    }
}

下一步做什么?

查看跨平台项目中添加依赖项的其他资料, 并学习以下内容: