Reputation: 29497
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:
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?gradle/wrapper/*
files (gradle-wrapper.jar
and gradle-wrapper.properties
)?*.gradle
files inside the project's gradle
directory. What are these additional Gradle files and what do they represent/do? Custom plugins?settings.gradle
vs what should be defined inside gradle.properties
?Upvotes: 263
Views: 269578
Reputation: 141
Update to the latest version.
build.gradle
wrapper.doFirst {
def versionService = new URL('https://services.gradle.org/versions/current')
gradleVersion = new groovy.json.JsonSlurper().parseText(versionService.text).version
}
Terminal
gradlew wrapper
Upvotes: 1
Reputation: 842
Answer to the first question (For Mac users):
I recently needed gradlew
and gradlew.bat
files as I created an Android library and I needed to publish it.
For this you need Gradle on your machine. I installed it using the command below (Install Homebrew first if you don't have it):
brew install gradle
Once Gradle is installed, you can use following command to initialize(or update) the gradle wrapper in your project:
gradle wrapper
That's it. Your gradlew
and gradlew.bat
files are generated.
Upvotes: 2
Reputation: 1
Upvotes: 0
Reputation: 24502
This is another syntax for Kotlin DSL (build.gradle.kts) tested with Gradle 8.2:
tasks.wrapper {
gradleVersion = 8.2 // OR, for example, properties["gradle.version"] as String
networkTimeout = 60_000 // milliseconds
distributionType = Wrapper.DistributionType.ALL // Includes JavaDoc and source code
validateDistributionUrl = false
}
Then, generate/update the wrapper with this command:
./gradlew wrapper
Upvotes: 1
Reputation: 545
If you are using Gradle version 7.3.3 or higher all you have to do is simply open terminal and run:
gradle wrapper
And that' it!
Upvotes: 1
Reputation: 21436
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 (just Gradle, not libraries) that includes source code:
./gradlew wrapper --gradle-version <version> --distribution-type all
This can now be done using the Gradle build itself, change your root build.gradle
to contain:
tasks.named('wrapper') {
distributionType = Wrapper.DistributionType.ALL
gradleVersion = '7.5'
}
Now to upgrade the gradle wrapper: just change the code to the new version and run the wrapper
task without any params.
Specifying the distribution-type with "all" will make sure Gradle downloads source files for itself that can be used 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.
Upvotes: 22
Reputation: 623
As gradle built-in tasks is deprecated in 4.8, try below
wrapper {
gradleVersion = '2.0' //version required
}
and run
gradle wrapper
Upvotes: 5
Reputation: 84756
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.)
Upvotes: 311
Reputation: 9154
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")
}
Upvotes: 10
Reputation: 45321
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:
$ <pathToExpandedZip>/bin/gradle wrapper
No need to define a task, though you probably need some kind of build.gradle
file.
This will update or create the gradlew
and gradlew.bat
wrapper as well as gradle/wrapper/gradle-wrapper.properties
and the gradle-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.
Upvotes: 2
Reputation: 15679
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.
Upvotes: 22
Reputation: 22245
// 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
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.
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.
Upvotes: 33