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

...

}

相关推荐
Lei活在当下4 分钟前
【Perfetto从入门到精通】4.使用 heapprofd 工具采样追踪 Java/Native 内存分配
android·性能优化·架构
alexhilton1 小时前
学会在Jetpack Compose中加载Lottie动画资源
android·kotlin·android jetpack
爱吃大芒果1 小时前
Flutter 主题与深色模式:全局样式统一与动态切换
开发语言·javascript·flutter·ecmascript·gitcode
summerkissyou19872 小时前
Android-Camera-为啥不移到packages/module
android·相机
liang_jy2 小时前
Android UID
android·面试
小a杰.4 小时前
Flutter 进阶:构建高性能跨平台应用的实践与技巧
flutter
nono牛5 小时前
安卓/MTK平台日志关键词详解
android
TimeFine6 小时前
Android AI解放生产力(四)实战:解放绘制UI的繁琐工作
android
sheji34166 小时前
【开题答辩全过程】以 基于Android的社区车位共享管理系统的设计与实现为例,包含答辩的问题和答案
android
TimeFine7 小时前
Android AI解放生产力(三):认识custom_prompts和skills
android