6.Gradle Api
Gradle為我們提供了很多豐富的api操作
主要有幾下幾種:
- Project api
- Task api
- 屬性 api
- 文件 api
- 以及一些其他api
由于api這塊篇幅比較多,就不展開(kāi)講解了,后面會(huì)單獨(dú)出一篇文章來(lái)講解這塊內(nèi)容
7.Gradle插件
Gradle插件在我們的項(xiàng)目中使用的還是比較多的,在一些優(yōu)秀的開(kāi)源框架:
如鵝廠的Tinker
,滴滴的VirtualApk
,阿里的Arouter
等
內(nèi)部都使用了Gradle
插件知識(shí)
筆者Gradle插件開(kāi)始學(xué)習(xí)的時(shí)候,也是一臉懵逼,
其實(shí)你把Gradle插件理解為一個(gè)第三方j(luò)ar包就可以了,只是這個(gè)jar包是用于我們apk構(gòu)建的過(guò)程
內(nèi)部其實(shí)也是使用一些Task,掛接到我們的apk構(gòu)建生命周期中。
這里也不會(huì)過(guò)多講解
下面我們來(lái)講下Gradle一個(gè)特性:
8.增量更新
有沒(méi)發(fā)現(xiàn)你在構(gòu)建過(guò)程中,如果修改的地方對(duì)整個(gè)任務(wù)容器影響不大情況下,你的編譯速度會(huì)很快,其實(shí)就是Gradle默認(rèn)支持增量更新功能。
-
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.
簡(jiǎn)單點(diǎn)說(shuō)就是Gradle目前對(duì)Task的輸入和輸出做了判斷,如果發(fā)現(xiàn)文件的輸入和輸出沒(méi)有變化,
就直接使用之前緩存的輸入輸出數(shù)據(jù),不再重新執(zhí)行,縮短編譯時(shí)間
taskInputsOutputs.png
這里就涉及到了Task的一些知識(shí)點(diǎn):
Task是我們apk構(gòu)建過(guò)程中給的最少單位,每個(gè)任務(wù)都有輸入和輸出,將輸入的信息傳遞給下一個(gè)任務(wù)作為下一個(gè)任務(wù)的輸入,這就是整個(gè)構(gòu)建體系正常運(yùn)行的核心。
-
2.
Task
輸入和輸出任務(wù)的執(zhí)行離不開(kāi)輸入和輸出,和我們方法執(zhí)行一樣,依賴輸入參數(shù)和輸出返回值
Gradle
中使用:
TaskInputs
:來(lái)管理輸入
TaskOutputs
:來(lái)管理輸出
我們來(lái)看下這個(gè)兩個(gè)類(lèi)的內(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();
}
源文件中我們可以看出:
輸入可以有以下種類(lèi):
- 1.文件,文件夾以及一個(gè)文件集合
- 2.普通的key value屬性
- 2.Map:傳遞一個(gè)Map的屬性集合
TaskInputs
還可以通過(guò)getHasInputs
判斷是否有輸入
同理我們來(lái)看下TaskOutputs
的源碼,篇幅原因,這里直接看下TaskOutputs的方法框架:
Outputs.png
大部分情況和inputs類(lèi)似,可以輸出為文件,屬性properties等
注意到這里有幾個(gè)關(guān)鍵的方法:
upToDateWhen和cacheIf
這兩個(gè)方法就是用來(lái)對(duì)構(gòu)建中的是否對(duì)輸出操作進(jìn)行緩存的點(diǎn),用于增量構(gòu)建
使用
總結(jié)
本篇文章主要是講解了Gradle一些基礎(chǔ)認(rèn)識(shí),Gradle工程項(xiàng)目的概括以及Gradle構(gòu)建生命周期管理和監(jiān)聽(tīng)等操作。
-
DSL
+關(guān)注
關(guān)注
2文章
58瀏覽量
38293 -
AS
+關(guān)注
關(guān)注
0文章
27瀏覽量
26080 -
gradle
+關(guān)注
關(guān)注
0文章
26瀏覽量
708
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論