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

原文地址:http://www.68idc.cn/help/mobilesys/android/20160507613407.html


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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值