gradle 7.0编译报错:Android Gradle plugin requires Java 11 to run

背景

Android工程中gradle已升级到7.0版本。
gradle-wrapper.properties文件内容:

distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

问题

执行gradle命令时报错。

gradlew clean build

报错信息:

* What went wrong:
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.
     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.

解决方法

方案一、 修改gradle.properties,增加如下内容.
后面的路径请自行修改为自己本地的jdk或jre版本(Java 11或以上)

org.gradle.java.home=D\:/Program Files/Android/Android Studio/jre

方案二、修改环境变量JAVA_HOME的值,指向Java 11或以上


关于我

厦门大学计算机专业 | 前华为工程师
分享编程技术,没啥深度,但看得懂,适合初学者。
Java | 安卓 | 前端 | 小程序 | 鸿蒙
公众号:花生皮编程

你可能感兴趣的:(gradle 7.0编译报错:Android Gradle plugin requires Java 11 to run)