Android Studio升级后,Flutter运行android设备报错

1、第一个问题

bash 复制代码
Launching lib/main.dart on YAL AL10 in debug mode...
Running Gradle task 'assembleDebug'...

FAILURE: Build failed with an exception.

* What went wrong:
Could not open cp_settings generic class cache for settings file '/Users/linwang.chen/AndroidStudioProjects/shorebird_demo/android/settings.gradle' (/Users/linwang.chen/.gradle/caches/7.5/scripts/49jg4rnjyeaopvpykxnb0egtu).
> 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 374ms

┌─ 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 version specified in                                                     │
│ /Users/linwang.chen/AndroidStudioProjects/shorebird_demo/android/gradle/wrapper/gradle-wrapper.p │
│ roperties to be compatible with that Java version. See the link below for more information on    │
│ compatible Java/Gradle versions:                                                                 │
│ https://docs.gradle.org/current/userguide/compatibility.html#java                                │
│                                                                                                  │
│                                                                                                  │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
Error: Gradle task assembleDebug failed with exit code 1

你的 Java 版本太新 ,而 Gradle 版本太旧,两者不兼容。
Unsupported class file major version 65

class file major version Java版本
61 Java 17
65 Java 21 ✅ ← 你当前用的
❌ 但Gradle 7.5 不支持 Java 21

⚙️ 问题原因:

你的项目使用了 Java 21(major 65)

但 android/gradle/wrapper/gradle-wrapper.properties 中的 Gradle 版本太低(7.5),

导致无法识别新版 Java 的 class 格式。

✅ 解决方法:

1️⃣ 打开文件:

android/gradle/wrapper/gradle-wrapper.properties

找到这一行:

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

改成:

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

(Gradle 8.7 是稳定版本,支持 Java 21)

同时更新 Android Gradle Plugin (AGP)

打开:

android/build.gradle

找到:

classpath 'com.android.tools.build:gradle:7.2.0'

改成:

classpath 'com.android.tools.build:gradle:8.4.0'

重新运行:

flutter clean

flutter pub get

flutter run

2、第二个问题

bash 复制代码
Launching lib/main.dart on YAL AL10 in debug mode...
Running Gradle task 'assembleDebug'...
You are applying Flutter's main Gradle plugin imperatively using the apply script method, which is deprecated and will be removed in a future release. Migrate to applying Gradle plugins with the declarative plugins block: https://flutter.dev/to/flutter-gradle-plugin-apply


FAILURE: Build completed with 2 failures.

1: Task failed with an exception.
-----------
* Where:
Build file '/Users/linwang.chen/AndroidStudioProjects/shorebird_demo/android/build.gradle' line: 27

* What went wrong:
A problem occurred evaluating root project 'android'.
> A problem occurred configuring project ':app'.
   > Could not create an instance of type com.android.build.api.variant.impl.ApplicationVariantImpl.
      > Namespace not specified. Specify a namespace in the module's build file. See https://d.android.com/r/tools/upgrade-assistant/set-namespace for information about setting the namespace.

        If you've specified the package attribute in the source AndroidManifest.xml, you can use the AGP Upgrade Assistant to migrate to the namespace value in the build file. Refer to https://d.android.com/r/tools/upgrade-assistant/agp-upgrade-assistant for general information about using the AGP Upgrade Assistant.

* 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.
==============================================================================

2: Task failed with an exception.
-----------
* Where:
Build file '/Users/linwang.chen/AndroidStudioProjects/shorebird_demo/android/build.gradle' line: 27

* What went wrong:
A problem occurred evaluating root project 'android'.
> A problem occurred configuring project ':app'.
   > Could not create an instance of type com.android.build.api.variant.impl.ApplicationVariantImpl.
      > Namespace not specified. Specify a namespace in the module's build file. See https://d.android.com/r/tools/upgrade-assistant/set-namespace for information about setting the namespace.

        If you've specified the package attribute in the source AndroidManifest.xml, you can use the AGP Upgrade Assistant to migrate to the namespace value in the build file. Refer to https://d.android.com/r/tools/upgrade-assistant/agp-upgrade-assistant for general information about using the AGP Upgrade Assistant.

* 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 1m 19s
Error: Gradle task assembleDebug failed with exit code 1

主要错误:Namespace not specified. Specify a namespace in the module's build file.

这是 Android Gradle Plugin(AGP 8.x)新规则。

以前 AndroidManifest.xml 里的 package 字段可以自动识别命名空间;

现在必须在 android/app/build.gradle 里手动添加明明空间

打开:android/app/build.gradle

在 android { ... } 块的最前面加一行:

android {

namespace "com.shorebird.demo.shorebird_demo" // 👈 添加这一行(包名和 AndroidManifest.xml 中一致)

compileSdkVersion flutter.compileSdkVersion

ndkVersion flutter.ndkVersion

...

}

相关推荐
贺biubiu2 小时前
2025 年终总结|总有那么一个人,会让你千里奔赴...
android·程序员·年终总结
xuekai200809013 小时前
mysql-组复制 -8.4.7 主从搭建
android·adb
nono牛4 小时前
ps -A|grep gate
android
未知名Android用户5 小时前
Android动态变化渐变背景
android
nono牛6 小时前
Gatekeeper 的精确定义
android
stevenzqzq7 小时前
android启动初始化和注入理解3
android
LawrenceLan8 小时前
Flutter 零基础入门(九):构造函数、命名构造函数与 this 关键字
开发语言·flutter·dart
一豆羹9 小时前
macOS 环境下 ADB 无线调试连接失败、Protocol Fault 及端口占用的深度排查
flutter
行者969 小时前
OpenHarmony上Flutter粒子效果组件的深度适配与实践
flutter·交互·harmonyos·鸿蒙
城东米粉儿9 小时前
compose 状态提升 笔记
android