Kotlin Help

Add dependencies

To add a dependency on a library, set a dependency of the required type (for example, implementation) in the dependencies block in your Gradle build script.

kotlin { sourceSets { val commonMain by getting { dependencies { implementation("com.example:my-library:1.0") } } } }
kotlin { sourceSets { commonMain { dependencies { implementation 'com.example:my-library:1.0' } } } }

Alternatively, you can set dependencies at the top level.

Dependency on the 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 kotlinOptions.jvmTarget compiler option of your Gradle build script

Learn how to change the default behavior.

Set dependencies on 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.

Set a dependency on a kotlinx library

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. You can also use the library base artifact name instead – kotlinx-coroutines-core.

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

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 or ktor-client-core.

kotlin { sourceSets { val commonMain by getting { dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2") } } } }
kotlin { sourceSets { commonMain { dependencies { implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2' } } } }
Last modified: 08 September 2021