【问题标题】:Kotlin Multiplatform. Cannot access class SqlDriver.Schema. Check your module classpath for missing or conflicting dependenciesKotlin 多平台。无法访问类 SqlDriver.Schema。检查您的模块类路径是否存在缺失或冲突的依赖项
【发布时间】:2021-11-29 08:30:17
【问题描述】:

我正在尝试构建一个针对 iOS、Android、JS(浏览器)、Mac、Windows 和 Linux 的 KMP 库。现在我只使用 Ktor 和 SQLDelight 作为依赖项。但是在为 SQLDelight 创建驱动程序时,在 nativeMain 的实际实现中遇到以下问题

虽然相同的代码对于使用相同的NativeSqliteDriver 的iOS main 没有任何问题(我需要它们分开,因为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")
        }
    }
}

【问题讨论】:

  • 你有什么发现吗?我看到了同样的事情。
  • 是的,请检查答案

标签: kotlin kotlin-multiplatform kotlin-native


【解决方案1】:

因此,问题似乎在某种程度上是由于两次将相同的依赖项添加到构建 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 目录中。这解决了问题。

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    相关资源
    最近更新 更多