Oftentimes you will need to know which exactly build of the application user needs. For your regular users version name should be enough, but when it comes to your testers, you will probably give them multiple version between releases to test. This is why you may want to add build version also to your application version name. To make this easy you can just get shortened commit hash and add it at the end of the application version name.
Add this code to your build.gradle (:app)
def getCommitHash = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-parse', '--short', 'HEAD'
standardOutput = stdout
}
return stdout.toString().trim()
}
preprod {
buildConfigField "String", "COMMIT_HASH", "\"${getCommitHash()}\""
}
And then wherever you want to use it simply add:
private fun setApplicationVersionInformation() {
val version: SemVer = SemVer.parse(BuildConfig.VERSION_NAME)
val build: String = BuildConfig.COMMIT_HASH
binding.applicationVersion.text = String.format(
"%s %s.%s.%s.%s",
"Application version: ",
version.major,
version.minor,
version.patch,
build
)
}