1

我尝试将 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
}

我使用: gradle: 6.1.1 gradle plugin: 4.0.1 kotlin: 1.3.72 upd: 添加代码

4

2 回答 2

1

你只有一个 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 { ... }
    ...
}
于 2021-01-05T05:51:41.043 回答
-1

确保您至少使用Android Studio 4.1。您应该只需要在您的公共 build.gradle 中包含 SQLDelight 插件。

构建.gradle

...
apply plugin: "com.squareup.sqldelight" in the common build.gradle
...

sourceSets {
    commonMain.dependencies {
        ...
        // SQL Delight
        implementation "com.squareup.sqldelight:runtime:${Versions.sqlDelight}"
        implementation "com.squareup.sqldelight:coroutines-extensions:${Versions.sqlDelight}"
    }

    androidMain.dependencies {
        ...
        // SQL Delight
        implementation "com.squareup.sqldelight:android-driver:${Versions.sqlDelight}"
        implementation "com.squareup.sqldelight:coroutines-extensions-jvm:${Versions.sqlDelight}"
    }

    iOSMain.dependencies {
        ...
        // SQL Delight
        implementation "com.squareup.sqldelight:ios-driver:${Versions.sqlDelight}"
    }
}

于 2020-08-24T18:04:11.853 回答