SQLDelight:Kotlin Multiplatform App 中未解决的参考 AndroidSqliteDriver
Posted
技术标签:
【中文标题】SQLDelight:Kotlin Multiplatform App 中未解决的参考 AndroidSqliteDriver【英文标题】:SQLDelight: Unresolved reference AndroidSqliteDriver in Kotlin Multiplatform App 【发布时间】:2020-12-06 11:13:31 【问题描述】:我尝试将 sqldelight 添加到我的 Kotlin 多平台应用程序,但 androidSqliteDriver 未解析引用。我不明白为什么。 我清除缓存并重新生成所有内容,但它也不起作用。
AndroidSqliteDriver:
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(common):
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")
build.gradle(android):
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 科特林:1.3.72 更新:添加代码
【问题讨论】:
请添加代码图像不起作用! 我添加代码,img 不起作用。 【参考方案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 ...
...
【讨论】:
我真的很愚蠢,因为我看不到我在哪里指定jvm(android)
并且我不知道在哪里或如何指定 Android 目标。有人可以发布一个例子。我有一份 kmm-networking-and-data-storage-final
示例的副本,由于同样的原因,该示例不再编译。【参考方案2】:
确保您至少使用 Android Studio 4.1。 你应该只需要在你的公共 build.gradle 中包含 SQLDelight 插件。
build.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"
【讨论】:
以上是关于SQLDelight:Kotlin Multiplatform App 中未解决的参考 AndroidSqliteDriver的主要内容,如果未能解决你的问题,请参考以下文章
将 sqldelight 的“未解决的参考:NativeSqliteDriver”添加到 ios 源集
无法解析 com.squareup.sqldelight:native-driver:1.3.0