Flutter 无法在 Android 上构建 App,因为它在 JDK 安装中找不到所需的工具

Flutter can't build App on Android because it can't find required tool in JDK installation

我可以为 Linux 设备构建带有 flutter 的应用程序,但我不能为任何连接的设备构建应用程序。 当我 运行 flutter doctor 我得到

[✓] Flutter (Channel master, 1.24.0-8.0.pre.277, on Linux, locale en_NG)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[✓] Linux toolchain - develop for Linux desktop
[!] Android Studio (not installed)
[✓] Connected device (2 available)

! Doctor found issues in 1 category.

但我安装了 Android Studio 问题是我什至不使用 Android Studio,我使用 Visual Studio 代码,但由于其他一些问题我安装了它。

现在的问题是 每当我尝试在我已连接的 Android 上构建应用程序时:

$ flutter devices
2 connected devices:

SM A505F (mobile) • RZ8M902VAZK • android-arm64 • Android 10 (API 29)
Linux (desktop)   • linux       • linux-x64     • Linux

我收到错误

$ flutter run -d RZ8M902VAZK
Launching lib/main.dart on SM A505F in debug mode...
                                                                        
FAILURE: Build failed with an exception.                                
                                                                        
* What went wrong:                                                      
Execution failed for task ':app:compileDebugKotlin'.                    
> Kotlin could not find the required JDK tools in the Java installation '/usr/lib/jvm/java-8-openjdk-amd64/jre' used by Gradle. Make sure Gradle is running on a JDK, not JRE.
                                                                        
* 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 8s                                                      
Running Gradle task 'assembleDebug'...                                  
Running Gradle task 'assembleDebug'... Done                         9.5s
Exception: Gradle task assembleDebug failed with exit code 1

但我安装了 JDK:

$ java -version
openjdk version "1.8.0_275"
OpenJDK Runtime Environment (build 1.8.0_275-8u275-b01-0ubuntu1~18.04-b01)
OpenJDK 64-Bit Server VM (build 25.275-b01, mixed mode)

非常感谢!!

打开 Android studio 并安装 Flutter Plugin 这将解决您的 Flutter 问题

> Kotlin could not find the required JDK tools in the Java installation '/usr/lib/jvm/java-8-openjdk-amd64/jre' used by Gradle. Make sure Gradle is running on a JDK, not JRE.

设置为/usr/lib/jvm/java-8-openjdk-amd64/jre

应将 JAVA_HOME 设置为 /usr/lib/jvm/java-8-openjdk-amd64/

为您的用户在 Mac OS X 或 Linux 上设置 JDK 路径

  1. 登录并打开终端或命令行 window。
  2. 使用 vi 或其他命令行编辑器(例如 Pico)打开 ~/.bash_profile。
  3. 例如输入vi ~/.bash_profile.
  4. 将您的 PATH 设置为包含名为 java 的 JDK 子文件夹。对于 example:export PATH=$PATH:/usr/java/jdk1.6.0_10/bin 保存更改并注销然后返回,或者,要激活新设置,请输入 source ~/.bash_profile

确保您已设置所有必需的路径和 JAVA_HOME。

Flutter 安装指南

Follow this if you are on Windows.

Follow this if one macos

any other