How to use the latest gradle version in Android Studio
Asked Answered
S

5

44

I've just noticed Gradle has released version 2.12 and according to the release notes the scripts should compile up to 20% faster. I'd like to upgrade to that version in Android Studio.

I'm using v1.5.1 and in the settings I've selected the "Use default gradle wrapper" option, which means that instead of using a local gradle install for every project, a specific gradle version will be used for each project. The version used is the one defined in the build.gradle file. Example:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

Now if I change that to this:

classpath 'com.android.tools.build:gradle:2.12'

then Android Studio can't find that version and throws an error. Apparently AS tries to find the binaries in a local cache first (Android Studio/gradle/m2repository) and then it tries to download it from bintray:

https://jcenter.bintray.com/com/android/tools/build/gradle/2.12/gradle-2.12.jar

Browsing the published builds it looks like the last version available here is v2.1.0-alpha1.

  • Why is v2.12 not in bintray yet? Is it not compatible with Android Studio?
  • If it were compatible, is there a way to download it and use a local install in a per-project basis? (I don't want to break older projects already in version control)
Sorkin answered 16/3, 2016 at 15:16 Comment(2)
Check this updated answerSalomo
find the latest version of Gradle that is going to use in gradle-wrapper.properties from hereSoinski
L
49

com.android.tools.build:gradle is android's plugin for gradle. It is not the same as gradle distribution. See here for release/version information of gradle android plugin: https://maven.google.com/web/index.html?q=gradle#com.android.tools.build:gradle

To change the gradle version that the plugin uses, edit the file:

<Project>/gradle/wrapper/gradle-wrapper.properties

and change this line to the gradle verison you want:

distributionUrl=http\://services.gradle.org/distributions/gradle-2.12-all.zip

Then rebuild your project.

Do keep in mind that the android plugin version you're using may not have been tested with this brand new gradle version and could potential cause unexpected issues.


android gradle plugin to Gradle version compatibility as of Feb2020

Plugin version      Required Gradle version
--                  --
1.0.0 - 1.1.3       2.2.1 - 2.3
1.2.0 - 1.3.1       2.2.1 - 2.9
1.5.0               2.2.1 - 2.13
2.0.0 - 2.1.2       2.10 - 2.13
2.1.3 - 2.2.3       2.14.1+
2.3.0+              3.3+
3.0.0+              4.1+
3.1.0+              4.4+
3.2.0 - 3.2.1       4.6+
3.3.0 - 3.3.2       4.10.1+
3.4.0 - 3.4.1       5.1.1+
3.5.0 - 3.5.3       6.0.1+
3.6.0+              6.0.1+
Langrage answered 16/3, 2016 at 15:31 Comment(3)
Is there a way to know which gradle versions are compatible with the plugin? I see nothing in the page you linked.Sorkin
Well, they do tell you what is the minimum gradle version to use, in the plugin release notes. For 1.5.0, the minimum gradle version is apparently is 2.2.1 They don't quite say how high you can go before things break. See here: developer.android.com/tools/revisions/gradle-plugin.html See also "Gradle Requirements" section here: tools.android.com/tech-docs/new-build-system/…Langrage
Bintray is no more.Barcot
W
34

For AS 2

The latest plugin is:

Download

classpath 'com.android.tools.build:gradle:[INSERT LATEST HERE]'

For AS 3

You should update the repositories to include google(), you also need gradle > 4.1 for that and the latest is then here:

https://developer.android.com/studio/releases/gradle-plugin.html

At time of writing that was:

buildscript {
    repositories {
        // Gradle 4.1 and higher include support for Google's Maven repo using
        // the google() method. And you need to include this repo to download
        // Android plugin 3.0.0 or higher.
        google()
        ...
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
    }
}
Wheelwork answered 27/10, 2016 at 14:6 Comment(5)
Nice one! Much better than 12 year old postsRichrichara
Are you sure? That image currently shows '2.3.3', whereas developer.android.com/studio/releases/gradle-plugin.html contains the text classpath 'com.android.tools.build:gradle:3.0.1'Audry
They've started using their own maven repo... I'll see what I can do.Wheelwork
@Wheelwork Do you have any idea if a newer Gradle version could be used with Android Studio than the version that it ships with? For example I tried Gradle 5.0 with AS 3.2 but it sync fails with unclear errors/exceptions. Or is Android Studio supposed to work with older versions only? I can't find any docs confirming the one or the other.Navelwort
@Navelwort I would see if it worked on command line first. e.g. ’./gradlew assembleDebug’ I would expect that you will see the very same errors.Wheelwork
U
3

What worked for me in addition to the above (changing the distibutionURL) was to keep my classpath as 2.0.0 in build.gradle (Modile:app):

classpath 'com.android.tools.build:gradle:2.0.0'

In File > Project Structure > Project: change 'Gradle version' to 2.12

In File > Settings > Build, Execution, Deployment > Build Tools > Gradle : Under 'Project-level settings' section, there are 2 options:

  1. Use default gradle wrapper (recommended)

  2. Download gradle-2.12-all.zip from http://gradle.org/post-download-gradle/ then select 'Use local gradle distibution' and map 'Gradle home:" i.e. C:/Gradle/gradle-2.12

Note that option 2 will require you to download each gradle version update.

Underline answered 8/4, 2016 at 17:23 Comment(0)
B
2

Try this: - change this url according to latest version in gradle-wrapper.properties and then change gradle.

   distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-all.zip

Hope it helps Thanks

Bailly answered 16/3, 2016 at 15:25 Comment(0)
S
2

You can't use the default gradle wrapper if you want to use the latest, you have to go it the settings, check "Use customizable gradle wrapper", then change in

Project/gradle/wrapper/gradle-wrapper.properties

the distributionUrl value

distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-bin.zip

Gradle Settings

Shannan answered 16/3, 2016 at 15:27 Comment(5)
I'm a bit lost here. I can't see any "Use customizable gradle wrapper" option in the gradle settings. Do you mean "Use local gradle distribution" by any chance?Sorkin
Nope, took a screenshot and it's there.Shannan
You must be using AS 2 beta. I'm still on 1.5.1Sorkin
Just using plain IntelliJ, actually.Shannan
So this option uses a custom gradle version but only for the project, right?Sorkin

© 2022 - 2024 — McMap. All rights reserved.