Skip to content

Instantly share code, notes, and snippets.

@kibotu
Last active July 18, 2022 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kibotu/f363ed0e86b6656f279cb707d762e975 to your computer and use it in GitHub Desktop.
Save kibotu/f363ed0e86b6656f279cb707d762e975 to your computer and use it in GitHub Desktop.
Generating build time variables into app/asset folder. Newly support for configuration caching.
/**
* Task to generate build.json and puts it into assets/meta folder within the app.
*/
preBuild.dependsOn tasks.register("generateBuildJson") {
// note: file can't be named 'build.json' otherwise it will not be bundled into the app.
outputs.file(project.rootProject.file('app/src/main/assets/meta.json'))
// need to remember configuration variables for later task execution
def majorVersion = MAJOR_VERSION
def minorVersion = MINOR_VERSION
def hotfixVersion = HOTFIX_VERSION
def buildNumber = BUILD_NUMBER
def versioncode = VERSIONCODE
def versionName = VERSIONNAME
doLast {
description = "Generate //${outputs.files.singleFile.path}"
println(description)
def branchName = {
def branch = ""
def proc = "git rev-parse --abbrev-ref HEAD".execute()
proc.in.eachLine { line -> branch = line }
proc.err.eachLine { line -> println line }
proc.waitFor()
// fix detached head
if (branch.contains("release/")) {
branch = branch.replace("heads/", "")
}
branch.trim()
}
def commitHash = {
def hash = ""
def proc = "git log -n 1 --format='%h'".execute()
proc.in.eachLine { line -> hash = line }
proc.err.eachLine { line -> println line }
proc.waitFor()
hash.trim().replaceAll('\'', '')
}
def json = groovy.json.JsonOutput.toJson([
'build-time' : new Date().getTime().toString(),
'branch-name' : branchName(),
'commit-hash' : commitHash(),
'major-version' : majorVersion.toString(),
'minor-version' : minorVersion.toString(),
'hotfix-version': hotfixVersion.toString(),
'build-number' : buildNumber.toString(),
'version-code' : versioncode.toString(),
'version-name' : versionName
])
outputs.files.singleFile.text = groovy.json.JsonOutput.prettyPrint(json)
}
}
clean {
delete project.rootProject.file('app/src/main/assets/meta.json')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment