升级并共存Android studio遇到的问题
背景:
目前开发是使用公司的MAC电脑,今天下载了最新的Android studio Ladybug,新老版本共存时,如果使用了新版的Android studio 打开了Flutter项目 ,那么它的Android toolchain 中的Java binary 就会变成新的Android studio的java版本,导致如果再使用旧版本编译的时候就会报错。
错误如下:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':gradle:compileGroovy'.
> BUG! exception in phase 'semantic analysis' in source unit '/Users/jiyuren/fvm/versions/3.27.3/packages/flutter_tools/gradle/src/main/groovy/app_plugin_loader.groovy' 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 3s
┌─ 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/jiyuren/Workspaces/Android/work/xxx/apps/xxxx/android/gradle/wrapper/grad │
│ le-wrapper.properties 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
上面的错误表明:你的错误 Unsupported class file major version 65 表示当前使用的 Java 版本(JDK 21,对应 class 文件版本 65)与 Gradle 版本不兼容。
flutter doctor -v
[✓] Flutter (Channel stable, 3.27.3, on macOS 15.1.1 24B91 darwin-arm64, locale zh-Hans-CN)
• Flutter version 3.27.3 on channel stable at /Users/jiyuren/fvm/versions/3.27.3
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision c519ee916e (2 周前), 2025-01-21 10:32:23 -0800
• Engine revision e672b006cb
• Dart version 3.6.1
• DevTools version 2.40.2
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
• Android SDK at /Users/jiyuren/Library/Android/sdk
• Platform android-35, build-tools 35.0.0
• ANDROID_HOME = /Users/jiyuren/Library/Android/sdk
• Java binary at: /Applications/Android Studio Ladybug.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 21.0.4+-12422083-b607.1)
• All Android licenses accepted.
因此使用 flutter doctor -v 查看哪里不对,发现 Android toolchain 下的Java binary at 这里使用的 java 版本是新版本的Android studio 的java 版本,新版本的Java 使用的是21,上个版本使用的是17,因此不兼容,所以就想着如何修改 Android toolchain 中的Java binary 版本,通过chatgpt 查找获知,可以
使用 flutter config 显式指定 JDK
因此 敲入 flutter config --help
flutter config --help
Configure Flutter settings.
To remove a setting, configure it to an empty string.
The Flutter tool anonymously reports feature usage statistics and basic crash reports to help improve Flutter tools over time. See Google's privacy policy: https://www.google.com/intl/en/policies/privacy/
Global options:
-h, --help Print this usage information.
-v, --verbose Noisy logging, including all shell commands executed.
If used with "--help", shows hidden options. If used with "flutter doctor", shows additional diagnostic
information. (Use "-vv" to force verbose logging in those cases.)
-d, --device-id Target device id or name (prefixes allowed).
--version Reports the version of this tool.
--enable-analytics Enable telemetry reporting each time a flutter or dart command runs.
--disable-analytics Disable telemetry reporting each time a flutter or dart command runs, until it is re-enabled.
--suppress-analytics Suppress analytics reporting for the current CLI invocation.
Usage: flutter config [arguments]
-h, --help Print this usage information.
--list List all settings and their current values.
--clear-ios-signing-cert Clear the saved development certificate choice used to sign apps for iOS device deployment.
--android-sdk The Android SDK directory.
--android-studio-dir The Android Studio installation directory. If unset, flutter will search for valid
installations at well-known locations.
--jdk-dir The Java Development Kit (JDK) installation directory. If unset, flutter will search for
one in the following order:
1) the JDK bundled with the latest installation of Android Studio,
2) the JDK found at the directory found in the JAVA_HOME environment variable, and
3) the directory containing the java binary found in the user's path.
--build-dir=<out/> The relative path to override a projects build directory.
--[no-]enable-web Enable or disable Flutter for web.
--[no-]enable-linux-desktop Enable or disable support for desktop on Linux.
--[no-]enable-macos-desktop Enable or disable support for desktop on macOS.
--[no-]enable-windows-desktop Enable or disable support for desktop on Windows.
--[no-]enable-android Enable or disable Flutter for Android.
--[no-]enable-ios Enable or disable Flutter for iOS.
--[no-]enable-fuchsia Enable or disable Flutter for Fuchsia.
This setting applies only to the master channel.
--[no-]enable-custom-devices Enable or disable early support for custom device types.
--[no-]cli-animations Enable or disable animations in the command line interface.
--[no-]enable-native-assets Enable or disable native assets compilation and bundling.
This setting applies only to the master channel.
--[no-]enable-flutter-preview Enable or disable Flutter preview prebuilt device.
This setting applies only to the master and beta channels.
--[no-]enable-swift-package-manager Enable or disable support for Swift Package Manager for iOS and macOS.
--clear-features Remove all configured features and restore them to the default values.
Run "flutter help" to see global options.
看到有一行是 --android-studio-dir ,那么我就尝试这个命令
flutter config --android-studio-dir /Applications/Android\ Studio.app
修改成功,再次编译运行,不报错了, 问题解决!
flutter doctor -v
[✓] Flutter (Channel stable, 3.27.3, on macOS 15.1.1 24B91 darwin-arm64, locale zh-Hans-CN)
• Flutter version 3.27.3 on channel stable at /Users/jiyuren/fvm/versions/3.27.3
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision c519ee916e (2 周前), 2025-01-21 10:32:23 -0800
• Engine revision e672b006cb
• Dart version 3.6.1
• DevTools version 2.40.2
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
• Android SDK at /Users/jiyuren/Library/Android/sdk
• Platform android-35, build-tools 35.0.0
• ANDROID_HOME = /Users/jiyuren/Library/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)
• All Android licenses accepted.