31

Lately I came to know the power of Gradle as a build system and as an Android developer I wanna understand it deeply.

One article said the following:

You can execute all the build tasks available to your Android project using the Gradle wrapper command line tool. It's available as a batch file for Windows (gradlew.bat) and a shell script for Linux and Mac (gradlew.sh), and it's accessible from the root of each project you create with Android Studio.

To run a task with the wrapper, use one of the following commands:

  • On Windows:
    gradlew task-name
    

Now I have some doubts which goes as follow:

  1. What is Gradle Wrapper and gradlew.bat?
  2. If I've got Android studio installed and it is using gradle to build my apps (so gradle is already installed on my system), do I still need to install gradle for build purpose from command line? As when i write any commend like gradle, gradlew on my command line I get error saying gradlew is not recognized as internal or external command (the same error for other commands). I may be using it on wrong path, help me on what path do I need to use Gradle related command?
  3. If I need to download and install it, how and where can I find the file? And the other processes?

I am using a Windows machine for this.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Abhishek Kumar
  • 4,532
  • 5
  • 31
  • 53
  • Possible duplicate of [How/when to generate Gradle wrapper files?](https://stackoverflow.com/questions/25769536/how-when-to-generate-gradle-wrapper-files) – Lukas Körfer Jul 01 '17 at 11:42

3 Answers3

45

The Gradle Wrapper is an optional part of the Gradle build system. It consists of four files that you check into version control system. The Unix start script <your root project>/gradlew, the <your root project>/gradlew.bat Windows start script, <your root project>/gradle/wrapper/gradle-wrapper.jar which contains the class files for the wrapper and is started by the start scripts and <your root project>/gradle/wrapper/gradle-wrapper.properties which contains some configuration for the wrapper, for example which Gradle version to use to build the project.

In my opinion, each and every Gradle project, even the tiniest, should make use of the Gradle wrapper.

The Gradle wrapper makes sure your build is always run with the same Gradle version, no matter who executes the build and where or that Gradle is installed or not, as long as the one uses the Gradle wrapper to build the project. This means you can design your build for that Gradle version and be sure that the build will not fail, just because someone is using a different version of Gradle and thus is also an important step in build reproducibility.

Also, someone wishing to build your project only needs to have Java installed and that's it. He does not need to have any Gradle version installed. Actually any already installed Gradle version is ignored. The Gradle wrapper checks whether in ~/.gradle/ the version that is necessary for the build is already present, because some Gradle wrapper of any project put it there already. If it is present already, it is used, otherwise it is automatically downloaded.

If you type gradlew on the commandline and the command is not found, that means you didn't put your root projects path to the PATH environment variable (I wouldn't recommend doing that either), nor are you currently in your root project's directory. To run a Gradle build, you have to be anywhere inside your project and call Gradle or the Gradle wrapper. But like with any executable file that is not on the path, you have to provide its path of course. So if you are in your root project directory, you can simply do gradlew. if you are in <root project dir>/foo/bar/, you would need to call ../../gradlew.

The Gradle Wrapper files are generated by the implicitly available Gradle task wrapper and then get checked into the VCS of the project in question. If those four files are not present for a project, it does not use the Gradle wrapper and you should post an improvement request to the project to add it.

If some project does not use the Gradle wrapper, but builds with Gradle, you can either install Gradle and use gradle instead of gradlew, or you can even call the Gradle wrapper of any other project that you have available on disk. The build will then be run with the Gradle version that wrapper or Gradle installation is using and thus might not behave as expected, which is why really each and every project should use the wrapper if it uses Gradle.

Vampire
  • 35,631
  • 4
  • 76
  • 102
  • very detailed answer! i have a few questions though. 1) how can jdk know how to bundle all the modules, resources, dependencies and manifest classes without using gradle (as you said only java needs to be installed to build code)? i believe gradle is necessary to be installed as a software since it combines all the assoc dependencies, right? (2) who initiates the gradle download for version that is mentioned in the `gradle.properties` ? is it Android studio? or is it JDK? (3) why `gradlew`/ `gradlew.bat` are kept in root and not alongside `gradle-wrapper.jar` – ansh sachdeva Oct 29 '22 at 17:02
  • (4) if `gradle-wrapper.jar` is a part of most of the programs, what exactly is downloaded as 100-150mb file ,when a project is launched ? (5) what exactly does the start scripts (`gradlew` and `gradlew.bat` ) *start* ? if you can expand more on your answer, it will be wonderful – ansh sachdeva Oct 29 '22 at 17:06
  • You should read my aber again, most of your questions should already be covered. As quick summary: 1. Gradle is needed to build, but it does not need to be installed. The Gradle wrapper is a tiny Java program checked in with your code that takes care of downloading and using the correct Gradle version for the build and to start that, only Java is necessary to be installed. 2. If you build from the commands line using the Gradle wrapper, it is the Gradle wrapper. If you build from AS, it is the Gradle tooling API bundled with AS. – Vampire Oct 30 '22 at 10:30
  • 3. Because it would be awful to always have to use `gradle/wrapper/gradlew` to build from the command line? 4. Gradle 5. The Gradle wrapper – Vampire Oct 30 '22 at 10:31
3

Edited after comments

Gradle is a build system.

This gradle-wrapper is kind of the primary interface to to build Android projects. It the part of Gradle-build-system and does some primary check if gradle in installed or not.

gradlew.bat - its a batch file used on Windows. You can even open it with a notepad to view the instructions in it. Batch files are like 'commands' written in a file to be executed. You use it (in case of Windows) to execute build commands. It also checks if gradle is installed or not. And in case it is not, it downloads and installs it.

Example : to build android app on Windows:

  • Open command prompt
  • Navigate to your project's root directory
  • execute gradlew.bat assembleDebug

It starts the wrapper, checks if Gradle is installed there and executes all the 'gradle specific' commands to build your project.

Do you need to install Gradle ?

Actually, no. Its the job of this gradlew script to check for that. If gradle its not already there, it would automatically download it and use it for all later builds.

zeekhuge
  • 1,594
  • 1
  • 13
  • 23
  • whenever I write gradlew or gradle or any gradle related command , I get error . See the edited post . – Abhishek Kumar Jul 01 '17 at 11:40
  • where can I get this wrapper ? – Abhishek Kumar Jul 01 '17 at 11:40
  • what are you using ? Windows ? or linxu/mac ? – zeekhuge Jul 01 '17 at 11:41
  • I am using windows – Abhishek Kumar Jul 01 '17 at 11:43
  • I went to the path of one of my project and then typed gradlew , now it downloaded something . I don't know what it downloaded as I read that if only you already don't have gradle installed then only this command install it for you . Now I've android studio installed , means gradle installed , so why does it download ? – Abhishek Kumar Jul 01 '17 at 12:01
  • Gradle does not come installed with Android Studio. Gradle is a different system. Or even if its installed, it might not be accessible. The gradle-wrapper (gradlew) is responsible for downloading and installing Gradle if its not already there. Gradle further might download the project dependencies. – zeekhuge Jul 01 '17 at 12:04
  • 4
    This answer is completely wrong. The Gradle wrapper is part of the Gradle build system and is also not Android specific. It also does not make the building easier, except that you don't need to have Gradle installed and then your build runs with the exact Gradle version the build is designed for. The "instructions" you write are identical to not using the wrapper. `gradlew.bat` is not an interface to the wrapper, but part of the wrapper. And the wrapper also does not care about conventionally installed Gradle versions, it just ignores them. – Vampire Jul 01 '17 at 13:24
1
  1. gradlew.bat IS the Gradle Wrapper (for Windows in this case). Gradle Wrapper is just a small utility that will ensure that Gradle is installed (or install it if necessary) so you can always build the project. Gradle itself allows you to manage dependencies and build configurations for your project.

  2. If you have installed Android Studio, you have Gradle installed and are good to go. (Technically, each project will have it's own wrapper to handle installing/using Gradle)

  3. As I mentioned above, you are good to go.

In the end Gradle is a command line tool that you use to build your project and you could very well use that directly (though you don't have to) since it is exactly what Android Studio uses to build your project.

RobertoCuba
  • 881
  • 9
  • 25
  • whenever I write gradlew or gradle or any gradle related command , I get error . See the edited post . – Abhishek Kumar Jul 01 '17 at 11:40
  • gradlew is at the root of your project so you need to be in that folder to run it. – RobertoCuba Jul 01 '17 at 11:44
  • 2
    This answer is completely wrong. `gradlew.bat` is *not* the wrapper, it is one of four files which are part of the wrapper. The wrapper does not care about conventionally installed Gradle versions, but always only uses wrapper-managed Gradle versions. – Vampire Jul 01 '17 at 13:27
  • Ok, yeah, I guess you could say that the wrapper is 4 files, 2 of which are the same (just for different OS's) and two which are used by the first 2. I went with the fact that the bat (or sh) file is how you use the wrapper, and I am not sure if making that distinction would have helped Abhishek. I'm not sure if that means I'm "completely wrong," though. – RobertoCuba Jul 01 '17 at 13:44
  • well, it was not the only error in your answer and by telling people that the start scripts **are** the wrapper, they can tend to only checkin those files which are useless alone. I've seen such a project a couple of days ago at last. ;-) – Vampire Jul 01 '17 at 16:38
  • I don't think this is the place for a lecture on Gradle. Literally all the information in your answer is on both the Android docs and Gradle's own docs. OP just didn't do any research it would seem. And just like you said in your reply to him, he should be coming in with concrete questions, not just "help me understand Gradle". Anyway, I still stand by my answer in the context of his question (i.e. Gralde(w) as it relates to Android Studio), though I clarified the second point if that will satisfy you (though I doubt it). – RobertoCuba Jul 01 '17 at 17:00
  • He didn't really ask about Gradle together with AS. He just mentioned he has AS installed to aks whether that is enough for having the wrapper installed.The question is about using the wrapper on the commandline. :-) – Vampire Jul 01 '17 at 17:03
  • I guess the tags, mentioning Android development, the quote, and the second question about whether AS comes with Gradle were just fluff then... ok, my bad. – RobertoCuba Jul 01 '17 at 17:09