262

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:

  1. 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?
  2. Same question above, but for the gradle/wrapper/* files (gradle-wrapper.jar and gradle-wrapper.properties)?
  3. 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?
  4. What is the difference in properties that go into settings.gradle vs what should be defined inside gradle.properties?

12 Answers 12

311
  1. 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.

  1. 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.

  2. Give a detailed example. Such file may have multiple purposes: multi-module project, responsibility separation, slightly modified script, etc.

  3. 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.)

6
  • Thanks @Opal (+1) - to give you a specific example for #3, see Netflix-Eureka. What are these Gradle files?!?
    – smeeb
    Commented Sep 10, 2014 at 16:18
  • Ok, I see it. As far as I see, developers from Netflix just divided one big 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 main build.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 :)
    – Opal
    Commented Sep 11, 2014 at 8:39
  • 2
    Another useful command-line option is --distribution-type, e.g., gradle wrapper --gradle-version 4.3.1 --distribution-type ALL
    – heenenee
    Commented Nov 15, 2017 at 1:35
  • Tip: Android Studio has a Gradle in its installation folder.
    – TWiStErRob
    Commented Jan 4, 2018 at 1:52
  • after install gradle I get this error java: symbol lookup error: /usr/lib/jni/libnative-platform-curses.so: undefined symbol: tgetent do you have any idea about it ..
    – Mina Samir
    Commented Feb 1, 2018 at 15:01
33

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.

2
  • 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?
    – raffian
    Commented Feb 15, 2015 at 22:39
  • 1
    I suppose you can go back in the 2-series of gradle. I'm not at all sure about before 2.x, but no, it doesn't need to follow the absolute latest version. Commented Feb 16, 2015 at 9:56
22

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.

0
22

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:

  • 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.

1
  • 3
    Are you sure this setting is about libraries? The official docs say otherwise and refer to the wrapper itself only: "The type of the Gradle distribution to be used by the wrapper." docs.gradle.org/current/dsl/… Commented Dec 18, 2018 at 17:41
10

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")
}
1
  • 1
    With more recent builds of gradle (not sure exactly which one) - rather than manually rewriting the url, it's probably best to use the inbuilt command in the same closure: distributionType = Wrapper.DistributionType.ALL
    – Dave Birch
    Commented Feb 6, 2018 at 13:11
5

As gradle built-in tasks is deprecated in 4.8, try below

wrapper {
   gradleVersion = '2.0' //version required
}

and run

gradle wrapper
2
  1. 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.

  2. Those are all part of the wrapper.

  3. 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.

  4. 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.

2

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.

1

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!

1

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
0

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
1
0
  • Just remove gradle folder and sync project, it will generate full gradle folder with wrapper folder including: -- gradlle->wrapper->[[gradle-wrapper.jar, gradle-wrapper.properties]] -- Happy coding.....:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.