task replacePackageName {
FileTree tree = fileTree(dir:'src')
tree.include'**/*.java'
tree.include'**/*.xml'
tree.each {File mfile ->
fileReader(mfile.path,targetName)
}
// fileReader("build.gradle", packageName)
}
def fileReader(path, target_package) {
def readerString ="";
def hasReplace =false
file(path).withReader('UTF-8') { reader ->
reader.eachLine {
if(it.find("com.lzl.test")) {
it = it.replace("com.lzl.test", target_package)
hasReplace =true
}
readerString <<= it
readerString <<'\n'
}
if(hasReplace) {
println(path +" has replace package.")
file(path).withWriter('UTF-8') {
within ->
within.append(readerString)
}
}
return readerString
}
}
使用方式:
先把包名所在文件夹名字改了,然后把task放到gradle中。执行task。批量替换修改后的包名