Gradle是一款優秀的構建系統工具,它的DSL(領域特定語言)基於Groovy實現,能夠方便地經過代碼控制這些DSL來達到構建的目的。java
注:執行gradle命令時,默認加載當前目錄下的build.gradle文件,亦能夠經過-b 指定要加載的執行文件,如: gradlew -b ./app/build.gradle assemble
android
gradlew tasks
bash
Gradle會以分組的方式列出task列表,好比構建類的 assemable,幫助類的 help等閉包
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Android tasks
-------------
androidDependencies - Displays the Android dependencies of the project.
signingReport - Displays the signing info for each variant.
sourceSets - Prints out all the source sets defined in this project.
Build tasks
-----------
assemble - Assembles all variants of all applications and secondary packages.
assembleAndroidTest - Assembles all the Test applications.
assembleDebug - Assembles all Debug builds.
assembleRelease - Assembles all Release builds.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
bundleDebug - Creates all Debug bundles.
bundleRelease - Creates all Release bundles.
clean - Deletes the build directory.
cleanBuildCache - Deletes the build cache directory.
compileDebugAndroidTestSources
compileDebugSources
compileDebugUnitTestSources
compileReleaseSources
compileReleaseUnitTestSources
Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.
Cleanup tasks
-------------
lintFix - Runs lint on all variants and applies any safe suggestions to the source code.
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'tapeView'.
components - Displays the components produced by root project 'tapeView'. [incubating]
dependencies - Displays all dependencies declared in root project 'tapeView'.
dependencyInsight - Displays the insight into a specific dependency in root project 'tapeView'.
dependentComponents - Displays the dependent components of components in root project 'tapeView'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'tapeView'. [incubating]
projects - Displays the sub-projects of root project 'tapeView'.
properties - Displays the properties of root project 'tapeView'.
tasks - Displays the tasks runnable from root project 'tapeView' (some of the displayed tasks may belong to subprojects).
Install tasks
-------------
installDebug - Installs the Debug build.
installDebugAndroidTest - Installs the android (on device) tests for the Debug build.
installRelease - Installs the Release build.
uninstallAll - Uninstall all applications.
uninstallDebug - Uninstalls the Debug build.
uninstallDebugAndroidTest - Uninstalls the android (on device) tests for the Debug build.
uninstallRelease - Uninstalls the Release build.
Verification tasks
------------------
check - Runs all checks.
connectedAndroidTest - Installs and runs instrumentation tests for all flavors on connected devices.
connectedCheck - Runs all device checks on currently connected devices.
connectedDebugAndroidTest - Installs and runs the tests for debug on connected devices.
deviceAndroidTest - Installs and runs instrumentation tests using all Device Providers.
deviceCheck - Runs all device checks using Device Providers and Test Servers.
lint - Runs lint on all variants.
lintDebug - Runs lint on the Debug build.
lintRelease - Runs lint on the Release build.
lintVitalRelease - Runs lint on just the fatal issues in the release build.
test - Run unit tests for all variants.
testDebugUnitTest - Run unit tests for the debug build.
testReleaseUnitTest - Run unit tests for the release build.
To see all tasks and more detail, run gradle tasks --all
To see more detail about a task, run gradle help --task <task>
BUILD SUCCESSFUL in 18s
1 actionable task: 1 executed
複製代碼
gradle內置的help任務,能夠了解每個Task的使用幫助,用法是gradlew help --task <taskName>
如:gradlew help --task assemable
app
gradlew --refresh-dependencies assemable
ide
只需按順序以空格分開便可,如gradlew clean jar
,先執行clean再執行生成jar工具
自定義屬性具備更普遍的做用域,能夠跨Project,跨Task訪問。只要能訪問這些屬性所屬的對象,那麼這些屬性就能夠被訪問到。post
apply plugin: "java"
ext.age = 18 //自定義一個屬性
//自定義多個屬性
ext{
phone = '020'
address = ''
dep = [android : "androidan"] //map
}
sourceSets.all{
ext.resourcesDir = null
}
sourceSets {
main{
resourcesDir="main/res"
}
test{
resourcesDir="test/res"
}
}
task customProperty{
doLast{
println "age: ${age}"
println "phone: $phone"
println "address: $address"
println "android: ${dep.android}"
sourceSets.each{
println"${it.name}'s resourcesDir is: ${it.resourcesDir}"
}
}
}
複製代碼
任務禁用時,當執行到該任務,會skip掉單元測試
task task1{
doLast{
println 'xxx'
}
}
task1.enabled = false; //false:禁用,true:開啓
複製代碼
任務有一個onlyIf方法,接收一個閉包做爲參數,若是該閉包返回true,則該任務執行,不然跳過測試
以鍵值對的形式添加屬性:-PK=V,中間不能有空格 ,如:
gradle -Pbuild_apps=shoufa build
final String BUILD_APPS_ALL="all";
final String BUILD_APPS_SHOUFA="shoufa";
final String BUILD_APPS_EXCLUDE_SHOUFA="exclude_shoufa";
task QQRelease{
doLast{
println "QQRelease"
}
}
task BaiduRelease{
doLast{
println "BaiduRelease"
}
}
task build{
group BasePlugin.BUILD_GROUP
description '打渠道包'
dependsOn QQRelease,BaiduRelease
}
QQRelease.onlyIf{
if (project.hasProperty('build_apps')) {
Object buildApps = project.property("build_apps")
if (BUILD_APPS_SHOUFA.equals(buildApps)) {
return false
}
}
return true;
}
//gradle -Pbuild_apps=shoufa build; //以鍵值對的形式添加屬性:-PK=V,中間不能有空格
複製代碼
雖然Gradle是個腳本文件,但寫的依舊是代碼,Groovy是兼容Java的,能靈活實現不少功能。如,給生成的apk以當前時間命名:
def buildTime(){
def date = new Date()
def formattedDate = data.format('yyyyMMdd')
return formattedDate
}
複製代碼
單引號和雙引號都能定義字符串。區別在於,單引號表示純粹的字符串常量,而雙引號具備運算能力。 如:
def name = "John"
println '單引號${name}' //輸出: 單引號${name}
println "雙引號${name}" //輸出: 雙引號John
println "雙引號$name" //輸出: 雙引號John
複製代碼
一個美圓符號緊接着一對花括號,花括號裏放表達式,如${name}
,當只有一個變量時,能夠省略花括號,如$name
;
def nums = [1,2,3,4] //定義arrayList
println nums[1] //下標索引訪問
nums.each{
println it //forEach,其中it變量爲正在迭代的元素
}
複製代碼
def map = ['width':1024,'height':768] //定義map
println map['width'] //訪問
println map.height //訪問
map.each{
println "Key:${it.key},Value:$it.value" //forEach,被變量的是Map.Entry
}
複製代碼
method1(2,3) //
method1 2,3 //可忽略括號,
def method1(int a,int b){
println a+b
}
複製代碼
def method2(int a,int b){
if(a>b){
a //即return a;
}else {
b //即return b;
}
}
複製代碼
//呆板寫法
nums.each({println it})
//格式化一些
nums.each({
println it
})
//Groovy中,若是方法的最後一個參數是 閉包,則能夠放到方法外面
nums.each(){
println it
}
//而後省略括號,則變成常見的樣式
nums.each{
println it
}
//多參數調用
eachMap{k,v->
println "$k is $v"
}
複製代碼
def Person p = new Person()
println "name is $p.name" //輸出null
p.name = "John"
println "name is $p.name" //輸出John
class Person {
private String name
}
複製代碼
task delegate{
doLast{
person{
name = "cap" //在閉包內對該it進行配置
age = 20 //在閉包內對該it進行配置
dumpPersion() //在閉包內對該it調用其方法
}
}
}
def dumpPersion(){
println "project delegate"
}
class Person{
private String name
private int age
def dumpPersion(){
println "name:${name};age:${age}"
}
}
def person(Closure<Person> closure){
Person p = new Person()
closure.delegate = p //設置委託
closure.setResolveStrategy(Closure.DELEGATE_FIRST)//設置委託優先
closure(p)
}
複製代碼
sourceSet,即源代碼集合(Android的buildTypes和這個相似),用來描述和管理源代碼和資源存放等功能。提供有sourceSets{}閉包放配置和生成sourceSet,如:
sourceSets{
main{
//在這裏能夠對main sourceSet進行配置
java{
srcDir 'src/java' //更改了java源代碼存放目錄,默認是 src/main/java
}
resources{
srcDir 'src/resources' //更改了資源文件存放目錄,默認是 src/main/resources
}
}
vip{
//生成了新的 vip sourceSet
}
}
複製代碼
SourceSet經常使用屬性:
屬性名 | 類型 | 描述 |
---|---|---|
name | String | 只讀,如,main |
java | SourceDirectorySet | 該源集的java源文件 |
java.srcDirs | Set | 該源集的java源文件所在目錄 |
resources | SourceDirectorySet | 該源集的資源文件 |
resources.srcDirs | Set | 該源集的資源文件的所在目錄 |
output.classesDir | File | 該源集編譯後的class文件目錄 |
output.resourcesDir | File | 編譯後生成的資源目錄 |
compileClassPath | FileCollection | 編譯該源集時所需的classPath |
Android Gradle插件的分類是根據Anroid工程的屬性分類的,在Android中,有3類:
注:src目錄下的androidTest,main,test分別是3個sourceSet,分別對應,Android單元測試代碼,Android app主代碼和資源,普通單元測試代碼。 在main中,特有AndroidManifest.xml和res這兩個Android特有的,用於描述Android App 配置和資源文件。