Kotlin Multiplatform Help

Add dependencies to your project

You've created and tweaked your first Kotlin Multiplatform project! Now let's learn how to add dependencies to third-party libraries, which is necessary for building successful cross-platform applications.

Dependency types

There are two types of dependencies that you can use in Kotlin Multiplatform projects:

  • Multiplatform dependencies. These are multiplatform libraries that support multiple targets and can be used in the common source set, commonMain.

    Many modern Android libraries already have multiplatform support, like Koin, Coil, and SQLDelight. Find more multiplatform libraries on klibs.io, a search service offered by JetBrains for discovering Kotlin Multiplatform libraries.

  • Native dependencies. These are regular libraries from specific ecosystems. In native projects you usually work with them, for example, using Gradle for Android and Swift Package Manager for iOS.

    When you work with a multiplatform project module, typically, you still need native dependencies to use platform APIs such as security storage, specific system calls, and so on. In the build script, you specify native dependencies in the configuration of native source sets, for example, androidMain and iosMain.

For both types of dependencies, you can use local and external repositories.

Add a multiplatform dependency

Let's make the greeting a little more festive: In addition to the OS version, add a function to display the number of days left until New Year's Day. The kotlinx-datetime library, which has full multiplatform support, is the most convenient way to work with dates in your shared code.

  1. Open the gradle/libs.versions.toml file and add the kotlinx-datetime dependency to the version catalog:

    [versions] kotlinx-datetime = "0.8.0" [libraries] kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinx-datetime" }
  2. Open the sharedLogic/build.gradle.kts file and add a reference to that library entry to the section that configures the common code source set:

    kotlin { //... sourceSets { commonMain.dependencies { implementation(libs.kotlinx.datetime) } } }
  3. Select the Build | Sync Project with Gradle Files menu item or click the Sync Gradle Changes button in the build script editor to synchronize Gradle files: Synchronize Gradle files

Call a kotlinx-datetime API

With the dependency added, you can add date and time calculations to your common code:

  1. Right-click the sharedLogic/src/commonMain/.../greetingkmp directory and select New | Kotlin Class/File to create a new file, NewYear.kt.

  2. In NewYear.kt, add two functions that calculate the number of days from today until the start of next year using the datetime date arithmetic and form the phrase to be displayed:

    fun daysUntilNewYear(): Int { val today = Clock.System.todayIn(TimeZone.currentSystemDefault()) val closestNewYear = LocalDate(today.year + 1, 1, 1) return today.daysUntil(closestNewYear) } fun daysPhrase(): String = "There are only ${daysUntilNewYear()} days left until New Year! 🎆"
  3. Add all necessary imports as suggested by the IDE. Make sure to import kotlin.time.Clock, not kotlinx.datetime.Clock.

  4. In the Greeting.kt file, update the Greeting class to see the result:

    class Greeting { private val platform: Platform = getPlatform() fun greet(): List<String> = buildList { add(if (Random.nextBoolean()) "Hi!" else "Hello!") add("Guess what this is! > ${platform.name.reversed()}!") add(daysPhrase()) } }
  5. To see the results, re-run your androidApp and iosApp run configurations from IntelliJ IDEA:

Updated mobile multiplatform app with external dependencies

Next step

In the next part of the tutorial, you'll add more dependencies and more complex logic to your project.

Proceed to the next part

See also

Get help

15 May 2026