-->

How to use the latest gradle version in Android St

2019-01-23 02:53发布

问题:

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)

回答1:

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://bintray.com/android/android-tools/com.android.tools.build.gradle/view

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.



回答2:

For AS 2

The latest plugin is:

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'
    }
}


回答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.



回答4:

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



回答5:

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