如何在Kotlin多平台移动的项目中添加和使用Android库?

kokeuurv  于 2022-11-25  发布在  Kotlin
关注(0)|答案(1)|浏览(197)

感觉这是非常直接的,但我是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源集中?我也一直在阅读文档,但是它有点混乱,我不能得到一个直接的答案。任何建议都将不胜感激!

ia2d9nvy

ia2d9nvy1#

您提到的库应该在androidApp模块中。您需要在androidApp/build.gradle.kts文件中添加此依赖项。并在您的Android表示层代码中使用它(位于androidApp/文件夹中)
你不能在shared-〉commonMain模块中使用特定于平台的库。
1.如果你想在共享代码中使用库,你需要在commonMain中为你的特定于平台的代码(类、方法、接口)创建公共api,并用特殊的expect关键字标记它。然后在androidMainiosMain中提供特定于平台的实现,并用actual关键字标记它们。请看official documentation
1.直接在androidApp模块中使用库,而不是在共享代码中使用库

相关问题