Android项目Kotlin Serialization插件未生效,如何解决?
Kotlin Serialization编译器插件未生效问题排查
我已遵循Kotlin官方文档配置,但仍无法正常使用Kotlin Serialization,使用@Serializable注解时收到如下警告:
kotlinx.serialization compiler plugin is not applied to the module, so this annotation would not be processed. Make sure that you've setup your buildscript correctly and re-import project.
项目级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)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" }
项目级settings.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'
警告截图

解决方案
针对当前配置,按以下步骤修复:
对齐序列化库与Kotlin版本
Kotlin版本1.9.20对应的序列化库最低版本为1.6.3,当前依赖的1.6.2版本不兼容。修改依赖:implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3"调整模块插件声明顺序
将序列化插件放在Kotlin Android插件之前声明,避免加载冲突:plugins { id 'com.android.application' id 'org.jetbrains.kotlin.plugin.serialization' id 'org.jetbrains.kotlin.android' }清理缓存并同步项目
- 点击Android Studio右上角的
Sync Project with Gradle Files同步项目 - 执行
File > Invalidate Caches... > Invalidate and Restart清理IDE缓存后重启
- 点击Android Studio右上角的
确认Gradle版本兼容性
Android Gradle插件8.1.4需要搭配Gradle8.0+版本,检查gradle/wrapper/gradle-wrapper.properties中的配置:distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
完成以上操作后重新编译项目,@Serializable注解的警告即可消除。
内容的提问来源于stack exchange,提问作者Avin
相关产品推荐
相关产品推荐

