【问题标题】:How do I add and use Android libraries in a Kotlin Multiplatform Mobile project?如何在 Kotlin Multiplatform Mobile 项目中添加和使用 Android 库?
【发布时间】:2022-11-23 01:46:05
【问题描述】:

感觉这非常简单,但我是 Kotlin 和 KMM 的新手><

基本上,我试图在我使用 Android Studio 中的 KMM 插件设置的 KMM 项目中使用 this library

我共享的 build.gradle.kts 文件如下所示:`

plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    id("com.android.library")
}

kotlin {
    android()
    iosX64()
    iosArm64()
    iosSimulatorArm64()

    cocoapods {
        summary = "Some description for the Shared Module"
        homepage = "Link to the Shared Module homepage"
        version = "1.0"
        ios.deploymentTarget = "14.1"
        podfile = project.file("../iosApp/Podfile")
        framework {
            baseName = "shared"
        }
    }
    
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        val androidMain by getting {
            dependencies {
                implementation("com.kizitonwose.calendar:compose:2.0.3")
            }
        }
        val androidTest by getting
        val iosX64Main by getting
        val iosArm64Main by getting
        val iosSimulatorArm64Main by getting
        val iosMain by creating {
            dependsOn(commonMain)
            iosX64Main.dependsOn(this)
            iosArm64Main.dependsOn(this)
            iosSimulatorArm64Main.dependsOn(this)
        }
        val iosX64Test by getting
        val iosArm64Test by getting
        val iosSimulatorArm64Test by getting
        val iosTest by creating {
            dependsOn(commonTest)
            iosX64Test.dependsOn(this)
            iosArm64Test.dependsOn(this)
            iosSimulatorArm64Test.dependsOn(this)
        }
    }
}

android {
    namespace = "com.example.pilld"
    compileSdk = 32
    defaultConfig {
        minSdk = 21
        targetSdk = 32
        multiDexEnabled = true
    }

    compileOptions {
        isCoreLibraryDesugaringEnabled = true
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    dependencies {
        implementation("com.kizitonwose.calendar:compose:2.0.3")
    }
}

dependencies {
    coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.2.0")
}

** App build.gradle.kts (I believe this is called top-level?):**

buildscript {
    dependencies {
        classpath("com.android.tools.build:gradle:7.3.0")
    }
}
plugins {
    //trick: for the same plugin versions in all sub-modules
    id("com.android.application").version("7.3.1").apply(false)
    id("com.android.library").version("7.3.1").apply(false)
    kotlin("android").version("1.7.10").apply(false)
    kotlin("multiplatform").version("1.7.10").apply(false)
    id("org.jetbrains.compose").version("1.2.1")
}

tasks.register("clean", Delete::class) {
    delete(rootProject.buildDir)
}

** And finally the shared/commonMain/Greeting.kt file in which I am trying to use the library:**

package com.example.pilld

//import com.kizitonwose.calendar.*

class Greeting {
    private val platform: Platform = getPlatform()

    fun greeting(): String {
        return "Hello, ${platform.name}!"
    }
}

`

还没有尝试太多,只是在做设置。我试过在 Greeting.kt 文件中导入,但它无法识别该库。我是否也应该将它作为依赖项添加到 commonMain sourceSet 中?我也一直在阅读文档,但到处都是,我无法得到直接的答案。任何意见,将不胜感激!

【问题讨论】:

    标签: kotlin kotlin-multiplatform kotlin-android-extensions


    【解决方案1】:

    你提到的库应该在androidApp模块中。您需要在androidApp/build.gradle.kts 文件中添加此依赖项。并在您的 Android 表示层代码中使用它(位于您的androidApp/ 文件夹中)

    您不能在 shared -> commonMain 模块中使用特定于平台的库。你有两个选择:

    1. 如果你想在共享代码中使用库,你需要在 commonMain 中为你的平台特定代码(类、方法、接口)创建通用 api,并用特殊的 expect 关键字标记它。然后在androidMainiosMain中提供平台相关的实现,并用actual关键字see进行标记

    2. 直接在你的androidApp模块中使用库,而不是在共享代码中

    【讨论】:

      猜你喜欢
      • 2021-08-13
      • 2021-02-01
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      相关资源
      最近更新 更多