JavaEar 专注于收集分享传播有价值的技术资料

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

4个回答

    最佳答案
    1. Make Android Studio use bundled JRE by setting File -> Project Structure... -> SDK Location -> JDK localtion to default value, typically C:\Program Files\Android\Android Studio\jre
    2. Make Gradle use the same JRE by overriding JAVA_HOME environment variable in gradlew.bat by placing set directive after @rem Find java.exe:
    @rem Find java.exe
    set JAVA_HOME="C:/Program Files/Android/Android Studio/jre"
    if defined JAVA_HOME goto findJavaFromJavaHome
    
    1. Restart Android Studio.
  1. 参考答案2
  2. 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)

  3. 参考答案3
  4. 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
  5. 参考答案4
  6. just set JAVA_HOME to the same dir as the JDK location in Android Studio

    Project structure in Androdi Studio 3.6 beta