I am trying to understand how the Gradle Wrapper works. In many source repos, I see the following structure:
projectRoot/
src/
build.gradle
gradle.properties
settings.gradle
gradlew
gradlew.bat
gradle/
wrapper/
gradle-wrapper.jar
gradle-wrapper.properties
My questions:
- How/when does one generate
gradlew
/gradlew.bat
? Are you supposed to generate them only one time when the project is first created, do you generate them every time you commit/push changes? And how are they generated? - Same question above, but for the
gradle/wrapper/*
files (gradle-wrapper.jar
andgradle-wrapper.properties
)? - Some times I see other
*.gradle
files inside the project'sgradle
directory. What are these additional Gradle files and what do they represent/do? Custom plugins? - What is the difference in properties that go into
settings.gradle
vs what should be defined insidegradle.properties
?
Generating the Gradle Wrapper
Project build gradle
Then at the command-line run
If you're missing gradle on your system install it or the above won't work. On a Mac it is best to install via Homebrew.
After you have successfully run the wrapper task and generated
gradlew
, don't use your system gradle. It will save you a lot of headaches.What about the gradle plugin seen above?
You should set the version to be the latest and you can check the tools page and edit the version accordingly.
See what Android Studio generates
The addition of gradle and the newest Android Studio have changed project layout dramatically. If you have an older project I highly recommend creating a clean one with the latest Android Studio and see what Google considers the standard project.
Android Studio has facilities for importing older projects which can also help.
You generate it once, and again when you'd like to change the version of Gradle you use in the project. There's no need to generate is so often. Here are the docs. Just add
wrapper
task tobuild.gradle
file and run this task to get the wrapper structure.Mind that you need to have Gradle installed to generate a wrapper. Great tool for managing g-ecosystem artifacts is SDKMAN!. To generate a gradle wrapper, add the following piece of code to
build.gradle
file:and run:
task. Add the resulting files to SCM (e.g. git) and from now all developers will have the same version of Gradle when using Gradle Wrapper.
With Gradle 2.4 (or higher) you can set up a wrapper without adding a dedicated task:
or
All the details can be found here
From Gradle
3.1
--distribution-type
option can be also used. The options are binary and all and bin. all additionally contains source code and documentation. all is also better when IDE is used, so the editor works better. Drawback is the build may last longer (need to download more data, pointless on CI server) and it will take more space.These are Gradle Wrapper files. You need to generate them once (for a particular version) and add to version control. If you need to change the version of Gradle Wrapper, change the version in
build.gradle
see (1.) and regenerate the files.Give a detailed example. Such file may have multiple purposes: multi-module project, responsibility separation, slightly modified script, etc.
settings.gradle
is responsible rather for structure of the project (modules, names, etc), while,gradle.properties
is used for project's and Gradle's external details (version, command line arguments-XX
, properties etc.)You will generate them once, but update them if you need a new feature or something from a plugin which in turn needs a newer gradle version.
Easiest way to update: as of Gradle 2.2 you can just download and extract the complete or binary Gradle distribution, and run:
No need to define a task, though you probably need some kind of
build.gradle
file.This will update or create the
gradlew
andgradlew.bat
wrapper as well asgradle/wrapper/gradle-wrapper.properties
and thegradle-wrapper.jar
to provide the current version of gradle, wrapped.Those are all part of the wrapper.
Some
build.gradle
files reference other files or files in subdirectories which are sub projects or modules. It gets a bit complicated, but if you have one project you basically need the one file.settings.gradle
handles project, module and other kinds of names and settings,gradle.properties
configures resusable variables for your gradle files if you like and you feel they would be clearer that way.As of Gradle 2.4, you can use
gradle wrapper --gradle-version X.X
to configure a specific version of the Gradle wrapper, without adding any tasks to yourbuild.gradle
file. The next time you use the wrapper, it will download the appropriate Gradle distribution to match.If you want to download gradle with source and docs, the default distribution url configured in gradle-wrapper.properites will not satisfy your need.It is https://services.gradle.org/distributions/gradle-2.10-bin.zip, not https://services.gradle.org/distributions/gradle-2.10-all.zip.This full url is suggested by IDE such as Android Studio.If you want to download the full gradle,You can configure the wrapper task like this:
This is the command to use to tell Gradle to upgrade the wrapper such that it will grab the distribution versions of libraries that includes source code:
Specifying the distribution-type with "all" will make sure Gradle downloads source files for use by your development environment.
Pros:
Cons:
Please comment or provide another answer if you know of any command line option to tell Gradle not to download sources on a build server.