【发布时间】:2021-07-03 20:11:34
【问题描述】:
我试图在我的 KMM 应用程序中使用 kotlinx.serialization 来解析来自网站的 json Http 响应。我尝试了许多在网上找到的解决方案,但都没有解决我的问题。我必须更新到 Android Studio/Kotlin 版本 1.5.20 以消除 Kotlin-datetime 模块中的错误。知道面临新的编译器错误。有人可以帮忙吗?
Kotlin 类:
import com.pagetyler.dailynasa.shared.entity.Defaults
import io.ktor.client.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*
import io.ktor.client.request.*
class NasaPicsApi {
private val httpClient = HttpClient {
install(JsonFeature) {
val json = kotlinx.serialization.json.Json { ignoreUnknownKeys = true }
serializer = KotlinxSerializer(json)
}
}
suspend fun getPicture(currDate:String): List<NasaPicture> {
val ep = "$PICTURES_ENDPOINT$EXTENSION?api_key=$DEFAULT_APIKEY;date=$currDate "
return httpClient.get(ep)
}
companion object {
private val PICTURES_ENDPOINT = Defaults().PICTURES_ENDPOINT
private val EXTENSION = Defaults().EXTENSION
private val DEFAULT_APIKEY = Defaults().DEFAULT_APIKEY
}
}
通用/共享 build.gradle.kts:
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
val kotlinVersion = "${rootProject.extra["kotlin_version"]}"
plugins {
kotlin("multiplatform")
kotlin("plugin.serialization") version "1.5.20"
id("com.android.library")
id("com.squareup.sqldelight")
}
group = "com.pagetyler"
version = "1.0-SNAPSHOT"
repositories {
gradlePluginPortal()
google()
jcenter()
mavenCentral()
maven(url = "https://kotlin.bintray.com/kotlinx/") // soon will be just jcenter()
}
kotlin {
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
useIR = true
}
}
val iOSTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
::iosArm64
else
::iosX64
iOSTarget("ios") {
binaries {
framework {
baseName = "shared"
}
}
}
jvm().compilations.all {
kotlinOptions {
useIR = true
}
}
android()
val ktorVersion = "1.4.0"
val serializationVersion = "1.0.0-RC-$kotlinVersion"
val sqlDelightVersion: String by project
val coroutinesVersion = "1.3.9-native-mt"
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.2.1")
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
implementation("io.ktor:ktor-client-serialization:$ktorVersion")
implementation(kotlin("stdlib-common"))
implementation("com.squareup.sqldelight:runtime:$sqlDelightVersion")
//implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting {
dependencies {
implementation("com.google.android.material:material:1.3.0")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.2.1")
//implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
implementation("io.ktor:ktor-client-serialization:$ktorVersion")
implementation("io.ktor:ktor-client-android:$ktorVersion")
implementation("com.squareup.sqldelight:android-driver:$sqlDelightVersion")
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13")
}
}
val iosMain by getting {
dependencies {
implementation("io.ktor:ktor-client-ios:$ktorVersion")
implementation("com.squareup.sqldelight:native-driver:$sqlDelightVersion")
}
}
val iosTest by getting
}
}
android {
compileSdkVersion(30)
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdkVersion(24)
targetSdkVersion(30)
versionCode = 2
versionName = "2.0"
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
}
}
}
sqldelight {
database("PicturesDB") {
packageName = "com.pagetyler.shared.cache"
}
}
val packForXcode by tasks.creating(Sync::class) {
group = "build"
val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
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)
这是全局 build.gradle.kts
buildscript {
val kotlin_version by extra("1.5.20")
repositories {
gradlePluginPortal()
google()
mavenCentral()
maven(url = "https://kotlin.bintray.com/kotlinx/") // soon will be just jcenter()
}
val sqlDelightVersion= "1.5.0"
dependencies {
classpath("com.android.tools.build:gradle:4.2.2")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0")
classpath("org.jetbrains.kotlin:kotlin-serialization:$kotlin_version")
classpath("com.squareup.sqldelight:gradle-plugin:$sqlDelightVersion")
file("libs\\YouTubeAndroidPlayerApi.jarlibs\\YouTubeAndroidPlayerApi.jar")
}
}
apply (plugin = "com.squareup.sqldelight")
apply (plugin = "kotlin")
group = "com.pagetyler"
version = "1.0-SNAPSHOT"
allprojects {
repositories {
google()
mavenCentral()
jcenter()
maven(url = "https://kotlin.bintray.com/kotlinx/") // soon will be just jcenter()
}
}
【问题讨论】:
-
也许
org.jetbrains.kotlinx:kotlinx-serialization-core和io.ktor:ktor-client-serialization发生冲突?不过只是猜测。 -
尝试删除
io.ktor:ktor-client-serialization但会产生更多错误。恢复它并升级到 gradle-6.8.2,现在得到相同的 IDE 错误,但构建完成且没有错误。 -
唯一猜测是删除另一个,但又只是猜测
-
检查依赖树是否有不同版本的 jar 冲突
-
我发现唯一可能发生冲突的是项目 Gradle 文件中的
classpath("org.jetbrains.kotlin:kotlin-serialization:$kotlin_version")和共享文件中的implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")。我的 Kotlin 版本是“1.5.20”,我的序列化核心版本是“1.0.0-RC”。我尝试将这些更改为使用相同的版本,但会破坏其中一个。
标签: json kotlin gradle kotlin-multiplatform-mobile