错误信息
Android studio 编译项目失败输出如下错误信息:
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\Users\Workplace\SampleApp\app\build.gradle' line: 2
An exception occurred applying plugin request [id: 'com.android.application']
> Failed to apply plugin 'com.android.internal.application'.
> Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
Your current JDK is located in C:\Users\.jdks\corretto-1.8.0_332\jre
You can try some of the following options:
- changing the IDE settings.
- changing the JAVA_HOME environment variable.
- changing `org.gradle.java.home` in `gradle.properties`.
* 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.
解决方法
因为 AGP (Android Gradle plugin)
需的最低 Java
版本更改为 Java 11
,如果 JVM 指向 1.8 版,那么您应该在设置中更改它。
1. 在如下选项路径中找到 JDK 的设置页面:
File > Settings > Build, Execution, Deployment > Build Tools > Gradle
(在 Mac 上的选项路径是:Android Studio > Preferences... > Build, Execution, Deployment > Build Tools > Gradle)
或者在 Gradle 视图中直接打开它的设置页面:
2. 修改 Gradle JDK
的版本为 JDK 11
3. 其他
如果使用的是独立于 Android Studio 的 AGP,请将 JAVA_HOME
环境变量或 -Dorg.gradle.java.home
命令行选项设置为您的 JDK 11 安装目录,以此升级 JDK 版本。
关于 AGP 和 JDK 的其他信息
1. 从 AGP 7.0.0-alpha01 版本开始需要 JDK 11 才能运行
https://android-developers.googleblog.com/2020/12/announcing-android-gradle-plugin.html
Java 11 requirement
With AGP 7.0.0-alpha01 we are changing the minimum required Java programming language version to Java 11. We are announcing this early in the Canary schedule and many months ahead of the stable release to allow developers time to get ready.
2. AGP 7.0.0 的兼容性要求
https://developer.android.com/studio/releases/gradle-plugin#compatibility_2
最低版本 | 默认版本 | |
---|---|---|
Gradle | 7.0.2 | 7.0.2 |
SDK Build Tools | 30.0.2 | 30.0.2 |
NDK | 不适用 | 21.4.7075529 |
JDK | 11 | 11 |