Gradle

一:在gradle 打印

在这里插入图片描述

二:在app/build.gradle 中 打印apk 的位置

三 :对象转成 字串

class Person {
    String name
    Integer age
}

1 对象----> 字串
//对象转成json字符串
def list=[new Person(name:'james',age:18),
          new Person(name:'lilei',age:18)]
// 把list 集合转成json 字串
println JsonOutput.toJson(list)

// 输出:[{"age":18, "name":"james"},{"age": 18, "name":"lilie"}]

2,对象----->格式化的字串

// 格式化
def json=JsonOutput.toJson(list)
println JsonOutput.prettyPrint(json)

// [{
    "age": 18,
    "name": "james"
}, {
    "age": 18,
    "name": "lilie"
}]

四:json 字串转成 对象

//json字符串转成对象
def jsonSluper=new JsonSlurper()
def object=jsonSluper.parse("[{\"age\":18,\"name\":\"james\"},{\"age\":18,\"name\":\"lilei\"}]".getBytes())
println object

// [[age:18,name:james],[age:18, name:lilei]]

def object2=jsonSluper.parse("[{\"abc\":\"james\"}]".getBytes())
println object2.abc

// 输出:[james]

五:XML 解析

import groovy.xml.MarkupBuilder
import groovy.xml.XmlSlurper

final String xml='''
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.demo">
    <test>123456789</test>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity2">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
'''

解析XML数据
//def xmlSluper=new XmlSlurper()
//def result=xmlSluper.parseText(xml) //拿到根节点manifest,并赋值给result,所以result代表manifest
//println result.@package  // result.@package 表示访问manifest节点下的package属性 ,拿到的结果是:com.example.demo
//println result.test.text() // 获取manifest节点下test节点的文本,拿到的文本结果是:123456789

读取有域名空间的节点
//result.declareNamespace('android':'http://schemas.android.com/apk/res/android')
result.application.@'android:allowBackup' 表示获取manifest根节点下的application节点的“android:allowBackup”的值,拿到结果是:true
//println result.application.@'android:allowBackup' 
//println result.application.activity[0].@'android:name' // 输出是:.MainActivity
//println result.application.activity[1].@'android:name' // 输出是:.MainActivity2
 

遍历XML节点,遍历打印Activity
//result.application.activity.each{activity ->
//    println activity.@'android:name' // 输出是:.MainActivity   .MainActivity2
//}

/**
 * 生成XML格式数据
 * <html>
 *     <title id='123',name='android'>xml生成
 *          <person></person>
 *     </title>
 *     <body name='java'>
 *         <activity id='001' class='MainActivity'>abc</activity>
 *         <activity id='002' class='SecActivity'>abc</activity>
 *     </body>
 * </html>

六:文件操作

def file=new File("C:\\Users\\FileDemo.txt")
//遍历文件,一行一行的输出
//file.eachLine { line ->
//    println line 
//}

//获取文件的所有文本
//def text=file.getText()
//println text

//以List<Stirng>返回文件的每一行
//def text=file.readLines()
//println text.toListString()

//以java中的流的方式读取文件内容
def reader=file.withReader{reader ->
    char[] buffer=new char[100]
    reader.read(buffer)
    return buffer
}
println reader
//写入数据
//file.withWriter { writer ->
//    writer.write("abc")
//}

七,屏蔽某些类,不让其打包到工程apk里

屏蔽动作

 输出的结果

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值