gradle学习笔记(四) Groovy高级用法

前言:

在前面两篇学习中,对Groovy的语法基础、闭包有了一定的了解。现在继续就使用Groovy写代码来属性该语言。

多看 Groovy JDK,少些代码。同样,将Groovy DownLoad下来,可以直接查看本地文件的document,查询速度快的多。

Groovy中代码写法:脚本和类

官方文档: scripts_versus_classes

在Groovy中,同样一段代码的写法可以使用脚本、也可以使用类。

//类的写法
class Main {                                    
    static void main(String... args) {          
        println 'Groovy world!'                 
    }
}

//脚本。
println 'Groovy world!';

A script is always compiled into a class. The Groovy compiler will compile the class for you, with the body of the script copied into a run method.
The generated script class will carry all methods into the script class, and assemble all script bodies into the run method.

Groovy编译器会把这种脚本编译为class文件,脚本中的内容会被放到生成类中的main()方法,方法会定义到类里。

  • 每一个脚本都会生成一个 static main 函数。
  • 脚本中的所有代码都会放到run函数中。
  • 如果脚本中定义了函数,则函数会被定义在类中
  • 变量的作用域取决于定义变量的方式,请看官方文档:variables。太多了,有点像js。

println "Groovy world" 生成代码:

import org.codehaus.groovy.runtime.InvokerHelper

class Main extends Script {                     
    def run() {                                 
        println 'Groovy world!' //脚本内容被放到run()方法中            
    }
    static void main(String[] args) {  //生成main()函数         
        InvokerHelper.runScript(Main, args)     
    }
}

Groovy 使用IO流读写文件

官方文档: Working with IO

IO流相关类:


1.读文件

//创建文件对象
def file = new File("filePath");

//读取文件,查看API文档,熟悉用法
def aClosure = {
    line ->
    println line;
};
file.eachLine(aClosure);

/*
这块要说明一下:
查看API文档说明,public Object eachLine(Closure closure);
eachLine函数中要传入一个closure对象,于是上面那种写法就是正统写法。我们调用eachLine()函数,向里面传入一个闭包对象。

下面这种写法就是简写。直接省略掉括号,直接使用闭包。
Groovy中,当函数最后一个参数是闭包时,可以省略括号。
*/

//简写
file.eachLine{
    line ->
    println line;
};

//使用闭包操作输入流
file.withInputStream{
  is ->
  println is.getText(); //查看API文档,操作输入流。不用手动关闭
};

这点要尤为注意:Groovy中,当函数最后一个参数是闭包时,可以省略括号。 要不然以后在gradle中会越看看不懂。

2.写文件

//创建文件对象
def file = new File("srcfilePath");
def newFile = new File("targetFilePath");

newFile.withOutputStream{  os ->
  os << file.getBytes(); //运算符重载
}

Groovy 操作XML文件

官方文档: processing xml

Groovy中提供两个工具类来操作XML文件,查看API,下载下来的api文件里:

  • groovy.util.XmlParser
  • groovy.util.XmlSlurper

解析XML这个功能在Android中应该会用,因为毕竟要操作manifest.xml文件。

示例,两种方式都能解析:

def text = '''
    <some>
        <technology name="groovyProperty">
            <name>GroovyNode</name>
        </technology>
    </some>
'''

//使用XmlSlurper解析
def some = new XmlSlurper().parseText(text); //根节点
assert some instanceof groovy.util.slurpersupport.GPathResult
println some.technology.name;
println some.technology[0].@name;


//使用XmlParser解析,需要调动text()方法
def list = new XmlParser().parseText(text)
assert list instanceof groovy.util.Node
println list.technology.name.text() == 'GroovyNode'

结语:

Groovy本身是一门基于JVM的语言,就像是一个新语言一样。这里仅仅学习、介绍了和gradle相关的Groovy使用。之后开始学习gradle,相信在深入学习gradle过程中会返回来加深对Groovy的理解和使用。

文中相关代码在 github ioLearn 上。

再次感谢前辈们的学习总结和经验,让自己少走了很多弯路。

感谢:
深入理解Android之Gradle

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

baiiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值