Android - Can't compile on JRE - Make sure Gradle is running on a JDK, not JRE

Viewed 25102

I want to compile my app from command line, and not from Android Studio. My app works perfectly when I launch it from Android Studio to my device / emulator, however when I try to do :

gradlew build

I get :

> Task :app:kaptGenerateStubsReleaseKotlin FAILED
Caught an exception trying to connect to Kotlin Daemon
org.gradle.api.GradleException: Kotlin could not find the required JDK tools in the Java installation 'C:\Program Files (x86)\Java\jre1.8.0_191' used by Gradle. Make sure Gradle is running on a JDK, not JRE.
        at org.jetbrains.kotlin.gradle.tasks.JarSearchingUtilKt.findToolsJar(jarSearchingUtil.kt:95)
        at org.jetbrains.kotlin.compilerRunner.GradleCompilerEnvironment$toolsJar$2.invoke(GradleCompilerEnvironment.kt:23)
        at org.jetbrains.kotlin.compilerRunner.GradleCompilerEnvironment$toolsJar$2.invoke(GradleCompilerEnvironment.kt:17)
        at kotlin.SynchronizedLazyImpl.getValue(Lazy.kt:131)
        ...

Here is my JAVA_HOME env var :

JAVA_HOME=C:\Program Files (x86)\Java\jre1.8.0_191

It IS actually a JRE (and not a JDK), but, when I change my env var by my JDK as following :

JAVA_HOME=C:\Program Files\Java\jdk-11.0.1    

And I launch :

gradlew build

Then I get :

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine java version from '11.0.1'.

* 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
5 Answers

You have to use JDK 8, but you are using JDK 11.

Its probably complaining about the fact you are trying to use a JDK 11 version when your gradle is only compatible up to a lesser JDK version.

The short answer would be - download a JDK 1.8.something version and try with that, the alternate (and probably better long term) answer would be to upgrade your gradle version to the latest (i.e. a JDK 11 compatible version).

EDIT: so google says that you need a minimum of Gradle 5.0 to use JDK 11 :)

In gradle-wrapper.properties add the following line

org.gradle.java.home=/usr/lib/jvm/java-8-openjdk-amd64

Replace with the path to your JDK

I used to build integration_test for flutter

This worked for me

  1. Download Gradle from gradle.org OR from services.gradle.org
  2. Unzip the downloaded file in C://
  3. Update the /bin path in System Environment Variables Run the program and check if the problem is fixed? Proceed next if you still got the error(s)
  4. Download Java SE Runtime Environment 8u261 here
  5. Update the /bin path in System Environment Variables This will fix the issue. Lemme know if you still got errors

In my case, java and javac had different versions. Check out and configure with:

 $ update-alternatives --config javac

and

 $ update-alternatives --config java

This article helped a lot. I see this article is for Windows, but somebody using Ubuntu may find this answer useful.

Related