6.Gradle Api
Gradle為我們提供了很多豐富的api操作
主要有幾下幾種:
- Project api
- Task api
- 屬性 api
- 文件 api
- 以及一些其他api
由于api這塊篇幅比較多,就不展開講解了,后面會單獨出一篇文章來講解這塊內(nèi)容
7.Gradle插件
Gradle插件在我們的項目中使用的還是比較多的,在一些優(yōu)秀的開源框架:
如鵝廠的Tinker
,滴滴的VirtualApk
,阿里的Arouter
等
內(nèi)部都使用了Gradle
插件知識
筆者Gradle插件開始學習的時候,也是一臉懵逼,
其實你把Gradle插件理解為一個第三方jar包就可以了,只是這個jar包是用于我們apk構建的過程
內(nèi)部其實也是使用一些Task,掛接到我們的apk構建生命周期中。
這里也不會過多講解
下面我們來講下Gradle一個特性:
8.增量更新
有沒發(fā)現(xiàn)你在構建過程中,如果修改的地方對整個任務容器影響不大情況下,你的編譯速度會很快,其實就是Gradle默認支持增量更新功能。
-
1.
定義
:官方:
An important part of any build tool is the ability to avoid doing work that has already been done.
Consider the process of compilation. Once your source files have been compiled,
there should be no need to recompile them unless something has changed that affects the output,
such as the modification of a source file or the removal of an output file. And compilation can take a significant amount of time,
so skipping the step when it’s not needed saves a lot of time.
簡單點說就是Gradle目前對Task的輸入和輸出做了判斷,如果發(fā)現(xiàn)文件的輸入和輸出沒有變化,
就直接使用之前緩存的輸入輸出數(shù)據(jù),不再重新執(zhí)行,縮短編譯時間
taskInputsOutputs.png
這里就涉及到了Task的一些知識點:
Task是我們apk構建過程中給的最少單位,每個任務都有輸入和輸出,將輸入的信息傳遞給下一個任務作為下一個任務的輸入,這就是整個構建體系正常運行的核心。
-
2.
Task
輸入和輸出任務的執(zhí)行離不開輸入和輸出,和我們方法執(zhí)行一樣,依賴輸入參數(shù)和輸出返回值
Gradle
中使用:
TaskInputs
:來管理輸入
TaskOutputs
:來管理輸出
我們來看下這個兩個類的內(nèi)部代碼:
TaskInputs.java
public interface TaskInputs {
/**
* Returns true if this task has declared the inputs that it consumes.
*
* @return true if this task has declared any inputs.
*/
boolean getHasInputs();
/**
* Returns the input files of this task.
*
* @return The input files. Returns an empty collection if this task has no input files.
*/
FileCollection getFiles();
/**
* Registers some input files for this task.
*
* @param paths The input files. The given paths are evaluated as per {@link org.gradle.api.Project#files(Object...)}.
* @return a property builder to further configure the property.
*/
TaskInputFilePropertyBuilder files(Object... paths);
/**
* Registers some input file for this task.
*
* @param path The input file. The given path is evaluated as per {@link org.gradle.api.Project#file(Object)}.
* @return a property builder to further configure the property.
*/
TaskInputFilePropertyBuilder file(Object path);
/**
* Registers an input directory hierarchy. All files found under the given directory are treated as input files for
* this task.
*
* @param dirPath The directory. The path is evaluated as per {@link org.gradle.api.Project#file(Object)}.
* @return a property builder to further configure the property.
*/
TaskInputFilePropertyBuilder dir(Object dirPath);
/**
* Returns a map of input properties for this task.
*
* The returned map is unmodifiable, and does not reflect further changes to the task's properties.
* Trying to modify the map will result in an {@link UnsupportedOperationException} being thrown.
*
* @return The properties.
*/
Map<String, Object> getProperties();
/**
* Registers an input property for this task. This value is persisted when the task executes, and is compared
* against the property value for later invocations of the task, to determine if the task is up-to-date.
*
* The given value for the property must be Serializable, so that it can be persisted. It should also provide a
* useful {@code equals()} method.
*
* You can specify a closure or {@code Callable} as the value of the property. In which case, the closure or
* {@code Callable} is executed to determine the actual property value.
*
* @param name The name of the property. Must not be null.
* @param value The value for the property. Can be null.
*/
TaskInputPropertyBuilder property(String name, @Nullable Object value);
/**
* Registers a set of input properties for this task. See {@link #property(String, Object)} for details.
*
* Note: do not use the return value to chain calls.
* Instead always use call via {@link org.gradle.api.Task#getInputs()}.
*
* @param properties The properties.
*/
TaskInputs properties(Map<String, ?> properties);
/**
* Returns true if this task has declared that it accepts source files.
*
* @return true if this task has source files, false if not.
*/
boolean getHasSourceFiles();
/**
* Returns the set of source files for this task. These are the subset of input files which the task actually does work on.
* A task is skipped if it has declared it accepts source files, and this collection is empty.
*
* @return The set of source files for this task.
*/
FileCollection getSourceFiles();
}
源文件中我們可以看出:
輸入可以有以下種類:
- 1.文件,文件夾以及一個文件集合
- 2.普通的key value屬性
- 2.Map:傳遞一個Map的屬性集合
TaskInputs
還可以通過getHasInputs
判斷是否有輸入
同理我們來看下TaskOutputs
的源碼,篇幅原因,這里直接看下TaskOutputs的方法框架:
Outputs.png
大部分情況和inputs類似,可以輸出為文件,屬性properties等
注意到這里有幾個關鍵的方法:
upToDateWhen和cacheIf
這兩個方法就是用來對構建中的是否對輸出操作進行緩存的點,用于增量構建
使用
總結
本篇文章主要是講解了Gradle一些基礎認識,Gradle工程項目的概括以及Gradle構建生命周期管理和監(jiān)聽等操作。
-
DSL
+關注
關注
2文章
59瀏覽量
38320 -
AS
+關注
關注
0文章
27瀏覽量
26100 -
gradle
+關注
關注
0文章
26瀏覽量
726
發(fā)布評論請先 登錄
相關推薦
評論