Adding dependencies on multiplatform libraries

Every program requires a set of libraries to operate successfully. A Kotlin Multiplatform project can depend on multiplatform libraries that work for all target platforms, platform-specific libraries, and other multiplatform projects.

To add a dependency on a library, update your build.gradle(.kts) file in the shared directory of your project. Set a dependency of the required type (for example, implementation) in the dependencies block:

【Kotlin】

  1. kotlin {
  2. sourceSets {
  3. val commonMain by getting {
  4. dependencies {
  5. implementation("com.example:my-library:1.0") // library shared for all source sets
  6. }
  7. }
  8. }
  9. }

【Groovy】

  1. kotlin {
  2. sourceSets {
  3. commonMain {
  4. dependencies {
  5. implementation 'com.example:my-library:1.0'
  6. }
  7. }
  8. }
  9. }

Alternatively, you can set dependencies at the top level.

Dependency on a Kotlin library

Standard library

A dependency on a standard library (stdlib) in each source set is added automatically. The version of the standard library is the same as the version of the kotlin-multiplatform plugin.

For platform-specific source sets, the corresponding platform-specific variant of the library is used, while a common standard library is added to the rest. The Kotlin Gradle plugin will select the appropriate JVM standard library depending on the compilerOptions.jvmTarget compiler option of your Gradle build script.

Learn how to change the default behavior.

Test libraries

The kotlin.test API is available for multiplatform tests. When you create a multiplatform project, the Project Wizard automatically adds test dependencies to common and platform-specific source sets.

If you didn’t use the Project Wizard to create your project, you can add the dependencies manually.

kotlinx libraries

If you use a multiplatform library and need to depend on the shared code, set the dependency only once in the shared source set. Use the library base artifact name, such as kotlinx-coroutines-core.

【Kotlin】

  1. kotlin {
  2. sourceSets {
  3. val commonMain by getting {
  4. dependencies {
  5. implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1")
  6. }
  7. }
  8. }
  9. }

【Groovy】

  1. kotlin {
  2. sourceSets {
  3. commonMain {
  4. dependencies {
  5. implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1'
  6. }
  7. }
  8. }
  9. }

If you use a kotlinx library and need a platform-specific dependency, you can use platform-specific variants of libraries with suffixes such as -jvm or -js, for example, kotlinx-coroutines-core-jvm.

【Kotlin】

  1. kotlin {
  2. sourceSets {
  3. val jvmMain by getting {
  4. dependencies {
  5. implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.7.1")
  6. }
  7. }
  8. }
  9. }

【Groovy】

  1. kotlin {
  2. sourceSets {
  3. jvmMain {
  4. dependencies {
  5. implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.7.1'
  6. }
  7. }
  8. }
  9. }

Dependency on Kotlin Multiplatform libraries

You can add dependencies on libraries that have adopted Kotlin Multiplatform technology, such as SQLDelight. The authors of these libraries usually provide guides for adding their dependencies to your project.

Check out this community-maintained list of Kotlin Multiplatform libraries.

Library shared for all source sets

If you want to use a library from all source sets, you can add it only to the common source set. The Kotlin Multiplatform Mobile plugin will automatically add the corresponding parts to any other source sets.

You cannot set dependencies on platform-specific libraries in the common source set.

添加依赖项 - 图1

【Kotlin】

  1. kotlin {
  2. sourceSets {
  3. val commonMain by getting {
  4. dependencies {
  5. implementation("io.ktor:ktor-client-core:2.3.2")
  6. }
  7. }
  8. val androidMain by getting {
  9. dependencies {
  10. // dependency to a platform part of ktor-client will be added automatically
  11. }
  12. }
  13. }
  14. }

【Groovy】

  1. kotlin {
  2. sourceSets {
  3. commonMain {
  4. dependencies {
  5. implementation 'io.ktor:ktor-client-core:2.3.2'
  6. }
  7. }
  8. androidMain {
  9. dependencies {
  10. // dependency to platform part of ktor-client will be added automatically
  11. }
  12. }
  13. }
  14. }

Library used in specific source sets

If you want to use a multiplatform library just for specific source sets, you can add it exclusively to them. The specified library declarations will then be available only in those source sets.

Don’t use a platform-specific name in such cases, like SQLDelight native-driver in the example below. Find the exact name in the library’s documentation.

添加依赖项 - 图2

【Kotlin】

  1. kotlin {
  2. sourceSets {
  3. val commonMain by getting {
  4. dependencies {
  5. // kotlinx.coroutines will be available in all source sets
  6. implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1")
  7. }
  8. }
  9. val androidMain by getting {
  10. dependencies {}
  11. }
  12. val iosMain by getting {
  13. dependencies {
  14. // SQLDelight will be available only in the iOS source set, but not in Android or common
  15. implementation("com.squareup.sqldelight:native-driver:1.5.5")
  16. }
  17. }
  18. }
  19. }

【Groovy】

  1. kotlin {
  2. sourceSets {
  3. commonMain {
  4. dependencies {
  5. // kotlinx.coroutines will be available in all source sets
  6. implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1'
  7. }
  8. }
  9. androidMain {
  10. dependencies {}
  11. }
  12. iosMain {
  13. dependencies {
  14. // SQLDelight will be available only in the iOS source set, but not in Android or common
  15. implementation 'com.squareup.sqldelight:native-driver:1.5.5'
  16. }
  17. }
  18. }
  19. }

Dependency on another multiplatform project

You can connect one multiplatform project to another as a dependency. To do this, simply add a project dependency to the source set that needs it. If you want to use a dependency in all source sets, add it to the common one. In this case, other source sets will get their versions automatically.

【Kotlin】

  1. kotlin {
  2. sourceSets {
  3. val commonMain by getting {
  4. dependencies {
  5. implementation(project(":some-other-multiplatform-module"))
  6. }
  7. }
  8. val androidMain by getting {
  9. dependencies {
  10. // platform part of :some-other-multiplatform-module will be added automatically
  11. }
  12. }
  13. }
  14. }

【Groovy】

  1. kotlin {
  2. sourceSets {
  3. commonMain {
  4. dependencies {
  5. implementation project(':some-other-multiplatform-module')
  6. }
  7. }
  8. androidMain {
  9. dependencies {
  10. // platform part of :some-other-multiplatform-module will be added automatically
  11. }
  12. }
  13. }
  14. }

下一步做什么?

Check out other resources on adding dependencies in multiplatform projects and learn more about: