短记:Flutter 项目常见问题 Gradle version is incompatible with the Java version

StackOverflow中有比较详细的解决方案

简而言之:Gradle版本和Java支持版本不一致

例子:

运行命令 flutter analyze --suggestions
在这里插入图片描述
如果版本不匹配情况,则最后一行会有类似提示

[] Java/Gradle/Android Gradle Plugin: Incompatible Java/Gradle versions. Java Version: 21.0.3, Gradle Version: 7.6.3

通过 AndroidStdio 创建的 Flutter项目,默认 gradle 版本可能是 7.6.3 或其他版本,如果当前 JDK 版本为 21 时,gradle 需要升级版本到 8.5 及以上才能支持

此时需要修改两个地方:

  1. gradle-wrapper.properties 中的 distributionUrl 配置:
// 修改为 gradle-8.5-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-all.zip
  1. settings.gradle 中的 com.android.application,也就是 AGP:
// 至少在 8.3.2 版本
plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "8.3.2" apply false
    id "org.jetbrains.kotlin.android" version "1.7.10" apply false
}

更多 JDK 与 Gradle 版本的对应关系详见 Compatibility Matrix,根据情况更新不同的版本即可

Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source! Launching lib\main.dart on AOSP on IA Emulator in debug mode... Support for Android x86 targets will be removed in the next stable release after 3.27. See https://github.com/flutter/flutter/issues/157543 for details. Running Gradle task 'assembleDebug'... FAILURE: Build failed with an exception. * What went wrong: Could not open cp_settings generic class cache for settings file 'D:\and sroid\Android\work\untitled5\android\settings.gradle' (C:\Users\����һ����\.gradle\caches\7.5\scripts\cgwnh8p454v85dv1xmzoedwjx). > BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 65 * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 1s ┌─ Flutter Fix ───────────────────────────────────────────────────────────────────────────────────┐ │ [!] Your project's Gradle version is incompatible with the Java version that Flutter is using │ │ for Gradle. │ │ │ │ If you recently upgraded Android Studio, consult the migration guide at │ │ https://flutter.dev/to/java-gradle-incompatibility. │ │ │ │ Otherwise, to fix this issue, first, check the Java version used by Flutter by running `flutter │ │ doctor --verbose`. │ │ │ │ Then, update the Gradle versio
最新发布
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值