0

我正在尝试在适用于 android 和 iOS 的 Kotlin Multiplatform 项目中使用 SQLDelight。我已按照此处找到的文档进行操作。该项目成功构建,但我无法访问 com.squareup.* shared/iosMain 文件夹中的任何位置。下面是我的代码文件。

项目/build.gradle.kts:

buildscript {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31")
        classpath("com.android.tools.build:gradle:7.0.3")
        classpath("com.squareup.sqldelight:gradle-plugin:1.5.2")

    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

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

项目/共享/build.gradle.kts:

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

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

}

version = "1.0"

kotlin {
    android()

    val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget = when {
        System.getenv("SDK_NAME")?.startsWith("iphoneos") == true -> ::iosArm64
        System.getenv("NATIVE_ARCH")?.startsWith("arm") == true -> ::iosSimulatorArm64
        else -> ::iosX64
    }

    iosTarget("ios") {}

    cocoapods {
        summary = "Some description for the Shared Module"
        homepage = "Link to the Shared Module homepage"
        ios.deploymentTarget = "14.1"
        frameworkName = "shared"
        podfile = project.file("../iosApp/Podfile")
    }
    
    sourceSets {
        val commonMain by getting {
            dependencies {
                // SQLDelight
                implementation("com.squareup.sqldelight:runtime:1.5.2")
                implementation ("com.squareup.sqldelight:coroutines-extensions:1.5.0")
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val androidMain by getting {
            dependencies {
                implementation("com.squareup.sqldelight:android-driver:1.5.0")

            }
        }
        val androidTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.13.2")
            }
        }
        val iosMain by getting {
            dependencies {
                implementation("com.squareup.sqldelight:native-driver:1.5.2")
            }
        }
        val iosTest by getting
    }
}

sqldelight {
    database("AppDatabase") {
        packageName = "com.example.sam.data.db"
        dialect = "sqlite:3.25"
    }
}

android {
    compileSdkVersion(31)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(21)
        targetSdkVersion(31)
    }
}

项目/shared/src/commonMain/kotlin/com/example/sam/data/db/DatabaseDriverFactory.kt

package com.example.sam.data.db

import com.squareup.sqldelight.db.SqlDriver

expect class DatabaseDriverFactory {
    fun createDriver(): SqlDriver
}

项目/shared/src/androidMain/kotlin/com/example/sam/data/db/DatabaseDriverFactory.kt

package com.example.sam.data.db

import android.content.Context
import com.squareup.sqldelight.android.AndroidSqliteDriver
import com.squareup.sqldelight.db.SqlDriver

actual class DatabaseDriverFactory(
    private val ctx: Context
) {

    actual fun createDriver(): SqlDriver {
        return AndroidSqliteDriver(AppDatabase.Schema, ctx, "app.db")
    }

}

我遇到问题的文件 - Project/shared/iosMain/kotlin/com/example/sam/data/db/DatabaseDriverFactory.kt

package com.example.sam.data.db

import com.squareup.sqldelight.db.SqlDriver
import com.squareup.sqldelight.drivers.native.NativeSqliteDriver


actual class DatabaseDriverFactory {

    actual fun createDriver(): SqlDriver {
        return NativeSqliteDriver(AppDatabase.Schema, "app.db")
    }

}

它找不到 SqlDriver、NativeSqliteDriver 甚至 com.squareup.*。

我已经搜索了网络,但没有找到任何帮助。

4

1 回答 1

0

评论不会让我发布代码,但我想尝试一些事情。

试试这个配置,看看 IDE 是否能更好地理解它。您仍然需要添加某种形式的更复杂的形式,但我想尽可能减少变量。

kotlin {
    android()

    iosX64("ios")

    cocoapods {
    //etc...
于 2021-11-07T15:48:35.150 回答