当前位置: 首页 > news >正文

使用groovy代码方式解开gradle配置文件神秘面纱

来到这里的是不是都有以下疑问:

1.build.gradle配置文件结构好复杂啊,怎么记?

2.内部是怎么进行分析和执行的?

3.为什么可以在配置文件里面写groovy代码,怎么识别的?

4.怎么才能很方便的记住和快速上手gradle呢?

好的,现在我带着你们的疑问,使用groovy代码的方式,慢慢解开build.grade神秘的面纱,探索其中的奥妙!!!

声明:不要急,我们慢慢深入,请耐心看完

首先我们先看一段groovy代码

        def dependencies = {println("我执行了")}dependencies()

执行结果:

这断代码就是一个无参方法声明和调用,是不是非常简单。

现在我们进一步改造

代码:

        def dependencies = {int a, int b ->println("我执行了" + a + b)}dependencies(1, 2)

 执行结果:

我这次只是加了两个整型参数,进行打印。

好的,我们再次深入改造

代码:

        def dependencies = {int a, int b, Closure c ->println( a+b)c.call()}dependencies(1, 2){println("我执行了")}

 执行结果:

 卧槽,什么鬼!!!!,竟然也行,

重要先看这部分代码

 dependencies(1, 2){println("我执行了")}

这部分代码起始可以看成是两部分:

1. 执行函数部分,dependencies(1, 2)

 2.回调函数逻辑实现部分, 

{println("我执行了")
}

当我们执行

dependencies(1, 2){println("我执行了")
}
这段代码时,实际执行流程就是将
{println("我执行了")
}这部分代码生成回调函数,由Closure对象的call函数进行执行回调 

然后真正执行函数dependencies,然后执行Closure.call执行回调逻辑。

以上都看明白了,现在我们真正进入主题

以gradle配置文件为例子,如下

dependencies {implementation 'com.google.android.material:material:1.4.0'implementation 'androidx.constraintlayout:constraintlayout:2.0.4'testImplementation 'junit:junit:4.13.2'androidTestImplementation 'androidx.test.ext:junit:1.1.3'androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

此时你有该蒙蔽了

implementation 'com.google.android.material:material:1.4.0'是什么意思啊,你上面没有说啊,

那好,我们现在说,其实方法调用还有另一种方式

方法名称  参数1[,参数2]

就拿前面举得例子

代码:

        def dependencies = {a ->println("结果:" + a)}dependencies 100

执行结果: 

和dependencies(100)效果是一样的,

那好现在明白了,原来implementation、testImplementation、androidTestImplementation 其实就是内置函数哈。

implementation 'com.google.android.material:material:1.4.0'implementation 'androidx.constraintlayout:constraintlayout:2.0.4'testImplementation 'junit:junit:4.13.2'androidTestImplementation 'androidx.test.ext:junit:1.1.3'androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

这些其实都是在调用函数哈,

dependencies {implementation 'com.google.android.material:material:1.4.0'implementation 'androidx.constraintlayout:constraintlayout:2.0.4'testImplementation 'junit:junit:4.13.2'androidTestImplementation 'androidx.test.ext:junit:1.1.3'androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

那好,现在我就按照这个配置文件整体实现一下代码。

代码:

 def implementation = {arg ->println("引入接口依赖:" + arg)};def testImplementation  = {arg ->println("引入测试接口依赖:" + arg)};def androidTestImplementation = {arg ->println("引入安卓测试接口依赖:" + arg)}def dependencies = {Closure c ->c.call()}dependencies{implementation 'com.google.android.material:material:1.4.0'implementation 'androidx.constraintlayout:constraintlayout:2.0.4'testImplementation 'junit:junit:4.13.2'androidTestImplementation 'androidx.test.ext:junit:1.1.3'androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'}

执行结果:

恍然大悟,原来build.gradle配置文件其实就是在执行各种函数。

应当随时学习,学习一切;应该集中全力,以求知道得更多,知道一切

http://www.lryc.cn/news/25911.html

相关文章:

  • kafka入门到实战二(使用docker搭建kafka集群)
  • 【简化开发】lombok的使用、编译后的代码及源码
  • 在线就能用的主图设计素材,免费分享!
  • 【测绘程序设计】——计算卫星位置
  • 山东双软认证的基本条件
  • TPM 2.0实例探索3 —— LUKS磁盘加密(4)
  • Linux连接RDP远程服务工具集记录
  • 离散事件动态系统
  • 无线WiFi安全渗透与攻防(二)之打造专属字典
  • 拥抱 Spring 全新 OAuth 解决方案
  • 前端开发与vscode开发工具介绍
  • C++---最长上升子序列模型---友好城市(每日一道算法2023.3.2)
  • maven高级知识。
  • Python 之 Pandas 处理字符串和apply() 函数、applymap() 函数、map() 函数详解
  • 汇川AM402和上位机C#ModebusTcp通讯
  • 给你一个电商网站,你如何测试?功能测试及接口测试思路是什么?
  • Spring Boot 3.0系列【5】基础篇之应用配置文件
  • SQLyog图形化界面工具【超详细讲解】
  • Linux: 中断只被GIC转发到CPU0问题分析
  • 模电学习10. MOS管简单应用电路
  • 轻松搞懂Linux中的用户管理
  • 力扣-丢失信息的雇员
  • FPGA采集AD7606全网最细讲解 提供串行和并行2套工程源码和技术支持
  • CSS介绍
  • Auto-encoder 系列
  • 【蓝桥杯入门不入土】变幻莫测的链表
  • axios的二次封装
  • GET与POST区别(最详细)
  • 精选博客系列|将基于决策树的Ensemble方法用于边缘计算
  • JS混淆加密:Eval的未公开用法