1. 程式人生 > >關於gradle 與gradle plugin 版本問題

關於gradle 與gradle plugin 版本問題

可以參考谷歌官方文件:  https://developer.android.google.cn/studio/releases/gradle-plugin.html#

有的時候在github上下載的demo會出現無法找到gradle中的某個方法,或者提示gradle外掛的某個版本找不到的問題.

這類問題一般都是因為gradle 的版本,與gradle外掛的版本(android plugin for gradle)不一致導致.

The Android Studio build system is based on Gradle, and the Android pluginfor Gradle adds several features that are specific to building Androidapps. Although the Android plugin is typically updated in lock-step with AndroidStudio, the plugin (and the rest of the Gradle system) can run independent ofAndroid Studio and be updated separately.

This page explains how to keep your Gradle tools up todate and what's in the recent updates.

For details about how to configure your Android builds with Gradle,see the following pages:

For more information about the Gradle build system, see the Gradle user guide.

Update the Android Plugin for Gradle

When you update Android Studio, you may receive a prompt to automatically update the Android plugin for Gradle to the latest available version. You can choose to accept the update or manually specify a version based on your project's build requirements.

You can specify the Android plugin for Gradle version in either the File > Project Structure > Project menu in Android Studio, or the top-level build.gradle file. The plugin version applies to all modules built in that Android Studio project. The following example sets the Android plugin for Gradle to version 3.0.1 from the build.gradle

file:

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

Caution: You should not use dynamic dependencies in version numbers, such as 'com.android.tools.build:gradle:2.+'. Using this feature can cause unexpected version updates and difficulty resolving version differences.

If the specified plugin version has not been downloaded, Gradle downloads it the next time you build your project or click Tools > Android > Sync Project with Gradle Files from the Android Studio menu bar.

Update Gradle

When you update Android Studio, you may receive a prompt to also update Gradle to the latest available version. You can choose to accept the update or manually specify a version based on your project's build requirements.

The following table lists which version of Gradle is required for eachversion of the Android plugin for Gradle. For the best performance, you shoulduse the latest possible version of both Gradle and the Android plugin.

Plugin versionRequired Gradle version
1.0.0 - 1.1.32.2.1 - 2.3
1.2.0 - 1.3.12.2.1 - 2.9
1.5.02.2.1 - 2.13
2.0.0 - 2.1.22.10 - 2.13
2.1.3 - 2.2.32.14.1+
2.3.0+3.3+
3.0.0+4.1+

You can specify the Gradle version in either the File > Project Structure > Project menu in Android Studio, or by editing the Gradle distribution reference in the gradle/wrapper/gradle-wrapper.properties file. The following example sets the Gradle version to 4.1 in the gradle-wrapper.properties file.

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