kotlin - SQLDelight:Kotlin Multiplatform App 中 Unresolved reference AndroidSqliteDriver

标签 kotlin kotlin-multiplatform sqldelight

我尝试将 sqldelight 添加到我的 Kotlin 多平台应用程序中,但 AndroidSqliteDriver 未解析引用。我不明白为什么。
我清除缓存并重新生成所有内容,但它也不起作用。
AndroidSqlite驱动程序:

package com.rompos.deactivator

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

lateinit var appContext: Context

actual fun createDB() : Server {
val driver = AndroidSqliteDriver(Server.Schema, appContext, "Server.db")
return Server(driver)
}
build.gradle(通用):
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
kotlin("multiplatform")
id("com.squareup.sqldelight")
}

kotlin {
//select iOS target platform depending on the Xcode environment variables
val iOSTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
    if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
        ::iosArm64
    else
        ::iosX64

iOSTarget("ios") {
    binaries {
        framework {
            baseName = "SharedCode"
        }
    }
}

jvm("android")

// Common Main
sourceSets["commonMain"].dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
    implementation("io.ktor:ktor-client-core:1.3.2")
    implementation("com.squareup.sqldelight:runtime:1.4.0")
}

// Android Main
sourceSets["androidMain"].dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib")
    implementation("com.squareup.sqldelight:android-driver:1.4.0") {
        exclude(group = "com.squareup.sqldelight", module = "runtime-jdk")
    }
}

// iOS Main
sourceSets["iosMain"].dependencies {
    implementation("com.squareup.sqldelight:native-driver:1.4.0")
}
}
构建.gradle(安卓):
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlinx-serialization'
apply plugin: 'com.squareup.sqldelight'

android {
compileSdkVersion 30
buildToolsVersion "30.0.1"
defaultConfig {
    applicationId "com.rompos.deactivator"
    minSdkVersion 26
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner 
"androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android- 
optimize.txt'), 'proguard-rules.pro'
    }
}
packagingOptions {
    exclude 'META-INF/*.kotlin_module'
}

buildFeatures {
    dataBinding true
}
}

dependencies {
implementation project(':SharedCode')
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "androidx.core:core-ktx:1.3.1"
implementation "androidx.appcompat:appcompat:1.2.0"
implementation "com.google.android.material:material:1.2.0"
implementation "androidx.constraintlayout:constraintlayout:1.1.3"
implementation 
"androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
implementation "androidx.navigation:navigation-fragment-ktx:2.3.0"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines- 
android:1.3.7"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"
implementation "org.jetbrains.kotlinx:kotlinx-serialization- 
runtime:0.20.0"
implementation "io.ktor:ktor-client-android:1.3.2"
implementation "com.squareup.sqldelight:android- 
driver:$sqldelight_version"

testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso- 
core:3.2.0'
}
build.gradle(项目):
buildscript {
ext {
    ext.kotlin_version = '1.3.72'
    sqldelight_version = "1.4.0"
}
repositories {
    google()
    jcenter()
    mavenCentral()
}
dependencies {
    classpath 'com.android.tools.build:gradle:4.0.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle- 
    plugin:$kotlin_version"
    classpath "org.jetbrains.kotlin:kotlin- 
    serialization:$kotlin_version"
    classpath "com.squareup.sqldelight:gradle- 
    plugin:$sqldelight_version"
}
}

allprojects {
repositories {
    google()
    jcenter()
    maven { url "https://kotlin.bintray.com/kotlinx" }
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}
我用:
等级:6.1.1
毕业插件:4.0.1
Kotlin :1.3.72
更新:添加代码

最佳答案

你只有一个 jvm 目标 jvm("android")而不是 android 目标。您需要一个 android 库或应用程序目标才能使用 android 库。
编辑 : 添加一个 android 目标:

//build.gradle(common)
plugins {
    ...
    // assuming your building a library. use application otherwise
    id("com.android.library") 
    id("kotlin-android")
}
...
android {
     // fill in your library or app info here
}

kotlin {
    android { // creates androidMain/Test source sets
        // ex: publishLibraryVariants("release", "debug")
    }

    ios() { ... } 

    val commonMain by getting { ... }
    val androidMain by getting { ... }
    ...
}

关于kotlin - SQLDelight:Kotlin Multiplatform App 中 Unresolved reference AndroidSqliteDriver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63448785/

相关文章:

android - RxJava 如何缓存静态可观察对象

android - Kotlin 轻松访问 Activity 之外的资源

gradle - 如何为 kotlin-multiplatform (kotlin 1.3.50) 添加对 build.gradle.kts 的依赖项?

android - 如何迭代 SqlDelight 选择结果而不将所有内容加载到内存中?

android - 使用 sqldelight 为 getter 和 setter 添加前缀

kotlin - 在 Kotlin 中打印 0001 到 1000。如何为数字添加填充?

android - 创建 Parcelable 的数据类

unit-testing - 无法从仅 jvm 模块访问 commonMain 多平台类

swift - Kotlin-Swift 互操作问题 : ArrayList has wrong count value when passed as NSMutableArray from Swift Code for Release builds

kotlin - ALTER TABLE 迁移不会更新生成的代码