Gradle是一種基於依賴的編程語言,你可以在已有的task中自定義task或者依賴規則。對比我們最常用的語言,比如java、Object-C,Gradle就像同時包含了配置虛擬機、字節碼解釋規則、code語法。Gradle會讓這些task按照順序執行,且只執行一次。有些build tools工具會在任何一個task執行前構建完成一個基於依賴的task隊列,便於完成它指定的編譯任務,比如com.android.tools.build。
一次Gradle構建包含三個階段:Initialization(初始化)、Configuration(配置)、Execution(運行)
1)Initialization
Gradle支持同時至少一個項目的構建,在Initialization階段,Gradle決定哪個項目可以參與構建(build),併爲它們分別創建一個Project實例。
2)Configuration
參與構建的所有項目的build script會被執行(從Gradle 1.4開始,有關聯的項目纔會被配置)
3)Execution
Gradle劃分完成在配置階段被創建的即將執行的task,通過gradle命令參數和當前目錄確定這些task是否應該得到執行。
Setting文件
Gradle確定一個默認名爲setting.gradle的setting文件,這個文件會在Initialization階段執行。同時構建多個項目時,必須在所有項目的頂層目錄中放置一個setting.gradle文件,這個文件用來確定哪個項目參與接下來的構建過程。如果只有個項目,可以沒有setting.gradle文件。
單項目構建舉例:
settings.gradle
println 'This is executed during the initialization phase.'
build.gradle
println 'This is executed during the configuration phase.'
task configured {
println 'This is also executed during the configuration phase.'
}
task test << {
println 'This is executed during the execution phase.'
}
task testBoth {
doFirst {
println 'This is executed first during the execution phase.'
}
doLast {
println 'This is executed last during the execution phase.'
}
println 'This is executed during the configuration phase as well.'
}
運行命令:gradle test testBoth
> gradle test testBoth
This is executed during the initialization phase.
This is executed during the configuration phase.
This is also executed during the configuration phase.
This is executed during the configuration phase as well.
:test
This is executed during the execution phase.
:testBoth
This is executed first during the execution phase.
This is executed last during the execution phase.
BUILD SUCCESSFUL
Total time: 1 secs
附註:在一段Gradle腳本中,可以通過一個project對象實現對屬性的訪問和方法的調用。同樣的,在setting文件中,可以通過setting(比如Setting類對象)對象實現對屬性的訪問和方法的調用。