Gradle Wrapper

Most tools, when being used, requires installation on a computer before using them. If the installation is easy, then anyone can install it, but the installation of any tool on a computer becomes an unnecessary burden on the users of the build. It is equally important whether the user has installed the right version, the user may install any old version of the software. The Gradle wrapper solves all the problem, and it is preferred by all the developers to start a Gradle build.

Executing a build with the Wrapper

For executing a build with the wrapper, the Gradle project should set up the wrapper. Following are the commands from the root of the project:

  • Unix: ./gradlew<task>
  • Windows: gradlew<task>

Each version of Gradle has a specific wrapper, when you run any one of the above commands for a given Gradle version. The corresponding Gradle distribution is downloaded and then used for the execution of the build. Using this, you don’t need to install Gradle by yourself.  Also, the version of Gradle will be accurate for which the build is designed. This makes it more reliable.

Following are the list of files and directories in a Gradle project which is used to make up the wrapper:

  • Unix shell script: gradlew
  • Windows batch file: gradlew.bat
  • Wrapper JAR: gradle/wrapper/gradle-wrapper.jar
  • Wrapper properties: gradle/wrapper/gradle-wrapper.properties

Note: Gradle distribution is stored in user home directory under $USER_HOME/.gradle/wrapper/dists

Adding the Wrapper to a project

The wrapper can be check-in version control. Distributing wrapper in a project can work without installing Gradle. Even the user will be guaranteed for the version of Gradle for which the build was designed to work. It is good for continuous integration servers because it does not require any configuration on the server.

To install the wrapper in our project, we have to run the wrapper task. To specify the Gradle version - -gradle-version command is used on the command line. By default, bin distribution is used by the wrapper. This distribution is the smallest distribution of Gradle. Android Studio and Intellij IDEA has additional context information when used with all the distributions. For selecting different Gradle distribution - -distribution-type is used. To download Gradle directly using URL –gradle-distribution-url is used. If version and distribution URL is not specified, then wrapper will be configured to use the Gradle version using which wrapper task will be executed.

Example:

Running wrapper task

Output:

E:\project2>gradle wrapper --gradle-version 2.0
BUILD SUCCESSFUL in 1s
1 actionable task: 1 up-to-date 

Example:

taskwrapper1(type: Wrapper) {
     gradleVersion = '2.0'
   }

Run the project on command line

 >>gradle tasks  

Output will be the following file structure

Gradle Wrapper

All the files are submitted to the control system only once. After these, all files are added in the project, now to build the project, a gradlew command is added. The gradlew command is used in the same way as Gradle command. For switching into a new version of Gradle, user doesn’t need to return the wrapper task.

Configuration

When gradlew command is run on Gradle, then the wrapper checks for Gradle distribution are available for wrapper or not. If it is available, then delegates the Gradle command with all the arguments which are passed originally to the gradlew command. Else it is not available, then it will download it.

While configuring the wrapper task, the Gradle version can also be specified. The gradlew command will download all the distribution from the Gradle repository, or you can also use URL to download the distribution. If the version or the URL for downloading is not specified, then the gradlew command will automatically download any version of Gradle. Then, it can be used to generate the wrapper files.

If you want that there should be no download to happen when your project is built via gradlew, then simply add the Gradle distribution zip in the version control at the location specified by your wrapper configuration. If you build via the wrapper then any Gradle distribution installed on the machine is ignored.

Authenticated Gradle distribution download

The Gradle wrapper can also download Gradle distribution from the servers using HTTP Basic Authentication. This helps to host the gradle distribution on the private protected server. Username and password can be specified in two different ways, they are as follows:

  1. System property
  2. Directly embedded in the distribution URL

The system property is done at the .gradle/gradle.properties file in the user’s home directory.

Example of Specifying the HTTP Basic Authentication credentials using system property

gradle.properties

systemProp.gradle.wrapperUser=username 
systemProp.gradle.wrapperPassword=password 

The distributionUrlis done at the gradle/wrapper/gradle-wrapper.properties file

Example of HTTP Basic Authentication credentials in distributionUrl

gradle-wrapper.properties.

distributionUrl=https://username:password@somehost/path/to/gradle-distribution.zip

Verification of downloaded Gradle distributions

The Gradle wrapper allows you to verify the downloaded Gradle distribution using the SHA-256 hash sum comparison. It is used to increase the security from targeted attacks, and also it prevents man-in-the-middle attackers from crime during downloading Gradle distribution.

For enabling this feature, .sha256 file is downloaded.