首页 > 解决方案 > 编译 Kotlin 序列化时的错误

问题描述

我已经阅读了一些关于图书馆的文章,但发现它们的工作方式仍然是一个谜。

我想使用 Kotlin 序列化库,但是在查看了我需要做的事情后,编译时仍然会出现错误。 Json.encodeToStringJson.decodeToString显示为红色。

https://medium.com/better-programming/why-and-how-to-use-kotlins-native-serialization-library-c88c0f14f93d


以下是我在 gradles 中的内容

在应用程序级别的gradle中我有

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlinx-serialization'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation 'org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0'
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlin_version"
}

在我的项目 Gradle 中

dependencies {
        classpath "com.android.tools.build:gradle:4.1.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

这是错误

Execution failed for task ':app:checkDebugAarMetadata'.
> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
   > Could not find org.jetbrains.kotlinx:kotlinx-serialization-json:extension 'kotlin'-version.
     Searched in the following locations:
       - https://dl.google.com/dl/android/maven2/org/jetbrains/kotlinx/kotlinx-serialization-json/extension 'kotlin'-version/kotlinx-serialization-json-extension 'kotlin'-version.pom
       - https://jcenter.bintray.com/org/jetbrains/kotlinx/kotlinx-serialization-json/extension 'kotlin'-version/kotlinx-serialization-json-extension 'kotlin'-version.pom
     Required by:
         project :app

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

标签: androidkotlinserialization

解决方案


更换的好像有问题kotlin_version。该行:

找不到 org.jetbrains.kotlinx:kotlinx-serialization-json:extension 'kotlin'-version。

工件应该是org.jetbrains.kotlinx:kotlinx-serialization-json:<version>. 确保在您的gradle.properties文件或其他地方(或不使用属性)正确定义了该属性。另请注意,序列化模块不跟踪 Kotlin 版本号,它使用不同的版本号。最新版本号为:下载


推荐阅读