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

...

}

相关推荐
2501_940007893 分钟前
Flutter for OpenHarmony三国杀攻略App实战 - 战绩记录功能实现
开发语言·javascript·flutter
灰灰勇闯IT8 分钟前
Flutter for OpenHarmony:TabBar 与 PageView 联动 —— 构建高效的内容导航系统
flutter
ujainu13 分钟前
Flutter + OpenHarmony 实战:从零开发小游戏(三)——CustomPainter 实现拖尾与相机跟随
flutter·游戏·harmonyos
2601_9499750821 分钟前
flutter_for_openharmonyflutter小区门禁管理app实战+报修详情实现
flutter
程序员清洒21 分钟前
Flutter for OpenHarmony:Scaffold 与 AppBar — 应用基础结构搭建
flutter·华为·鸿蒙
Rysxt_25 分钟前
UniApp获取安卓系统权限教程
android·uni-app
子春一27 分钟前
Flutter for OpenHarmony:构建一个 Flutter 习惯打卡应用,深入解析周视图交互、连续打卡逻辑与状态驱动 UI
flutter·ui·交互
菜鸟小芯1 小时前
【开源鸿蒙跨平台开发先锋训练营】DAY8~DAY13 底部选项卡&推荐功能实现
flutter·harmonyos
毕设源码-朱学姐1 小时前
【开题答辩全过程】以 基于安卓的教师上课辅助系统为例,包含答辩的问题和答案
android
kirk_wang1 小时前
Flutter艺术探索-Repository模式:数据层抽象与复用
flutter·移动开发·flutter教程·移动开发教程