AI智能
改变未来

Android中gradle脚本 删除目录 修改目录 修改文件字符串等操作

//编码格式
tasks.withType(JavaCompile) {
options.encoding = \’UTF-8\’
}

//定义全局变量
ext{
NAME=\”test\”
}

//读取gradle配置文件
def initBuildPath() {
Properties properties = new Properties()
File propertyFile = new File(rootDir.getAbsolutePath() + \”/local.properties\”)
properties.load(propertyFile.newDataInputStream())
ext.sdkDir = properties.getProperty(\’sdk.dir\’)
ext.ndkDir = properties.getProperty(\’ndk.dir\’)
}

//读取文件并替换字符串

def fileReader(path, oldStr, newStr) {
def readerString = \”\”;
new File(path).withReader(\’UTF-8\’) { reader ->
reader.eachLine {
if (it.find(oldStr)) {
it = it.replace(oldStr, newStr)
}
readerString <<= it
readerString << \’\\n\’
}
return readerString
}
}

//写文件

def fileWrite(path, stringBuffer) {
new File(path).withWriter(\’UTF-8\’) {
within ->
within.append(stringBuffer)
}
}

//copy src 目录

task copyPackage(type: Copy) {
initBuildPath()
from \’src\’
into SRC_TEMP
// 剔除不需要的文件
exclude \’*values-zh-rCN\’, \’*values-zh-rTW\’, \’*values-th\’, \’*values-ko\’
}

//修改目录

task moveToTest(type: Copy) {
// println(\”开始移动到test\”)
from JAVA_DIR
into JAVA_DIR_TEST
filter { String line ->
if (line.find(\’package com.main;\’)) {
//替换字符串
line = line.replace(\”package com.main;\”, \”package com.main.test;\”)
}
\”$line\”
}
//删除原目录
doLast {
File file1 = new File(rootDir.getAbsolutePath() + \’\\\\app\\\\\’ + JAVA_DIR);
file1.deleteDir();
}
}

//替换文件的字符串

task replaceConstants << {
def strBuffer = fileReader(rootDir.getAbsolutePath() + \’\\\\app\\\\\’ + JAVA_DIR_TEST + \’\\\\Test.java\’, \”TEST_FLAG = 0\”, \”TEST_FLAG = 1\”);
fileWrite(rootDir.getAbsolutePath() + \’\\\\app\\\\\’ + JAVA_DIR_TEST + \’\\\\Test.java\’, strBuffer);
}

//运行命令行

task createR(type: Exec) {
workingDir \’E:\\\\SDK\\\\build-tools\\\\23.0.2\\\\\’
commandLine \’cmd\’, \’/c\’, \’aapt package -f -m -J \’ + r文件要存放的目录 + \” -S v7的res文件 -S res文件 -M manifest文件 -I android.jar文件 –auto-add-overlay\”
}

//apk编译之前运行taskA

preBuild.dependsOn taskA

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » Android中gradle脚本 删除目录 修改目录 修改文件字符串等操作