android 未生成Sqldelight数据库架构

iibxawm4  于 2023-01-11  发布在  Android
关注(0)|答案(3)|浏览(136)

我有一个KMM项目,并希望使用SqlDelight库,但当我构建项目时,未生成数据库模式和表实体。

actual class DatabaseDriverFactory(private val context: Context) {
    actual fun createDriver(): SqlDriver {
               //Unresolved reference: CoreDb
        return AndroidSqliteDriver(CoreDb.Schema, context, "test.db")
    }
}

我已经在共享模块中定义了sqldelight文件夹,还为功能生成的kotlin类创建了文件夹,因为它在gradle.build.kts中配置,并且在sqldelight文件夹中还有一个*.sq文件

sqldelight {
  database("CoreDb") {
    packageName = "com.example.app.core.database"
    sourceFolders = listOf("sqldelight")
    dialect = "sqlite:3.24"
  }
}

当我运行generateSqlDelightInterface任务时,我只看到这些日志

> Task :core:generateAndroidDebugCoreDbInterface NO-SOURCE
> Task :core:generateAndroidReleaseCoreDbInterface NO-SOURCE
> Task :core:generateIosMainCoreDbInterface NO-SOURCE
> Task :core:generateMetadataCommonMainCoreDbInterface NO-SOURCE
> Task :core:generateMetadataMainCoreDbInterface NO-SOURCE
> Task :core:generateSqlDelightInterface UP-TO-DATE
can't register checkAndroidModules

BUILD SUCCESSFUL in 311ms
1:40:36 PM: Task execution finished 'generateSqlDelightInterface'.

下面是我的完整build.gradle.kts

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

plugins {
  kotlin("multiplatform")
  kotlin("plugin.serialization")
  id("com.android.library")
  id("kotlin-android-extensions")
  id("koin")
  id("com.squareup.sqldelight")
}

repositories {
  gradlePluginPortal()
  google()
  jcenter()
  mavenCentral()
  maven {
    url = uri("https://dl.bintray.com/kotlin/kotlin-eap")
  }
  maven {
    url = uri("https://dl.bintray.com/ekito/koin")
  }
}
kotlin {
  android()

  val iOSTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
    if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
      ::iosArm64
    else
      ::iosX64

  iOSTarget("ios") {
    binaries {
      framework {
        baseName = "core"
      }
    }
  }
  val coroutinesVersion = "1.3.9-native-mt"
  val ktor_version = "1.4.2"
  val serializationVersion = "1.0.0-RC"
  val koin_version = "3.0.0-alpha-4"
  val sqlDelight = "1.4.4"

  sourceSets {

    val commonMain by getting {
      dependencies {
        implementation("io.ktor:ktor-client-core:$ktor_version")
        implementation("io.ktor:ktor-client-serialization:$ktor_version")

        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
        implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")

        implementation("com.squareup.sqldelight:runtime:$sqlDelight")
        // SqlDelight extension
        implementation("com.squareup.sqldelight:coroutines-extensions:$sqlDelight")
        // Koin for Kotlin
        implementation("org.koin:koin-core:$koin_version")
        //shared preferences
        implementation("com.russhwolf:multiplatform-settings:0.6.3")
      }
    }
    val commonTest by getting {
      dependencies {
        implementation(kotlin("test-common"))
        implementation(kotlin("test-annotations-common"))
      }
    }
    val androidMain by getting {
      dependencies {
        implementation("androidx.core:core-ktx:1.3.2")
        implementation("io.ktor:ktor-client-android:$ktor_version")
        implementation("com.squareup.sqldelight:android-driver:$sqlDelight")
      }
    }
    val androidTest by getting {
      dependencies {
        implementation(kotlin("test-junit"))
        implementation("junit:junit:4.12")
      }
    }
    val iosMain by getting {
      dependencies {
        implementation("io.ktor:ktor-client-ios:$ktor_version")
        implementation("com.squareup.sqldelight:native-driver:$sqlDelight")
      }
    }

  
    val iosTest by getting
  }
}


android {
  compileSdkVersion(29)
  sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
  defaultConfig {
    minSdkVersion(23)
    targetSdkVersion(29)
    versionCode = 1
    versionName = "1.0"
  }
  buildTypes {
    getByName("release") {
      isMinifyEnabled = false
    }
  }
}
val packForXcode by tasks.creating(Sync::class) {
  group = "build"
  val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
  val sdkName = System.getenv("SDK_NAME") ?: "iphonesimulator"
  val targetName = "ios" + if (sdkName.startsWith("iphoneos")) "Arm64" else "X64"
  val framework =
    kotlin.targets.getByName<KotlinNativeTarget>("ios").binaries.getFramework(mode)
  inputs.property("mode", mode)
  dependsOn(framework.linkTask)
  val targetDir = File(buildDir, "xcode-frameworks")
  from({ framework.outputDirectory })
  into(targetDir)
}
tasks.getByName("build").dependsOn(packForXcode)

sqldelight {
  database("CoreDb") {
    packageName = "com.example.app.core.database"
    sourceFolders = listOf("sqldelight")
    dialect = "sqlite:3.24"
  }
}

并且对于顶层build.gradle
classpath "com.squareup.sqldelight:gradle-plugin:$sqlDelight"

    • 更新**

我的项目文件夹结构

root
  app
    src
      ...

  core //(kmm shared module)
    androidMain
        com.example.app.core
            database
    commonMain
        com.example.app.core
            database
            repository
            ...
            sqldelight
    iosMain
        com.example.app.core
            database
cgyqldqp

cgyqldqp1#

看起来你把你的sqldelight文件夹,你把你的*.sq文件,在错误的位置。
它应该直接放在commonMain中,而在sqldelight中,您必须指定您的包。
如果您的软件包是com.example.kmmtest003.database,则文件夹结构应为:

- commonMain
    - sqldelight
      - com
        - example
          - kmmtest003
            - database

在这里你必须把你的*.sq文件。

其他可能原因

IgorGanapolsky指出,不兼容的gradle插件版本也可能导致同样的问题。

zzwlnbp8

zzwlnbp82#

这可能会在将来对某些人有所帮助...我在KMM中创建了一个目录,并将其命名为sqldelight.com.package.database
我认为这是一个合适的包,但是意识到SQLDelight没有捕获到这个包,因此没有生成我的模式。
我需要创建一个结构作为一个适当的目录结构(然后一切工作):

sqldelight
  com
    package
      database
stszievb

stszievb3#

或者,如果希望SQL代码与您的代码位于同一目录中,则可以将文件夹更改为Kotlin:

sqldelight {
  database("CoreDb") {
    ...
    sourceFolders = listOf("kotlin")
    ...
  }
}

相关问题