Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gradlew uses wrong Java version

I am working on an android application using Kotlin. I am running a kotlin linter through command line ./gradlew klint.
But it gives me an error:

Gradle 4.4 requires Java 7 or later to run. You are currently using Java 6.

When I check the project structure, I am using the embedded JDK.
I also updated android studio to latest version. I do not know how to update the embedded JDK to latest version.

java version

"1.6.0_65" Java(TM) SE Runtime Environment (build 1.6.0_65-b14-468) Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-468, mixed mode)

which java - /usr/bin/java

echo $PATH - /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/username/Library/Android/sdk/platform-tools

echo $JAVA_HOME - empty

like image 491
Pardeep Kumar Avatar asked Apr 20 '18 04:04

Pardeep Kumar


2 Answers

A copy of the latest OpenJDK comes bundled with Android Studio 2.2 and higher, and this is the JDK version we recommend you use for your Android projects.

https://developer.android.com/studio/intro/studio-config.html#jdk

This however, is only for building your apps within Android Studio, not from the terminal with gradlew, otherwise, it'll use whatever is on your OS's $PATH variable.

In order to use the embedded JDK, you at least need to set JAVA_HOME, for example on Linux/Mac,

$ export JAVA_HOME=/path/to/AndroidStudio/jdk  # TODO: Find this
$ ./gradlew

My recommendation, however, is to use the mechanism for your OS for installing Java.

For easy Java library management (on Linux & Mac), you can try using sdkman

like image 187
OneCricketeer Avatar answered Sep 23 '22 06:09

OneCricketeer


gradle.properties can be updated to point to the embedded JDK that comes with Android Studio in Mac OS:

org.gradle.java.home=/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home

like image 27
Vairavan Avatar answered Sep 22 '22 06:09

Vairavan