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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章