//import groovy.util.XmlSlurper //解析XML时候要引入这个groovy的package
//直接printx里打印为何出不来呢.
def printx() {
return "utils's Method,用到了ext设置后,各个project都可调用>>>"
}
def copyFile(String srcFile,String targetFile) {
// ......//拷贝文件函数,用于将最后的生成物拷贝到指定的目录
//srcFile-->targetFile复制文件
def demoCopy="/$project.name"+"-demo.apk"
def demoName="/$project.name"+"-"+"$project.versionName"+"-demo.apk"
def src = new File(srcFile+demoCopy)
def target = new File(targetFile+demoName)
target.withOutputStream {
def os = it//输出流
src.withInputStream {
def ins = it//输入流
os << ins//利用OutputStream的<<操作符重载,完成从inputstream到OutputStream
//的输出
}
}
}
def rmFile(String targetFile) {
//.....//删除指定目录中的文件
}
def cleanOutput(boolean bJar = true) {
// ....//clean的时候清理
}
def copyOutput(boolean bJar = true) {
// E:\Java\Android\MyApplication\myapplication2\build\outputs\apk\myapplication2-debug.apk
def srcFile = project.buildDir.absolutePath + "/outputs/apk"
def dstFile = "C:/Users/tyxiong/Desktop"
copyFile(srcFile, dstFile)
// ....//copyOutput内部会调用copyFile完成一次build的产出物拷贝
}
def getVersionNameAdvanced() {//解析AndroidManifest.xml文件里信息>
def xmlFile = project.file("AndroidManifest.xml")
def rootManifest = new XmlSlurper().parse(xmlFile)
return rootManifest['@android:versionName']
}
//对于android library编译,我会disable所有的debug编译任务
def disableDebugBuild() {
//project.tasks包含了所有的tasks,下面的findAll是寻找那些名字中带debug的Task。
//返回值保存到targetTasks容器中
def targetTasks = project.tasks.findAll { task ->
task.name.contains("Debug")
}
//对满足条件的task,设置它为disable。如此这般,这个Task就不会被执行
targetTasks.each {
println "disable debug task :${it.name}"
it.setEnabled false
}
}
//将函数设置为extra属性中去,这样,加载utils.gradle的Project就能调用此文件中定义的函数了
ext {
copyFile = this.©File
rmFile = this.&rmFile
cleanOutput = this.&cleanOutput
copyOutput = this.©Output
getVersionNameAdvanced = this.&getVersionNameAdvanced
disableDebugBuild = this.&disableDebugBuild
printx = this.&printx
}
gradle中的utils.gradle
最新推荐文章于 2024-07-13 11:14:38 发布