我无法导入Kotlin序列化,我认为我遵循了Kotlin文档中的说明,但当我使用@Serializable注释时,我收到以下警告:

kotlinx.serialization编译器插件未应用于模块,因此不会处理此注释.确保你已经正确地设置了你的buildscript并重新导入项目.

以下是我的项目级build.gradle:

buildscript {
    ext {
        compose_version = '1.5.5'
        kotlin_version = '1.9.20'
    }
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '8.1.4' apply false
    id 'com.android.library' version '8.1.4' apply false
    id 'org.jetbrains.kotlin.android' version "$kotlin_version" apply false
    id 'org.jetbrains.kotlin.plugin.serialization' version "$kotlin_version" apply false
}

我创建了一个新的模块cannotimportSerialization只是为了进行健全性判断,下面是模块级别(CannotimportSerialization)build.gradle文件

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'org.jetbrains.kotlin.plugin.serialization'
}

android {
    namespace 'com.example.cannotimportserialization'
    compileSdk 34

    defaultConfig {
        applicationId "com.example.cannotimportserialization"
        minSdk 29
        targetSdk 34
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.4.3'
    }
    packaging {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.2'
    implementation 'androidx.activity:activity-compose:1.8.1'
    implementation platform('androidx.compose:compose-bom:2023.03.00')
    implementation 'androidx.compose.ui:ui'
    implementation 'androidx.compose.ui:ui-graphics'
    implementation 'androidx.compose.ui:ui-tooling-preview'
    implementation 'androidx.compose.material3:material3'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation platform('androidx.compose:compose-bom:2023.03.00')
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
    debugImplementation 'androidx.compose.ui:ui-tooling'
    debugImplementation 'androidx.compose.ui:ui-test-manifest'

    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.2"
}

以下是项目级别设置.gradle:

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "Test Project"
include ':app'
include ':cannotimportserialization'

warning

我在这里做什么来将编译器插件应用到模块?

推荐答案

Based on the response from the developers in the GitHub issue,这是Android Studio的Kotlin插件的问题. 据我所知,这个问题将存在,如果插件版本看起来像这样:231-1.9.10-release-459-AS9392.1.2311.11076708(1.9.10是关键部分).你可以go Settings -> Languages & Frameworks -> Kotlin号房看看.

解决方案(来自相同的回复):

要解决此问题,您需要更新Kotlin IDE插件. 遗憾的是,1.9.20版IDE插件版本不适用于Hedgehog 2023.1.1还没有.有两种方法可以修复它:

Android相关问答推荐

在画布上画直线

Android Bundle getSerializable(String?):'可序列化?&# 39、被抛弃了在Java中被弃用

滚动屏幕时更改按钮外观

如何制作带有图标和文本的Fab

Android开发:主题排版不适用于按钮文本

如何在Jetpack composeH中创建具有弯曲末端的六边形形状

约束布局:垂直链中的视图应将内容包裹到空间的1/3

在不增加父行宽度的情况下添加延迟行或可滚动行

这款应用与最新版本的Android不兼容.在Android 14中

BroadCastReceiver的onReceive方法中的Intent上的Extras为空

我想使用代码自动重命名我的Android存储中的文件夹

如何从sqlite数据库中检索数据到碎片android?

空数据来自改装

我需要在房间数据库中保留旧的自动迁移行吗?

在 Jetpack Compose 中包装内容

组成不重叠的元素

在 Jetpack Compose 中使用 .observeAsState() 时,如何在更改 MutableLiveData 的值后开始执行一段代码?

在 Kotlin 客户端应用程序中发送 FCM 推送通知 - Firebase 云消息传递

新安装 Expo Go 的 android 设备上的 sdk 46.0.0 (@snack/sdk.46.0.0) 出错

当我更改 ViewModel var 时,Kotlin + Compose 中的 Composable 不会更新