解决 Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8

I've been a little empty recently, so I started a new project and plan to tinker with something new.

After creating a new project, it was found that assembleRelease, including compilation, reported an error. The specific error information is as follows


* 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.

* Get more help at https://help.gradle.org

After looking at it, it probably means that the groovy in the build.gradle of the new project depends on the java11 version, and the java8 should be used on my computer, so the project cannot be compiled. TT

This can be done, although you can change the content in build.gradle to solve this problem, but it is too cumbersome to do it every time.

At the same time, you can't directly upgrade java8 directly. If you are promoted, the project in the company may not be able to compile and pass, and the loss outweighs the gain. . . So Baidu searched for a solution.

The more solution found is this change

Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8._LMF·的博客-CSDN博客But I tried it, no matter how I change it, if I continue to compile, I still have this problem. I gave up Baidu and googled it, but I didn't find a particularly good solution.

After calming down, I took a closer look at the solution recommended by AS

   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`.

The above blog post uses - changing the IDE settings to solve this problem. Since it doesn't work, let's take a look at the next two methods

Changing the JAVA_HOME environment variable. It means to change the javahome of our development environment. It looks a bit troublesome after Baidu, and it feels that it may affect existing projects.

Then there is only one last solution

changing `org.gradle.java.home` in `gradle.properties`.

------------------------------------------------------------

Next is the positive solution to this problem on my computer.

------------------------------------------------------------

First of all, since we do not have a java11 environment locally, we need to download a java11 jdk (jdk address https://repo.huaweicloud.com/java/jdk/11.0.2+9/jdk-11.0.2_osx-x64_bin.dmg )

When jdk is installed, just click next all the time. Since we only depend on some project-level dependencies on java11, we don't need to configure javahome for java11. After the installation is complete, you can see our

There is an additional version of jdk11 under the /Library/Java/JavaVirtualMachines path

 Then we only need to add a parameter in the gradle.properties of the project, for example, it should be

org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home

JavaVirtualMachines should be followed by the version you downloaded. After the addition is complete, synchronize the project, try it out, and the compilation is passed~

 

This is for mac computers, but it should work for windows computers as well.

reference:

Install JDK11 under Mac (domestic mirror image)

Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8._LMF·'s Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/just_hu/article/details/125822130