Shiasu-sama
Shiasu-sama

Reputation: 1269

How to instruct Gradle to use Java from different location?

I was building my test app in Android Studio, then in the Event Log it said:

Android Studio is using this JDK location: C:\Program Files\Android\Android Studio\jre which is different to what Gradle uses by default: C:\Program Files (x86)\Java\jdk1.8.0_181 Using different locations may spawn multiple Gradle daemons if Gradle tasks are run from command line while using Android Studio.

Then gave these options:

  1. More info...
  2. Set Android Studio to use the same JDK as Gradle and sync
  3. Do not show this warning again

I clicked on:

Set Android Studio to use the same JDK as Gradle and sync

then my app didn't want to build any more.

This is the error:

Gradle sync failed: Unable to start the daemon process.
This problem might be caused by incorrect configuration of the daemon.

For example, an unrecognized jvm option is used.
Please refer to the User Manual chapter on the daemon at https://docs.gradle.org/5.4.1/userguide/gradle_daemon.html

Process command line: C:\Program Files (x86)\Java\jdk1.8.0_181\bin\java.exe -Xmx1536m -Dfile.encoding=windows-1252 -Duser.country=ZA -Duser.language=en -Duser.variant -cp C:\Users\{MyUserName}\.gradle\wrapper\dists\gradle-5.4.1-all\3221gyojl5jsh0helicew7rwx\gradle-5.4.1\lib\gradle-launcher-5.4.1.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 5.4.1

Please read the following process output to find out more:

Error occurred during initialization of VM Could not reserve enough space for 1572864KB object heap Consult IDE log for more details (Help | Show Log) (16 s 166 ms)

I checked the event logs:

I think previously it did this:

Instructing gradle to use java from C:/Program Files/Android/Android Studio/jre

Now it says this:

Instructing gradle to use java from C:/Program Files (x86)/Java/jdk1.8.0_181

How do I set it back to the way it was so my app can build again?

Any help / advice will be appreciated

Upvotes: 50

Views: 107129

Answers (6)

Michael Besteck
Michael Besteck

Reputation: 2423

Update for Android Studio "Artic Fox | 2020.3.1 Patch 4" running on Linux (5.3.0-62-generic #56~18.04.1-Ubuntu) using Xfce4 Desktop environment.
This way allows for using independent, i.e. not synchronized, JRE/JDK versions on system and Android Studio.

Changed: The Gradle settings have been moved to File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle

To use unique JRE/JDK for Gradle execution:

  1. Open Gradle Settings and note the "Gradle JDK" Path.
    Hint: To select "Embedded JDK" first, if available, should be an appropriate choice.

  2. Edit the properties of the Android Studio starter (on Start Menu, Desktop, system menu bar, ...) , which should look like

    /home/USERNAME/android-studio/bin/studio.sh

    and change to

    /bin/bash -c "export JAVA_HOME=<GRRADLE JDK PATH FROM 1.>;/home/m/android-studio/bin/studio.sh"

Hint (untested): May be possible to insert the export JAVA_HOME=... statement into the studio.sh script, which may be overwritten on updates.

Check: After applying changes start Android Studio and execute Build -> Rebuild Project: The error message should have disappeared. Then check the same after File -> Invalidate Caches / Restart. Last, open the Android Studio embedded Terminal (bottom line click) and execute echo $JAVA_HOME. That should show the <GRADLE JDK PATH FROM 1.>, while a terminal opend from outside Android Studio should still show the (different) system's JAVA_HOME.

Upvotes: 1

Hoornet
Hoornet

Reputation: 1418

Go to File, Project Structure, SDK location and change the JDK location dropdown to JAVA_HOME.

Project structure in Androdi Studio 3.6 beta

Upvotes: 44

PJ_Finnegan
PJ_Finnegan

Reputation: 2181

Ubuntu 16.04 LTS, Android Studio 3.6

That's how I got rid of the warning:

1) locate the AS launcher script studio.sh on your machine, mine is under: /opt/android-studio-3.x/bin
2) inside the script, you'll find the following lines:

# ---------------------------------------------------------------------
# Locate a JDK installation directory which will be used to run the IDE.
# Try (in order): STUDIO_JDK, studio.jdk, ./jre64, JDK_HOME, JAVA_HOME, "java" in PATH.
# ---------------------------------------------------------------------
if [ -n "$STUDIO_JDK" -a -x "$STUDIO_JDK/bin/java" ]; then
  JDK="$STUDIO_JDK"
fi

if [ -z "$JDK" -a -s "$HOME/.AndroidStudio3.6/config/studio.jdk" ]; then
  USER_JRE=`"$CAT" $HOME/.AndroidStudio3.6/config/studio.jdk`
...

3) if, for some reason, you're unable to export your environment variables to the launcher (like it happens to me from the xfce4 session), you can write the desired JAVA_HOME value inside the studio.jdk file whose location on my system, as per the script, is in $HOME/.AndroidStudio3.6/config/studio.jdk.

$ echo $JAVA_HOME > $HOME/.AndroidStudio3.6/config/studio.jdk
$ cat $HOME/.AndroidStudio3.6/config/studio.jdk
/usr/lib/jvm/java-8-oracle

4) lastly, by restarting AS from the DE session, you should see the JAVA_HOME under the File -> Project Structure -> SDK Location window set to the above value and both AS and gradle should use the same JRE:

Project Structure Window

Upvotes: 2

Pablo Alfonso
Pablo Alfonso

Reputation: 2465

Gradle is clearly using my native java installation (from Oracle) whereas Android Studio is using its own version.

I have been ignoring this warning since the upgrade of Android Studio to 3.6.1. No issue so far. Indeed, I have released new versions of my app without issues.

If this becomes annoying, and IF you have no other issues, you have an option to ignore future warnings: Just press the warning and select option to ignore future messages related to this warning.

Upvotes: 1

Guru Ananda
Guru Ananda

Reputation: 81

You need to change/add an environment variable.

Follow this link to know how to change/add environment variables.

Now add/modify the variable 'JAVA_HOME' and set the path to C:\Program Files\Android\Android Studio\jre

Note: When you are going to work on a different Java project later on in a different IDE, you might have to change this variable again to the default Java location (in this case perhaps C:/Program Files (x86)/Java/jdk1.8.0_181)

Upvotes: 8

Shiasu-sama
Shiasu-sama

Reputation: 1269

On Android Studio I did:

File > Project Structure... > SDK Location (On the left pane)

Then at the bottom there is a drop down below: JDK location:

I then selected / browsed to this directory:

C:\Program Files\Java\jdk1.8.0_181

Gradle Sync worked again for me after that but I still get this warning:

Android Studio is using this JDK location: C:\Program Files\Java\jdk1.8.0_181 which is different to what Gradle uses by default: C:\Program Files (x86)\Java\jdk1.8.0_181. Using different locations may spawn multiple Gradle daemons if Gradle tasks are run from command line while using Android Studio.

  1. More info...
  2. Set Android Studio to use the same JDK as Gradle and sync project
  3. Do not show this warning again

Upvotes: 0

Related Questions