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

Flutter构建APK时Kotlin Gradle插件版本报错求助

解决Flutter中Kotlin Gradle插件版本不兼容问题

你的问题核心是Android Gradle Plugin(AGP)与Kotlin Gradle Plugin(KGP)版本不匹配,即使你尝试了不同的Kotlin版本,缓存或版本约束冲突依然会导致报错,以下是具体解决方案:

1. 严格匹配AGP与KGP的兼容版本

你当前使用的AGP版本是7.1.3,对应的KGP版本范围是1.6.20 ~ 1.7.20:

  • 避免使用1.8.0-RC这类预览版,不稳定且兼容性差;
  • 确认build.gradle中Kotlin版本设置为兼容区间内的稳定版,比如1.7.20:
    buildscript {
        ext.kotlin_version = '1.7.20'
        repositories {
            google()
            mavenCentral()
        }
    
        dependencies {
            classpath 'com.android.tools.build:gradle:7.1.3'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
            classpath 'com.google.gms:google-services:4.3.14'    
        }
    }
    

2. 彻底清理Gradle缓存

旧缓存会残留不兼容的依赖,导致版本检测异常:

  • 执行Flutter清理命令:
    flutter clean
    
  • 手动删除项目中android/.gradle、android/build、build文件夹;
  • 重新拉取依赖:
    flutter pub get
    

3. 统一项目Kotlin版本约束

部分Flutter插件可能内置了低版本Kotlin依赖,引发冲突:

  • 在android/app/build.gradle中添加明确的Kotlin编译配置,强制统一版本:
    android {
        // ...其他配置
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
        kotlinOptions {
            jvmTarget = '1.8'
        }
    }
    

4. 匹配Gradle Wrapper版本

AGP 7.1.3需要搭配Gradle 7.2及以上版本,检查android/gradle/wrapper/gradle-wrapper.properties:

distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip

完成以上步骤后,重新尝试构建APK即可解决版本不兼容报错。

内容的提问来源于stack exchange,提问作者Ahmed Ibrahim

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 09:35:16