向项目中添加依赖

This is the fourth part of the Getting started with Kotlin Multiplatform for mobile tutorial. Before proceeding, make sure you’ve completed previous steps.

First step Set up an environment
Second step Create your first cross-platform app
Third step Update the user interface
Fourth step Add dependencies
Fifth step Share more logic
Sixth step Wrap up 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 Multiplatform Mobile 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. You usually work with them in native iOS projects using CocoaPods or another dependency manager and in Android projects using Gradle.

When you work with a shared module, you can also depend on native dependencies and use them in the native source sets, androidMain and iosMain. Typically, you’ll need these dependencies when you want to work with platform APIs, for example, security storage, and there is common logic.

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

Add a multiplatform dependency

If you have experience developing Android apps, adding a multiplatform dependency is similar to adding a Gradle dependency in a regular Android project. The only difference is that you need to specify the source set.

4. 添加依赖 - 图7

Let’s now 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.

  1. Navigate to the build.gradle.kts file in the shared directory.
  2. Add the following dependency to the commonMain source set dependencies:

    1. kotlin {
    2. sourceSets {
    3. val commonMain by getting {
    4. dependencies {
    5. implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
    6. }
    7. }
    8. }
    9. }
  3. Synchronize the Gradle files by clicking Sync Now in the notification.

    Synchronize the Gradle files

  4. In shared/src/commonMain/kotlin, create a new file NewYear.kt in the project folder.

  5. 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:

    1. import kotlinx.datetime.*
    2. fun daysUntilNewYear(): Int {
    3. val today = Clock.System.todayIn(TimeZone.currentSystemDefault())
    4. val closestNewYear = LocalDate(today.year + 1, 1, 1)
    5. return today.daysUntil(closestNewYear)
    6. }
  6. In Greeting.kt, update the greet() function to see the result:

    1. class Greeting {
    2. private val platform: Platform = getPlatform()
    3. fun greet(): List<String> = buildList {
    4. add(if (Random.nextBoolean()) "Hi!" else "Hello!")
    5. add("Guess what it is! > ${platform.name.reversed()}!")
    6. add("\nThere are only ${daysUntilNewYear()} days left until New Year! 🎆")
    7. }
    8. }
  7. To see the results, re-run your androidApp and iosApp configurations from Android Studio:

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