我正在尝试kotlinx.serialization
在我的 KMM 应用程序中使用来解析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()
}
}