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

Flutter打包Release版本时出现Kotlin版本不兼容等警告求助

Flutter Release打包时Kotlin版本不兼容及Firebase插件警告解决方案

一、解决Kotlin版本不兼容问题

你配置里指定了ext.kotlin_version = '1.8.0',但编译时出现「预期Kotlin元数据版本1.6.0,实际为1.8.0」的错误,核心原因是Gradle版本与Kotlin插件版本不匹配,或第三方依赖强制引入了旧版Kotlin。

1. 匹配Gradle与Kotlin插件版本

Gradle和Kotlin插件有严格的适配关系:

  • 若保留当前com.android.tools.build:gradle:7.3.0,建议将Kotlin版本调整为1.7.20(官方适配版本)
  • 若想保留Kotlin 1.8.0,需将Gradle版本升级到7.4.0+

修改项目根目录build.gradle:

buildscript {
    // 方案1:适配Gradle 7.3.0的Kotlin版本
    ext.kotlin_version = '1.7.20'
    // 方案2:升级Gradle适配Kotlin 1.8.0
    // classpath 'com.android.tools.build:gradle:7.4.2'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

2. 强制统一所有依赖的Kotlin版本

部分第三方依赖可能自带旧版Kotlin,需在项目根目录build.gradle中添加强制统一规则:

allprojects {
    repositories {
        google()
        mavenCentral()
    }
    // 强制所有依赖使用指定的Kotlin版本
    configurations.all {
        resolutionStrategy {
            force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
            force "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
            force "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
            force "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
        }
    }
}

3. 清理缓存并重新构建

执行以下命令彻底清理缓存,避免旧配置残留:

flutter clean
flutter pub get
cd android
./gradlew clean
./gradlew build --refresh-dependencies
cd ..
flutter build --release

二、处理Firebase插件的unchecked/deprecated API警告

这些警告来自cloud_firestore和firebase_messaging插件的源码,属于插件开发者需要修复的问题,你可以通过配置Gradle抑制这些警告:

在android/app/build.gradle的android块中添加:

android {
    // ...其他现有配置
    lintOptions {
        disable 'Unchecked', 'Deprecation'
        checkReleaseBuilds false // 可选:关闭Release构建的lint检查
    }
    // 统一Java/Kotlin编译版本
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

额外检查点

  • 运行flutter doctor确认Flutter SDK为稳定版,且与Firebase插件版本兼容
  • 检查android/gradle/wrapper/gradle-wrapper.properties中的Gradle分发版本,需与build.gradle中classpath版本一致,例如:
    distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 02:00:57