相信有很多像我一樣的朋友在使用Android Studio時,對 Gradle 和 Gradle Android 插件的版本號和作用不是很清楚,本篇文章的將對這些進行解釋,最后通過一個實際的項目工程來說明其中的配置塊的含義,并通過源代碼的角度去剖析腳本的結構。
一.第一部分:Q&A
1.Gradle是什么?
Gradle 是一個JVM平臺上的自動化的構建工具,支持多項目構建,強有力依賴管理(本地或者遠程依賴),構建腳本使用Groovy語言編寫。
在Android Studio的 project 視圖下的 gradle/ wrapper/gradle-wrapper.properties 路徑下聲明了項目使用的Gradle版本號,這里使用的是 3.3版本
distributionUrl=https/://services.gradle.org/distributions/gradle-3.3-all.zip
2.Gradle Android Plugin 是什么?
在項目根目錄中的build.gradle文件中有如下設置:
dependencies {    classpath 'com.android.tools.build:gradle:2.3.3'  }這里聲明的是項目對 Gradle Android Plugin 的依賴,其版本號為2.3.3。Gradle的Android插件提供了許多專為構建Android的操作項。
classpath表明的是類路徑,該Android Plugin for Gradle 的對應文件位置在Android Studio根目錄下的:gradle/m2repository/com/android/tools/build/gradle/對應版本號/gradle-3.0.0.jar
	
附上我電腦上的路徑
可以看到,我們依賴的僅僅就是jar文件,build.gradle的腳本是使用Groovy語言編寫的,Groovy編寫的程序可以運行在JVM虛擬機中。而Android Plugin for Gradle是專門為構建Android項目提供庫文件。
平時我們經常使用的比如 buildToolsVersion、compileSdkVersion,buildTypes{ }、sourceSets { }、defaultConfig{ }等方法函數(是的,就是方法,這是Groovy語言中的閉包和函數調用時的特性,現在無需關心,后面第三部分有講到這個),都是Gradle Android Plugin 這個庫提供的方法。可不要以為Gradle僅僅是用來構建Android項目。
3.Android Studio中的compileSdkVersion、buildToolsVersion、minSdkVersion、targetSdkVersion這些配置項是什么?
在導入github上面的工程時,如果該工程所需要的Gradle版本、Android Plugin版本、buildToolsVersion版本,SDK 版本與你本地不符合時,往往會卡死,所以在導入之前可以更改為你本地的版本,在進行導入即可。
4. Gradle Wrapper是什么?
	The Gradle Wrapper allows you to execute Gradle builds on machines where Gradle is not installed. This is useful for example for some continuous integration servers. It is also useful for an open source project to keep the barrier low for building it. The wrapper is also very interesting for the enterprise. It is a zero administration approach for the client machines. It also enforces the usage of a particular Gradle version thus minimizing support issues.
	Gradle Wrapper 可以在沒有安裝Gradle的機器上執行Gradle 構建,經常在持續性構建平臺上所使用,例如jenkis。同時對于客戶端機器來說零成本管理。
Gradle User Guide
第二部分. 關于Android Studio 工程項目你需要知道的一些東西
第三部分 .實際工程分析
通過github上的timber項目分析各模塊下的build.gradle配件文件的含義。可以直接到github上搜索找到該項目。
	
1.Timber項目結構
根目錄的setting.gradle文件,告訴Gradle需要構建的模塊包括那些
include ':timber',include ':timber-lint',include ':timber-sample'
a.其中timber-sample是Application 模塊,對應聲明為
apply plugin: 'com.android.application'
b.timber是android library 模塊,對應聲明為
apply plugin: 'com.android.library'
c.timber-lint是java library模塊,對應聲明為
apply plugin: 'java-library'
2. build.gradle 文件中結構解釋, 大招來了 :)
下面我將通過簡單易懂的方式去讓使用者理解build腳本文件的結構。
常見的模塊下build.gradle文件格式如下:
apply plugin: 'com.android.application'android {  compileSdkVersion 25  buildToolsVersion "26.0.1"  defaultConfig {     }  buildTypes {  }}dependencies {}2.1: “apply plugin: 'com.android.application'” 語句解釋:
Gradle是使用Groovy所寫,這里是調用了 apply方法,Groovy中方法調用時可以省略括號,在你按住Control + 左鍵(mac為command+左鍵)時,可以進入到對應的類中,之前上文也提到過,Android Plugin 僅僅是Jar文件,讓我們進入看看其中的對應方法是什么?
public interface PluginAware {  void apply(Map<String, ?> options);}在Grovvy中 a.b() 這種格式可以寫為a b
所以呢 compileSdkVersion ,apply 等語句都是在調用對應的函數
2.2 xxx { } 格式到底是什么?
def debugClosure(int num, String str, Closure closure){    //dosomething } debugClosure(1, "groovy", {  println"hello groovy!" })首先"{ }" 在Groovy語言中是“閉包”,簡單講閉包就是用“{ }”擴起來的一段代碼段 ,在Groovy中有調用方法的時候有這樣一條規定:“在調用方法的時候,如果方法僅有一個參數是Closure類型(也就是閉包),調用的時候,可以把閉包中的執行的代碼寫到括號中,為xxx({ }),當把括號省略之后就變成了 xxx{ } 格式”。
最前面代碼中的"android{ }"語句函數定義在Project類中,函數定義為:
AppExtension android(Closure configuration);
結論:build.gradle腳本文件在運行的其實就是在執行一系列的函數
3. 根目錄下build.gradle文件分析
該文件定義的是整個項目的構建配置,該配置同樣生效于其他module
3.1 extra 屬性
我們可以在項目頂級build.gradle中聲明ext 塊,在其中定義的屬性可以在其他各個模塊中去使用,通過這種方式可以一次性更改項目的各個模塊的構建配置。
ext {  compileSdkVersion = 26、  supportLibVersion = "26.1.0"}使用:在其他模塊通過rootProject.ext.compileSdkVersion去使用該屬性。
3.2 buildScript 塊
buildscript { repositories {  jcenter()  google() } dependencies {  classpath deps.androidPlugin  classpath 'com.github.ben-manes:gradle-versions-plugin:0.17.0' }}其中repositories中聲明的是遠程倉庫的類別,平時我們在dependencies塊中使用的類似compile 'io.reactivex:rxjava:1.0.0'語句,它在jcenter倉庫中都是唯一存在,在構建的時候,本地如果沒有的話,會到jcenter中去進行下載對應的版本。
4. 模塊下的build.gradle文件分析
1、defaultConfig 塊:定義的是APK各種構建版本的默認設置,這里面的一些屬性可在AndroidManifest.xml文件中重載配置
applicationId 'com.example.myapp' minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" }
productFlavors 塊: 可以配置多種產品類型,比如可以一個是free,另外一種類型是免費
productFlavors {  free {   applicationId 'com.example.myapp.free'  }  paid {   applicationId 'com.example.myapp.paid'  } }buildTypes 塊:可以配置多種構建類型的相應的配置項,比如debug、relase版本。
buildTypes {  release {    minifyEnabled true     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'  } }以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答