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 and gradle-wrapper.properties)? Some times I see other *.gradle files inside the project's gradle 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 inside gradle.properties?
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 to build.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: task wrapper(type: Wrapper) { gradleVersion = '2.0' //version required } and run: gradle wrapper 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: gradle wrapper --gradle-version 2.3 or gradle wrapper --gradle-distribution-url https://myEnterpriseRepository:7070/gradle/distributions/gradle-2.3-bin.zip 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.)
Generating the Gradle Wrapper
Project build gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
// Running 'gradle wrapper' will generate gradlew - Getting gradle wrapper working and using it will save you a lot of pain.
task wrapper(type: Wrapper) {
gradleVersion = '2.2'
}
// Look Google doesn't use Maven Central, they use jcenter now.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
Then at the command-line run
gradle wrapper
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.
brew install gradle
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.
./gradlew assemble
What about the gradle plugin seen above?
com.android.tools.build:gradle:1.0.1
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.
gradleVersion = '2.2'
in taskWrapper
can use whatever version I desire for generating the wrapper, correct? Does not have to be the latest version of gradle?
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 your build.gradle
file. The next time you use the wrapper, it will download the appropriate Gradle distribution to match.
This is the command to use to tell Gradle to upgrade the wrapper such that it will grab the distribution versions of the Gradle jars that includes source code:
./gradlew wrapper --gradle-version <version> --distribution-type all
Specifying the distribution-type with "all" will make sure Gradle downloads source files for itself that can be used by your development environment.
Pros:
IDEs will have immediate access to source code of Gradle. For example, Intellij IDEA won't prompt you to update your build scripts to include the source distro (because this command already did that)
Cons:
Longer/Bigger build process because it's downloading Gradle source code. This is a waste of time/space on a build or CI server where the source code is not necessary (110MB vs 150MB for Gradle 7.4).
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.
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:
task wrapper(type: Wrapper) {
gradleVersion = '2.13'
distributionUrl = distributionUrl.replace("bin", "all")
}
As gradle built-in tasks is deprecated in 4.8, try below
wrapper {
gradleVersion = '2.0' //version required
}
and run
gradle wrapper
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: $
Success story sharing
build.gradle
script into multiple shorter, and devoted to one particular purpose scripts. As You can the script located under gradle dir a applied to mainbuild.gradle
file, view: github.com/Netflix/eureka/blob/master/build.gradle. Where You put such modules is of Your choice. That's all in general. If You're satisfied with the answer please accept the answer :)--distribution-type
, e.g.,gradle wrapper --gradle-version 4.3.1 --distribution-type ALL