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

...

}

相关推荐
李斯维7 分钟前
布局性能优化利器:ViewStub 极简指南
android·性能优化
卢叁25 分钟前
Flutter之阿里云视频播放器支持 iOS模拟器解决方案
flutter
循环不息优化不止1 小时前
Ktor Pipeline 机制深度解析
android
q***56381 小时前
Springboot3学习(5、Druid使用及配置)
android·学习
q***64972 小时前
SpringSecurity踢出指定用户
android·前端·后端
q***76662 小时前
SpringSecurity 实现token 认证
android·前端·后端
Chejdj2 小时前
ViewModel#onCleared的实现原理
android·源码阅读
CheungChunChiu2 小时前
Android 系统中的 NTP 服务器配置与选择逻辑详解
android·运维·服务器
q***49862 小时前
MySQL数据的增删改查(一)
android·javascript·mysql
aqi003 小时前
FFmpeg开发笔记(九十一)基于Kotlin的Android直播开源框架RootEncoder
android·ffmpeg·kotlin·音视频·直播·流媒体