This tutorial is going to cover shortly about Gradlew – the Gradle Wrapper, how to use it, and how to create it for our projects.

Gradle
1. Introduce about the Gradle Wrapper
Firstly, let’s review what will we have to do when we have a Gradle project
- Download and install Gradle if we didn’t have it –> May take time and install not right version for the build.
- Use the existing version if we had it –> The existing may be not the right version for the the build.
Gradle provides Gradlew, the Gradle Wrapper, will help us resolve the above problems.
So, what will Gradle Wrapper do?
Generally, it will:
- Allow us to define the desire version of Gradle in configuration file.
- When users use the Wrapper to build, it’ll check for the right version of Gradle in user machine. If not found, it will download Gradle automatically and use for the build.
2. Use Gradle Wrapper to build a project
To use Gradle Wrapper to build a project, we need to make sure it was added to the project first. We can do that by looking for the gradlew and related in the root project directory.
Let’s assume that the project has been set up the wrapper, we can execute the build using the following command:
On Linux and Mac OSX:
1 |
./gradlew <task> |
On Windows
1 |
gradlew <task> |
For example:
1 |
./gradlew test |
If the desire version of Gradle is not found, the Wrapper will download Gradle automatically at the first time and then cache it in the local for the next times. You can refer to the following article for Gradle Cache Location
3. Add Gradle Wrapper to a project
There are 2 approaches for us to add Gradle Wrapper to a project.
3.1. Running the wrapper task
1 |
gradle wrapper |
1 2 3 4 5 6 7 |
> gradle wrapper --gradle-version 3.1 Starting a Gradle Daemon (subsequent builds will be faster) :wrapper BUILD SUCCESSFUL Total time: 15.178 secs |
To specify a Gradle version, we can use the –gradle-version option as below:
1 |
gradle wrapper --gradle-version 3.0 |
3.2. Defining the wrapper task in the build.gradle
Add the below task in the build.gradle file.
1 2 3 |
task wrapper(type: Wrapper) { gradleVersion = '3.1' } |
Then we execute the task:
1 |
gradle wrapper |
We will get the same result likes the first approach.
3.3. TheĀ wrapper generated files
After we run the Gradle command to add the wrapper to our project, there are files generated in the project directory
1 2 3 4 5 6 |
your_project/ gradlew gradlew.bat gradle/wrapper/ gradle-wrapper.jar gradle-wrapper.properties |
Those files are used for Wrapper and should be kept in the project and checked in source control system also.
4. Summary
We have learned about to use Gradle Wrapper to build a project and how to add Gradle Wrapper to a project as well . Even it takes a little bit time at the first time for downloading the Gralde, it’s simplify the build for all Gradle project in general. Below are other articles related to Gradle for your reference:
Install Gradle on Ubuntu 16.04 LTS (Xenial Xerus)
Where is Gradle Cache Location
Set Java Source Compatibility and Target Compatibility in Gradle
Refresh or Redownload Dependencies in Gradle