Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is gradle wrapper and the gradlew.bat file?

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.

like image 793
Abhishek Kumar Avatar asked Jul 01 '17 11:07

Abhishek Kumar


People also ask

What is gradle and gradlew bat?

gradlew and gradlew. bat: This is the gradle wrapper which is used the run various gradle tasks and Commands. gradlew works in Linux and MacOS and gradlew. bat is used for Windows. settings.gradle: This file indicates which projects to include in the build.

What is difference between gradle and gradlew?

The difference lies in the fact that ./gradlew indicates you are using a gradle wrapper. The wrapper is generally part of a project and it facilitates installation of gradle.

What is the use of gradlew file?

Overview. Gradle is commonly used by developers to manage their project's build lifecycle. It's the default choice of build tool for all new Android projects.

Do I need gradle to run gradlew?

1. No need to install gradle locally. The gradlew script doesn't rely on a local Gradle installation. It goes and fetches a Gradle installation from the internet the first time it runs on your local machine, and caches it.


1 Answers

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), not are you currently in your root projects 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 any project should use the wrapper if it uses Gradle.

like image 112
Vampire Avatar answered Sep 24 '22 08:09

Vampire