什么是Gradle?
Gradle是一個(gè)基于?Apache Ant?和?Apache Maven?概念的項(xiàng)目自動(dòng)化構(gòu)建工具,支持依賴管理和多項(xiàng)目,類似?Maven,但比之簡(jiǎn)單輕便。它使用一種基于?Groovy?的特定領(lǐng)域語言來聲明項(xiàng)目設(shè)置,而不是傳統(tǒng)的?XML。
對(duì)于Android開發(fā)的小伙伴,可以參考Gradle 完整指南(Android)。
什么是Android Gradle plugin?
Android Gradle Plugin是Google為Android Studio引入Gradle構(gòu)建系統(tǒng),開發(fā)的一系列插件。
Android Studio經(jīng)常性的升級(jí),在升級(jí)時(shí)還不忘提醒用戶,讓你感受新的功能和性能提升。比如它又建議我:
```
To take advantage of all the latest features (such as Instant Run), improvements and security fixes, we strongly recommend that you update the Android Gradle plugin to version 3.1.2 and Gradle to version 4.4. You can learn more about this version of the plugin from the release notes.
```
如果你每次都遵從建議的話,卻不明白原理的話,你可能會(huì)中招。
中招1:版本不一致導(dǎo)致的基本問題
錯(cuò)誤提升信息:
Minimum supported Gradle version is 3.3. Current version is 3.2.1.
Please fix the project's Gradle settings.Fix Gradle wrapper and re-import project Gradle settings
或
Gradle sync failed: Minimum supported Gradle version is 4.4. Current version is 4.1. If using the gradle wrapper, try editing the distributionUrl in /work/mapboom/mapboom-gl-native-android/platform/android/gradle/wrapper/gradle-wrapper.properties to gradle-4.4-all.zip
原因是版本不一致導(dǎo)致的。解決辦法就是保持正確的Gradle和Android Gradle Plugin的對(duì)應(yīng)關(guān)系。
Gradle和Android gradle plugin對(duì)應(yīng)關(guān)系如下:
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+
解決辦法:
1. 修改Gradle版本:?
*? 方法1:
????* 在android-studio/gradle目錄直接刪除前一版本,如gradle-3.2.1
????*?訪問gradle發(fā)布地址下載新版本的gradle,如gradle-3.3。
????*?解壓到android-studio/gradle目錄
*? 方法2:
在Android Studio中打開Project Structure對(duì)話框,點(diǎn)擊Project,指定Gradle version和Android Plugin Version。修改前需確保Gradle相應(yīng)版本在目錄android-studio/gradle中,Android Plugin Version相應(yīng)版本在目錄android-studio/gradle/m2repository/com/android/tools/build/gradle中。
驗(yàn)證:
在android-studio重新打開工程時(shí),會(huì)自動(dòng)生成app\gradle\wrapper\gradle-wrapper.properties文件,打開該文件進(jìn)行查看:
```
#Thu Jun 07 14:46:24 CST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
```
如果一直無法下載(說明你的網(wǎng)絡(luò)環(huán)境有問題),可能會(huì)提示:
Error:Could not run build action using Gradle distribution 'https://services.gradle.org/distributions/gradle-4.1-all.zip'.
這是可以考慮通過瀏覽器下載后,放入下述文件夾:
/home/brian/.gradle/wrapper/dists/gradle-3.3-all\? ?//Linux
C:\Users\用戶名\.gradle\wrapper\dists\gradle-3.3-all\的文件夾下(注意這里不要解壓縮) ?//Windows?
我的機(jī)器上有多個(gè)gradle版本:
```
gradle-3.2.1-all?
gradle-4.1-all?
gradle-4.8-all
gradle-3.3-all\55gk2rcmfc6p2dg9u9ohc3hw9\...? ?
gradle-4.4-all
```
2. 修改Android Gradle Plugin版本
Android Gradle Plugin目錄在: android-studio/gradle/m2repository/com/android/tools/build/gradle
一般有多個(gè)版本,如2.3.2/3.0.1/2.2.0
可以在build.gradle文件中指定當(dāng)前工程使用的Android gradle plugin版本:
app\build.gradle文件
```
dependencies {
? ? ? ? classpath 'com.android.tools.build:gradle:3.1.2'
? ? }
```
中招2:版本不一致導(dǎo)致的其他問題
Gradle sync failed: Cannot set the value of read-only property 'outputFile' for object of type com.android.build.gradle.internal.api.LibraryVariantOutputImpl.
Could not set unknown property 'outputFileName' for object of type com.android.build.gradle.internal.api.LibraryVariantImpl.
Gradle sync failed: Cannot create variant 'android-lint' after configuration ':Map**AndroidSDK:debugRuntimeElements' has been resolved