将 Kotlin 库的依赖项添加到 Kotlin/Wasm 项目

You can use the Kotlin standard library (stdlib) and test library (kotlin.test) in Kotlin/Wasm out of the box. The version of these libraries is the same as the version of the kotlin-multiplatform plugin.

Other official Kotlin (kotlinx) and multiplatform libraries are not fully supported yet. You can try experimental versions of such libraries by adding the Kotlin experimental repository to your Gradle project.

For Kotlin 1.9.0 and later, use the latest available libraries’ versions.

添加 Kotlin 库依赖 - 图1

Supported Kotlin libraries for Kotlin/Wasm

You can use one of the following repositories to add Kotlin libraries to your project:

  • Maven Central for stdlib and kotlin.test libraries:

    1. // build.gradle.kts
    2. repositories {
    3. mavenCentral()
    4. }
  • Custom Maven repository for experimental Kotlin/Wasm artifacts:

    1. // build.gradle.kts
    2. repositories {
    3. maven("https://maven.pkg.jetbrains.space/kotlin/p/wasm/experimental")
    4. }
  • Custom Maven repository for Compose Multiplatform dev artifacts:

    1. // build.gradle.kts
    2. repositories {
    3. maven("https://maven.pkg.jetbrains.space/public/p/compose/dev/")
    4. }
LibraryVersionRepository
stdlib1.9.10Maven Central
kotlin-test1.9.10Maven Central
kotlinx-coroutines1.7.2-wasm0Custom for experimental Kotlin/Wasm artifacts
Compose Multiplatform1.4.0-dev-wasm09Custom for experimental Kotlin/Wasm artifacts
kotlinx-serialization1.5.2-wasm0Custom for experimental Kotlin/Wasm artifacts
Ktor2.3.3-wasm0Custom for experimental Kotlin/Wasm artifacts
kotlinx-atomicfu0.21.0-wasm0Custom for experimental Kotlin/Wasm artifacts
kotlinx-collections-immutable0.4-wasm0Custom for experimental Kotlin/Wasm artifacts
kotlinx-datetime0.4.0-wasm1Custom for experimental Kotlin/Wasm artifacts
skiko0.0.7.68-wasm03Custom for Compose Multiplatform dev artifacts

Enable libraries in your project

To set a dependency on a library, such as kotlinx.serilization and kotlinx.coroutines, update your build.gradle.kts file:

  1. // `build.gradle.kts`
  2. repositories {
  3. maven("https://maven.pkg.jetbrains.space/kotlin/p/wasm/experimental")
  4. }
  5. kotlin {
  6. sourceSets {
  7. val wasmMain by getting {
  8. dependencies {
  9. implementation("org.jetbrains.kotlinx:kotlinx-serialization-core-wasm:1.5.1-wasm0")
  10. implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-wasm:1.6.4-wasm0")
  11. implementation("io.ktor:ktor-client-core-wasm:2.3.1-wasm0")
  12. }
  13. }
  14. }
  15. }

What’s next?

Explore the Kotlin/Wasm interoperability with JavaScript