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

...

}

相关推荐
勤劳打代码2 小时前
isar_flutter_libs 引发 Namespace not specified
android·flutter·groovy
奔跑吧 android4 小时前
【android bluetooth 协议分析 18】【PBAP详解 2】【车机为何不显示电话号码为空的联系人信息】
android·蓝牙电话·hfp·pbap·电话簿
深盾科技4 小时前
安卓二次打包技术深度拆解:从逆向篡改到防护逻辑
android
4Forsee4 小时前
【Android】消息机制
android·java·前端
2501_915921435 小时前
iOS 虚拟位置设置实战,多工具协同打造精准调试与场景模拟环境
android·ios·小程序·https·uni-app·iphone·webview
龚礼鹏5 小时前
Android 图像显示框架三——演示demo以及解析
android·交互
QuantumLeap丶6 小时前
《Flutter全栈开发实战指南:从零到高级》- 11 -状态管理Provider
android·flutter·ios
百锦再6 小时前
第6章 结构体与方法
android·java·c++·python·rust·go
gustt6 小时前
用小程序搭建博客首页:从数据驱动到界面展示
android·前端·微信小程序