Gradle入门知识之Gradle详解(下)

电子说

1.2w人已加入

描述

6.Gradle Api

Gradle为我们提供了很多丰富的api操作

主要有几下几种:

  • Project api
  • Task api
  • 属性 api
  • 文件 api
  • 以及一些其他api

由于api这块篇幅比较多,就不展开讲解了,后面会单独出一篇文章来讲解这块内容

7.Gradle插件

Gradle插件在我们的项目中使用的还是比较多的,在一些优秀的开源框架:

如鹅厂的Tinker,滴滴的VirtualApk,阿里的Arouter

内部都使用了Gradle插件知识

笔者Gradle插件开始学习的时候,也是一脸懵逼,

其实你把Gradle插件理解为一个第三方jar包就可以了,只是这个jar包是用于我们apk构建的过程

内部其实也是使用一些Task,挂接到我们的apk构建生命周期中。

这里也不会过多讲解

下面我们来讲下Gradle一个特性:

8.增量更新

有没发现你在构建过程中,如果修改的地方对整个任务容器影响不大情况下,你的编译速度会很快,其实就是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的输入和输出做了判断,如果发现文件的输入和输出没有变化,

就直接使用之前缓存的输入输出数据,不再重新执行,缩短编译时间

AS

taskInputsOutputs.png

这里就涉及到了Task的一些知识点:

Task是我们apk构建过程中给的最少单位,每个任务都有输入和输出,将输入的信息传递给下一个任务作为下一个任务的输入,这就是整个构建体系正常运行的核心。

  • 2.Task输入和输出

    任务的执行离不开输入和输出,和我们方法执行一样,依赖输入参数和输出返回值

Gradle中使用:

TaskInputs:来管理输入

TaskOutputs:来管理输出

我们来看下这个两个类的内部代码:

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的方法框架:

AS

Outputs.png

大部分情况和inputs类似,可以输出为文件,属性properties等

注意到这里有几个关键的方法:

upToDateWhen和cacheIf

这两个方法就是用来对构建中的是否对输出操作进行缓存的点,用于增量构建使用

总结

本篇文章主要是讲解了Gradle一些基础认识,Gradle工程项目的概括以及Gradle构建生命周期管理和监听等操作。

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分