首页 > 解决方案 > 共享模块中的 KMM 依赖问题

问题描述

我们正在使用 KMM 创建一个应用程序。但是我们面临一个问题,kSoap2 库在 android 应用程序中工作正常,但是当这个库在 Android Studio 的共享目录中使用时,它不起作用并给出错误。同样的事情也适用于日历。它也不起作用,在编写 Calendar.getInstance() 时,错误说

无法访问“java.util.Calendar”的超类型“Kotlin.Cloneable”。检查您的模块类路径是否存在缺失或冲突的依赖项

build.gradle.kts(for Shared)

    import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
    
     
    
    plugins {
    
        kotlin("multiplatform")
    
        id("com.android.library")
    
    }
    
    repositories {
    
        google()
    
        mavenCentral()
    
    }

 

kotlin {

    android()

 

    val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =

        if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)

            ::iosArm64

        else

            ::iosX64

 

    iosTarget("ios") {

        binaries {

            framework {

                baseName = "shared"

            }

        }

    }

 

    sourceSets {

        val commonMain by getting {

            dependencies {

//                implementation("com.facebook.android:facebook-android-sdk:[5,6)")

//                implementation("com.google.android.material:material:1.3.0")

//                implementation("com.google.firebase:firebase-messaging:10.2.1")

//                implementation("com.google.android.gms:play-services:10.2.1")

//                implementation("com.google.android.gms:play-services-ads:10.2.1")

//                implementation("com.google.firebase:firebase-core:10.2.1")

//                implementation("com.google.firebase:firebase-auth:10.2.1")

//                implementation("com.github.siyamed:android-shape-imageview:0.9.+@AndreiA")

//                implementation("com.github.barteksc:android-pdf-viewer:3.1.0-beta.1")

//                implementation("org.apache.commons:commons-io:1.3.2")

 

 

                implementation("com.google.android.material:material:1.3.0")

                implementation("androidx.appcompat:appcompat:1.2.0")

                implementation("androidx.legacy:legacy-support-v13:1.0.0")

                implementation("androidx.core:core:1.0.0")

                implementation("androidx.legacy:legacy-support-v4:1.0.0")

                implementation("androidx.constraintlayout:constraintlayout:1.1.3")

                implementation("com.android.support.constraint:constraint-layout:")

                implementation("com.google.firebase:firebase-messaging:10.2.1")

                implementation("com.google.android.gms:play-services:10.2.1")

                implementation("com.google.android.gms:play-services-ads:10.2.1")

                implementation("com.google.firebase:firebase-core:10.2.1")

                implementation("com.google.firebase:firebase-auth:10.2.1")

                implementation("com.github.siyamed:android-shape-imageview:0.9.+@AndreiA")

                implementation("com.facebook.android:facebook-android-sdk:[5,6)")

                implementation("androidx.mediarouter:mediarouter:1.0.0")

                implementation("androidx.vectordrawable:vectordrawable:1.0.0")

                implementation("com.github.barteksc:android-pdf-viewer:3.1.0-beta.1")

                implementation("org.apache.commons:commons-io:1.3.2")

                implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.30")

            }

        }

 

        val commonTest by getting {

            dependencies {

                implementation(kotlin("test-common"))

                implementation(kotlin("test-annotations-common"))

            }

        }

        val androidMain by getting

        val androidTest by getting {

            dependencies {

 

                implementation(kotlin("test-junit"))

                implementation("junit:junit:4.13.2")

            }

        }

        val iosMain by getting

        val iosTest by getting

 

    }

}

 

apply { plugin("com.google.gms.google-services") }

 

android {

    compileSdkVersion(30)

    sourceSets["main"].manifest.srcFile("src//androidMain//AndroidManifest.xml")

 

    defaultConfig {

        minSdkVersion(17)

        targetSdkVersion(30)

    }

 

}

 

val packForXcode by tasks.creating(Sync::class) {

    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"

    val framework = kotlin.targets.getByName<KotlinNativeTarget>("ios").binaries.getFramework(mode)

    val targetDir = File(buildDir, "xcode-frameworks")

 

    group = "build"

    dependsOn(framework.linkTask)

    inputs.property("mode", mode)

 

    from({ framework.outputDirectory })

    into(targetDir)

}

 

tasks.getByName("build").dependsOn(packForXcode)

dependencies {

    implementation(files("/Users/mbp/AndroidStudioProjects/PashtoLibrary/androidApp/libs/ksoap2-android-assembly-3.3.0-jar-with-dependencies.jar"))

    implementation(files("/Users/mbp/AndroidStudioProjects/PashtoLibrary/androidApp/libs/org.apache.commons.net_2.0.0.v200905272248.jar"))

}

build.gradle.kts(适用于 AndroidApp):

import org.jetbrains.kotlin.gradle.targets.js.npm.includedRange

 

plugins {

    id("com.android.application")

    kotlin("android")

 

}

repositories {

    google()

    jcenter()

}

dependencies {

    /*implementation(project(":shared"))

    implementation("com.google.android.material:material:1.3.0")

    implementation("androidx.appcompat:appcompat:1.2.0")

    implementation("androidx.constraintlayout:constraintlayout:2.0.4")*/

    implementation(project(":shared"))

 

    implementation("androidx.multidex:multidex:2.0.0")

 

    implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))

    implementation(files("libs/ksoap2-android-assembly-3.3.0-jar-with-dependencies.jar"))

    implementation(files("libs/pinchzoom.jar"))

    implementation(files("libs/org.apache.commons.net_2.0.0.v200905272248.jar"))

    implementation(files("libs/StartAppInApp-3.4.2.jar"))

    implementation(files("libs/picasso-2.5.2.jar"))

//    implementation("com.google.android.material:material:1.3.0")

//    implementation("androidx.appcompat:appcompat:1.2.0")

//

//    implementation("androidx.appcompat:appcompat:1.0.0")

//    implementation("androidx.legacy:legacy-support-v13:1.0.0")

//    implementation("androidx.core:core:1.0.0")

//    implementation("androidx.legacy:legacy-support-v4:1.0.0")

//

//

//    implementation("androidx.constraintlayout:constraintlayout:1.1.3")

//    implementation("com.android.support.constraint:constraint-layout:")

//    implementation("com.google.firebase:firebase-messaging:10.2.1")

//    implementation("com.google.android.gms:play-services:10.2.1")

//    implementation("com.google.android.gms:play-services-ads:10.2.1")

//    implementation("com.google.firebase:firebase-core:10.2.1")

//    implementation("com.google.firebase:firebase-auth:10.2.1")

//    implementation("com.github.siyamed:android-shape-imageview:0.9.+@AndreiA")

//    implementation("com.facebook.android:facebook-android-sdk:[5,6)")

//    implementation("androidx.mediarouter:mediarouter:1.0.0")

//    implementation("androidx.vectordrawable:vectordrawable:1.0.0")

//    implementation("com.github.barteksc:android-pdf-viewer:3.1.0-beta.1")

//    implementation("org.apache.commons:commons-io:1.3.2")

//

//

//    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.30")

 

}

apply { plugin("com.google.gms.google-services") }

 

android {

    compileSdkVersion(30)

    defaultConfig {

        applicationId = "co.beyondsolutions.libraryapp"

        minSdkVersion(17)

        targetSdkVersion(30)

        versionCode = 1

        versionName = "1.0"

 

        multiDexEnabled = true

    }

    buildTypes {

        getByName("release") {

            isMinifyEnabled = false

        }

    }

}

标签: kotlinbuild.gradlekotlin-multiplatformkotlin-nativekmm

解决方案


你所有的 android 依赖项都应该androidMaincommonMain.

作为一般规则,您只能拥有可以编译到所有目标的依赖项。由于您的 android 依赖项位于公共源集之下,因此它并不真正知道如何将它们编译到 iOS

这是使用 Android Arch 视图模型的示例:https ://github.com/halcyonmobile/MultiplatformPlayground/blob/master/common/src/androidMain/kotlin/com/halcyonmobile/multiplatformplayground/shared/CoroutineViewModel.kt


推荐阅读