科特林多平台。无法访问 class SqlDriver.Schema。检查您的模块 classpath 是否缺少或冲突的依赖项

Kotlin Multiplatform. Cannot access class SqlDriver.Schema. Check your module classpath for missing or conflicting dependencies

我正在尝试构建针对 iOS、Android、JS(浏览器)、Mac、Windows 和 Linux 的 KMP 库。现在我只使用 Ktor 和 SQLDelight 作为依赖。但是在为 SQLDelight

创建驱动程序时,在 nativeMain 的实际实现中出现以下问题

虽然相同的代码不会给 iOS main 带来任何问题,它也使用相同的 NativeSqliteDriver(我需要单独使用它们,因为 iOS 和桌面平台的 Ktor 客户端是分开的)。 以下是我的 build.gradle.kts

plugins {
    kotlin("multiplatform") version "1.5.31"
    id("maven-publish")
    id("com.android.library")
    kotlin("plugin.serialization") version "1.5.31"
    id("com.squareup.sqldelight") version "1.5.3"
}

group = "me.group"
version = "1.0-SNAPSHOT"

val xcFrameworkName = "AddressLib"

repositories {
    google()
    mavenCentral()
}

kotlin {
    jvm {
        compilations.all {
            kotlinOptions.jvmTarget = "1.8"
        }
        testRuns["test"].executionTask.configure {
            useJUnit()
        }
    }
    js(LEGACY) {
        browser {
            commonWebpackConfig {
                cssSupport.enabled = true
            }
        }
    }

    val xcFramework = XCFramework(xcFrameworkName)

    val hostOs = System.getProperty("os.name")
    val isMingwX64 = hostOs.startsWith("Windows")
    when {
        hostOs == "Mac OS X" -> macosX64("native") {
            binaries.framework(xcFrameworkName) {
                xcFramework.add(this)
            }
        }
        hostOs == "Linux" -> linuxX64("native")
        isMingwX64 -> mingwX64("native")
        else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
    }

    android()

    ios {
        binaries.framework(xcFrameworkName) {
            xcFramework.add(this)
        }
    }

    val coroutinesVersion = "1.5.2-native-mt"
    val serializationVersion = "1.3.1"
    val ktorVersion = "1.6.5"
    val sqlDelightVersion = "1.5.3"
    val napierVersion = "2.2.0"
    val koinVersion = "3.1.4"

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")

                implementation("io.ktor:ktor-client-core:$ktorVersion")
                implementation("io.ktor:ktor-client-serialization:$ktorVersion")
                implementation("io.ktor:ktor-client-logging:$ktorVersion")

                implementation("com.squareup.sqldelight:runtime:$sqlDelightVersion")
                implementation("io.insert-koin:koin-core:$koinVersion")

                implementation("io.github.aakira:napier:$napierVersion")
            }
        }
        val commonTest by getting
        val jvmMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-java:$ktorVersion")
                implementation("com.squareup.sqldelight:sqlite-driver:$sqlDelightVersion")
            }
        }
        val jvmTest by getting
        val jsMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-js:$ktorVersion")
                implementation("com.squareup.sqldelight:sqljs-driver:$sqlDelightVersion")
            }
        }
        val jsTest by getting
        val nativeMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-curl:$ktorVersion")
                implementation("com.squareup.sqldelight:native-driver:$sqlDelightVersion")
            }
        }
        val nativeTest by getting
        val androidMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-android:$ktorVersion")
                implementation("com.squareup.sqldelight:android-driver:$sqlDelightVersion")
            }
        }
        val androidTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.13.2")
            }
        }
        val iosMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-ios:$ktorVersion")
                implementation("com.squareup.sqldelight:native-driver:$sqlDelightVersion")
            }
        }
        val iosTest by getting
    }

    sqldelight {
        database("AddressDatabase") {
            packageName = "com.library.address.database"
        }
    }
}

android {
    compileSdkVersion(31)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(24)
        targetSdkVersion(31)
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

publishing {
    repositories {
        maven {
            credentials {
                username = "<username>"
                password = "<pwd>"
            }
            url = URI("https://mymavenrepo.com")
        }
    }
}

所以问题似乎是由于相同的依赖项被添加到构建 gradle 两次,并且相应的代码也被添加了两次。为了解决这个问题,我必须制作一个单独的源集,如下所示

val sqlDriverNativeMain by creating {
    dependsOn(commonMain)
    dependencies {
        implementation("com.squareup.sqldelight:native-driver:$sqlDelightVersion")
    }
}
val iosMain by getting {
    dependsOn(sqlDriverNativeMain)
    dependencies {
        implementation("io.ktor:ktor-client-ios:$ktorVersion")
    }
}
val nativeMain by getting {
   dependsOn(sqlDriverNativeMain)
   dependencies {
       implementation("io.ktor:ktor-client-curl:$ktorVersion")
    }
}

然后将驱动程序创建代码移动到名为 sqlDriverNativeMain 的 sourceSet 目录中。这解决了问题。