概念
Gradle的官方定義:
Gradle是一個基於Apache Ant和Apache Maven概念的項目自動化構建開源工具。它使用一種基於Groovy的特定領域語言(DSL)來聲明項目設置,拋棄了基於XML的各種繁瑣配置。
個人通俗的理解爲:
Gradle是一個構建工具,是用來幫我們執行編譯、打包APP等過程的,並且我們可以自己配置構建規則,Gradle便可以根據我們的命令爲我們自動構建出apk包
以下內容基於Android Studio編譯器進行初建的APP工程進行示例說明
全局配置文件
隨着APP項目的模塊化開發、組件化開發以及插件化開發等模式的出現,爲了能方便在不同的module中進行統一管理公共配置信息,全局的gradle配置文件顯得有必要,其中下列方式只是其中的一種配置方式
在項目的根目錄下創建config.gradle
文件,用來統一定義並管理公共配置信息部分
統一定義並管理的config.gradle
內容
/**
* ext:擴展內容,固定格式(即只能用ext{}),並且是一個閉包的內容
*/
ext {
//appcompatV7Version:自定義的屬性,下同
appcompatV7Version = "26.+"
junitVersion = "4.12"
//appId:自定義的一個map映射集合,key-value
appId = [
"app": "app的包名"
]
android = [
compileSdkVersion: 26,
buildToolsVersion: "26.0.2",
minSdkVersion : 19,
targetSdkVersion : 26,
versionCode : 1,
versionName : "1.0"
]
dependencies = [
//shell語言語法:${變量名}
"appcompat-v7": "com.android.support:appcompat-v7:${appcompatV7Version}",
"junit" : "junit:junit:${junitVersion}"
]
}
config.gradle
文件的使用
其實上圖的寫法與以下內容的寫法是一樣的,區別只是做了兩個配置文件的整合而已
// Top-level build file where you can add configuration options common to all sub-projects/modules.
/**
* ext:擴展內容,固定格式(即只能用ext{}),並且是一個閉包的內容
*/
ext {
//appcompatV7Version:自定義的屬性,下同
appcompatV7Version = "26.+"
junitVersion = "4.12"
//appId:自定義的一個map映射集合,key-value
appId = [
"app": "app的包名"
]
android = [
compileSdkVersion: 26,
buildToolsVersion: "26.0.2",
minSdkVersion : 19,
targetSdkVersion : 26,
versionCode : 1,
versionName : "1.0"
]
dependencies = [
//shell語言語法:${變量名}
"appcompat-v7": "com.android.support:appcompat-v7:${appcompatV7Version}",
"junit" : "junit:junit:${junitVersion}"
]
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
全局配置的使用
以app模塊爲例(參照配置中的註釋進行理解)
apply plugin: 'com.android.application'
//賦值與引用,def 類型 相當於java中的Object類型,如config是一個map集合對象
def config = rootProject.ext.android
def appId = rootProject.ext.appId
println("config內容:${config}")//輸出內容-》config內容:[compileSdkVersion:26, buildToolsVersion:26.0.2, minSdkVersion:19, targetSdkVersion:26]
println("appId內容:${appId}")//輸出內容-》appId內容:[app:com.itsdf07.appnotes]
android {
compileSdkVersion config.compileSdkVersion //map集合取值方式一
buildToolsVersion config.buildToolsVersion
defaultConfig {
applicationId appId["app"]//map集合取值方式二
minSdkVersion config.minSdkVersion
targetSdkVersion config.targetSdkVersion
versionCode config.versionCode
versionName config.versionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
其中上面內容使用到了println
打印就相當於java中的system.out.println
一樣,打印結果與查看內容位置如下圖展示
基於上述的統一管理方式以及對Groovy語言的深入瞭解,我們可以進一步進行復雜內容的配置,比如渠道多渠道打包等等