gradle针对版本号的管理——任务模式,直接修改build.gradle文件

app/gradle.build

// 版本号自增
task('increaseVersionCode') << {
    def buildFile = file("build.gradle")
    def pattern = Pattern.compile("versionCode\\s+(\\d+)")
    def manifestText = buildFile.getText()
    def matcher = pattern.matcher(manifestText)
    matcher.find()
    def versionCode = Integer.parseInt(matcher.group(1))
    def manifestContent = matcher.replaceAll("versionCode " + ++versionCode)
    buildFile.write(manifestContent)
}

// 版本号自增
tasks.whenTaskAdded { task ->
    if (task.name.matches('assemble.*?Release')) {
        task.dependsOn 'increaseVersionCode'
    }
}

你可能感兴趣的:(Java)