2

I have been working on Android Studio lately and I am able to develop apps. I add dependencies via build.gradle for external libraries. Everything seems to work just fine. Recently I was just trying read about gradle and I noticed it was needed of me to set "GRADLE_HOME%\bin" to system variable under path.

I dont think I have set any environment variable for gradle. Being honest I didnt have proper guidance and I lack much knowledge regarding gradle. If somebody would kindly explain its purpose,I'd be thankful.

edit-I have JAVA_HOME set,My question was not regarding that. I couldnt find anything regarding GRADLE_HOME being set,but it didn't seem to affect my work with android studio(involving gradle). Hence I wanted to know in what way it GRADLE_HOME was useful.

  • Possible duplicate of [what is the reason for the existence of the JAVA\_HOME environment variable?](https://stackoverflow.com/questions/5102022/what-is-the-reason-for-the-existence-of-the-java-home-environment-variable) – Tim Sep 18 '17 at 09:14
  • that one is about JAVA_HOME but all _HOME variables serve the same purpose – Tim Sep 18 '17 at 09:15
  • @TimCastelijns I understand the purpose of having JAVA_HOME system variable pointing to the location of installed jdk. Which If Iam not wrong helps me run java code,even if the code is not placed in the specific path. But my question is regarding GRADLE_HOME, I dont seem to have a system variable set for GRADLE_HOME but It hasn't stopped me from adding dependencies or compilation,or anything of that sort. That kinda confuses me. – Shyamnath Mallinathan Sep 18 '17 at 09:37
  • then it's probably automatically set during installation of android studio. But the reason why you need it is the same as for java_home – Tim Sep 18 '17 at 09:39

1 Answers1

2

Let me explain by its code.Normally there is directory called wrapper under gradle. If you use JD-GUI to read gradle-wrapper.jar, you can find GradleUserHomeLookup.class

public class GradleUserHomeLookup{

  public static final String DEFAULT_GRADLE_USER_HOME = System.getProperty("user.home") + "/.gradle";
  public static final String GRADLE_USER_HOME_PROPERTY_KEY = "gradle.user.home";
  public static final String GRADLE_USER_HOME_ENV_KEY = "GRADLE_USER_HOME";

  public static File gradleUserHome(){
    String gradleUserHome;

    if ((gradleUserHome = System.getProperty("gradle.user.home")) != null) {
      return new File(gradleUserHome);
    }

    if ((gradleUserHome = System.getenv("GRADLE_USER_HOME")) != null) {
      return new File(gradleUserHome);
    }
    return new File(DEFAULT_GRADLE_USER_HOME);
  }
}
  • If gradle.user.home exists, it will be returned.
  • If there is no gradle.user.home but you have set GRADLE_USER_HOME, GRADLE_USER_HOME will be returned
  • If gradle.user.home and GRADLE_USER_HOME neither exists, DEFAULT_GRADLE_USER_HOME (Users/your-name/.gradle) will be returned.

Here is an example. In gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

Notice the last line which configures gradle's version = 3.3. In Android-Studio, gradle command is from wrapper/dists/your-version/xxxx/gradle-3.3/bin/. If there is no version, it will download gradle and install it in wrapper/dists/.... More details in Install.class.

EDIT

You can find more info here

CoXier
  • 2,523
  • 8
  • 33
  • 60