Add dependencies to your project
You've already created your first cross-platform 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, Apollo, and Okio.
Native dependencies. These are regular libraries from relevant ecosystems. In native projects you usually work with them using Gradle for Android and using CocoaPods or another dependency manager for iOS.
When you work with a shared module, typically, you still need native dependencies when you want to use platform APIs such as security storage. You can add native dependencies to the native source sets,
androidMain
andiosMain
.
For both types of dependencies, you can use local and external repositories.
Add a multiplatform dependency
Let's go back to the app and make the greeting a little more festive. In addition to the device information, 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.
Open the
build.gradle.kts
file located in theshared
directory.Add the following dependency to the
commonMain
source set dependencies:kotlin { sourceSets { commonMain.dependencies { implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.0") } } }Synchronize the Gradle files by clicking Sync Now in the notification.
In
shared/src/commonMain/kotlin
, create a new fileNewYear.kt
in the project directory.Update the file with a short function that calculates the number of days from today until the New Year using the
date-time
date arithmetic:import kotlinx.datetime.* 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! 🎆"In
Greeting.kt
, update theGreeting
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()) } }To see the results, re-run your composeApp and iosApp configurations from Android Studio:
Next step
In the next part of the tutorial, you'll add more dependencies and more complex logic to your project.
See also
Discover how to work with multiplatform dependencies of all kinds: Kotlin libraries, Kotlin Multiplatform libraries, and other multiplatform projects.
Learn how to add Android dependencies and iOS dependencies with or without CocoaPods for use in platform-specific source sets.
Check out the examples of how to use Android and iOS libraries in sample projects.
Get help
Kotlin Slack. Get an invite and join the #multiplatform channel.
Kotlin issue tracker. Report a new issue.