0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

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

jf_78858299 来源:小余的自习室 作者:小余的自习室 2023-03-30 10:51 次阅读

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的输入和输出做了判断,如果发现文件的输入和输出没有变化,

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

图片

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

图片

Outputs.png

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

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

upToDateWhen和cacheIf

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

总结

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

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

    关注

    2

    文章

    57

    浏览量

    38199
  • AS
    AS
    +关注

    关注

    0

    文章

    27

    浏览量

    25979
  • gradle
    +关注

    关注

    0

    文章

    26

    浏览量

    662
收藏 人收藏

    评论

    相关推荐

    gradle安装与配置unity

    Gradle是一种用于构建和自动化构建过程的强大工具,而Unity则是一款跨平台的游戏引擎。在Unity项目中使用Gradle可以帮助我们更方便地管理和构建项目,同时能够提供更高的自定义能力和稳定性
    的头像 发表于 12-07 14:48 754次阅读

    Android Studio与Gradle深入

    小语言规范用来处理一个特定的事情(大多情况是配置)。Android 的插件的 DSL 文档在 Android Gradle DSL有说明。  理解了以上基础之后,你就会知其然,知其所以然了。  以上知识
    发表于 08-31 17:58

    Gradle for Android

    Gradle for Android
    发表于 07-16 15:50

    请问OpenHarmony鸿蒙demo gradle报错怎么解决?

    鸿蒙demo gradle报错怎么解决build.gradle:4: Could not find method ohos() for arguments
    发表于 04-01 11:27

    DevEco报错不能顺利进入gradle8.0怎么解决?

    报错内容为:此版本中使用了已弃用的 Gradle 功能,使其与 Gradle 8.0 不兼容。翻译一,大概有些人说这个中外使用了Gradle的特性,但它应该与
    发表于 04-21 11:15

    DevEco Studio自动配置gradle的方法分享

    方法①在项目右键 Find in Path输入gradle-5.4.1-all.zip方法②下载gradle-5.4.1-all.zip解压至不要含有中文名称的目录比如我解压后的路径是D
    发表于 06-09 10:26

    快速入门Gradle的方法

    我们前面的Gradle是一门基于Groov的DSL,可能很多童鞋就是因为你是这个Gradle的迷,第一觉得Gradle是一门独立的语言呀,如果想进入歧途了,我一开始也是这么迷糊的,当你了解之后,你就可以这么理解
    的头像 发表于 04-08 10:56 1015次阅读
    快速<b class='flag-5'>入门</b><b class='flag-5'>Gradle</b>的方法

    Gradle入门知识Gradle详解

    大家回想一下自己第一次接触`Gradle`是什么时候? 相信大家也都是和我一样,在我们打开第一个AS项目的时候, 发现有很多带gradle字样的文件:`setting.gradle, build.
    的头像 发表于 03-30 10:47 2226次阅读
    <b class='flag-5'>Gradle</b><b class='flag-5'>入门</b><b class='flag-5'>知识</b>之<b class='flag-5'>Gradle</b><b class='flag-5'>详解</b>

    Gradle入门知识Gradle语法1

    很多开发喜欢把`Gradle`简单定义为一种构建工具,和`ant,maven`等作用类似, 诚然Gradle确实是用来做构建,但是如果简单得把Gradle拿来做构建,就太小看Gradle
    的头像 发表于 03-30 10:54 808次阅读
    <b class='flag-5'>Gradle</b><b class='flag-5'>入门</b><b class='flag-5'>知识</b>之<b class='flag-5'>Gradle</b>语法1

    Gradle入门知识Gradle语法2

    很多开发喜欢把`Gradle`简单定义为一种构建工具,和`ant,maven`等作用类似, 诚然Gradle确实是用来做构建,但是如果简单得把Gradle拿来做构建,就太小看Gradle
    的头像 发表于 03-30 10:54 579次阅读

    Gradle入门知识Gradle api解析(上)

    由于Project源码篇幅太长:这里只列出类的部分方法和属性: 我们前面分析过,每个build.gradle对应一个Project,Project在初始过程中会被构建为`树`形结构:
    的头像 发表于 03-30 11:01 2052次阅读
    <b class='flag-5'>Gradle</b><b class='flag-5'>入门</b><b class='flag-5'>知识</b>之<b class='flag-5'>Gradle</b> api解析(上)

    Gradle入门知识Gradle api解析(下)

    由于Project源码篇幅太长:这里只列出类的部分方法和属性: 我们前面分析过,每个build.gradle对应一个Project,Project在初始过程中会被构建为`树`形结构:
    的头像 发表于 03-30 11:05 840次阅读

    Gradle Plugin和AGP的区别1

    Gradle Plugin`和`AGP`的区别? `Gradle Plugin`是`Gradle`构建过程中使用的插件的总称,而`Android Gradle Plugin`是这
    的头像 发表于 03-30 11:48 996次阅读
    <b class='flag-5'>Gradle</b> Plugin和AGP的区别1

    Gradle Plugin和AGP的区别2

    Gradle Plugin`和`AGP`的区别? `Gradle Plugin`是`Gradle`构建过程中使用的插件的总称,而`Android Gradle Plugin`是这
    的头像 发表于 03-30 11:49 911次阅读

    Gradle Plugin和AGP的区别3

    Gradle Plugin`和`AGP`的区别? `Gradle Plugin`是`Gradle`构建过程中使用的插件的总称,而`Android Gradle Plugin`是这
    的头像 发表于 03-30 11:50 961次阅读