You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Build.gradle同步报错:kotlin.plugin.serialization插件类型不匹配

问题:Kotlin序列化插件同步Gradle时出现类型不匹配错误

跟随Android官方基础教程开发,严格按步骤操作,但同步build.gradle时遇到如下错误:

Cause: org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin: org.jetbrains.kotlinx.serialization.gradle.SerializationGradleSubplugin not a subtype

我的app模块build.gradle文件内容如下:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'

    //User plugins
    id "org.jetbrains.kotlin.plugin.serialization" version "1.7.21"
}

android {
    namespace 'com.example.testeworkmanager'
    compileSdk 32

    defaultConfig {
        applicationId "com.example.testeworkmanager"
        minSdk 26
        targetSdk 32
        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.1.1'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.1'
    implementation "androidx.compose.ui:ui:$compose_ui_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
    implementation 'androidx.compose.material:material:1.1.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"

    //User dependencies

    implementation 'androidx.work:work-runtime-ktx:2.7.1'
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation "com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0"
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.0"
}

解决方案

  • 统一Kotlin插件版本:错误核心是org.jetbrains.kotlin.android插件和序列化插件版本不匹配。查看项目根目录build.gradle中定义的Kotlin版本(通常是ext.kotlin_version),将序列化插件版本修改为完全一致的版本。

    • 例如根目录Kotlin版本是1.7.21则无需改动,若为其他版本(如1.8.20),则修改plugins块中的序列化插件版本:
      id "org.jetbrains.kotlin.plugin.serialization" version "1.8.20"
      
  • 优化插件声明方式:Android项目中推荐使用简化的插件ID,且版本由根目录统一管理,避免手动指定版本出错。修改app模块plugins块:

    plugins {
        id 'com.android.application'
        id 'org.jetbrains.kotlin.android'
        id 'kotlinx-serialization' // 自动同步根目录Kotlin版本,无需手动指定
    }
    
  • 同步序列化依赖版本:确保kotlinx-serialization-json版本与Kotlin版本兼容。Kotlin 1.7.x对应1.4.x版本兼容,若升级Kotlin版本,需同步升级该依赖(如Kotlin 1.8.x对应1.5.x)。

  • 清理缓存重新同步:执行File -> Invalidate Caches... -> Invalidate and Restart,重启Android Studio后重新同步Gradle,清除缓存残留问题。

内容的提问来源于stack exchange,提问作者Anderson Linhares

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 10:01:14