版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhifeng172/article/details/88910338
修改根目录下的settings.gradle,增加以下代码
gradle.beforeProject {
//定义变量
def pattern
def matcher
def result
//获取Config.java中的变量值
//从public static final String ServAddr = "https://10.0.0.111:9093";中抽取111
def cfgFile = file("app/src/main/java/com/jeff/demo/config/Config.java")
pattern = Pattern.compile("(?:^|\\n)[^/].*?.(\\d+):")
matcher = pattern.matcher(cfgFile.getText())
matcher.find()
def flag = matcher.group(1)
//修改build.gradle中的short_ip值
def buildFile = file("app/build.gradle")
result = buildFile.getText()
pattern = Pattern.compile("short_ip\\s.*'(\\d*)'")
matcher = pattern.matcher(result)
matcher.find()
result = matcher.replaceAll("short_ip = \'" + flag + "\'")
//修改版本号
pattern = Pattern.compile("versionCode\\s+(\\d+)")
matcher = pattern.matcher(result)
matcher.find()
def versionCode = Integer.parseInt(matcher.group(1))
result = matcher.replaceAll("versionCode " + ++versionCode)
//修改版本名
pattern = Pattern.compile("versionName\\s+\"(.+)\"")
matcher = pattern.matcher(result)
matcher.find()
int no = 0
int decade = 0
int hundred = 0
int thousand = 0
int remain = versionCode
int base = 10
int operand = base**3
if (versionCode >= operand) {
thousand = remain / operand
remain = remain % operand
}
operand = base**2
if (remain >= operand) {
hundred = remain / operand
remain = remain % operand
}
operand = base**1
if (remain >= operand) {
decade = remain / operand
remain = remain % operand
}
if (remain > 0) {
no = remain % operand
}
def versionName = "\"" + thousand + "." + hundred + "." + decade + "." + no + "\""
result = matcher.replaceAll("versionName " + versionName)
//重写文件
buildFile.write(result)
}