This is a short blog combining multiple resources I found on the internet in one place.
Problem
When we build apk in android by default the apk file is named based on the combination of the main application module name appending by buildType. For example app-debug.apk
of app-release.apk
This works fine until QA teams are involved and they are testing multiple apk at the same time.
So when QA builds the apk locally or from CI, and testing multiple tasks, it’s hard to identify which apk is for which task.
Usually, in most project management tools, each task has a unique id which makes it easy to track or find that task.
Solution
Rename apk to task id in the build so that it is easy to identify the apk for that task.
So how do we get the task id?
The simplest solution is to create a branch name of that task id and use that branch name to rename the apk.
1. Rename apk file
We use gitBranch()
function to return the current branch name and use the setProperty
method in the android block to rename the apk.
android {
...
defaultConfig {
applicationId "com.burhanrashid52.example"
versionCode 1
versionName "1.0.0"
setProperty("archivesBaseName", gitBranch() + "-v" + versionCode + "(" + versionName + ")")
}
}
def gitBranch() {
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()
branch
}
If the git branch name is 1021-rename-apk
then the file name will be 1021-rename-apk-v1(1.0.0).apk
in app/build/outputs/apk
folder.
2. Show on UI
We can also use resValue
property to show the git branch name in the app UI.
android {
...
defaultConfig {
applicationId "com.burhanrashid52.example"
resValue "string", "branch_name", gitBranch()
}
}
def gitBranch() {
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()
branch
}
And use string resources to get the branch name like this.
context.getString(R.string.branch_name)
Complete code
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.burhanrashid52.example"
minSdkVersion 24
targetSdkVersion 30
versionCode 1
versionName "1.0.0"
resValue "string", "app_name", "Rock"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
setProperty("archivesBaseName", gitBranch() + "-v" + versionCode + "(" + versionName + ")")
resValue "string", "branch_name", gitBranch()
}
}
def gitBranch() {
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()
branch
}
That’s it
Thank you for taking the time to read this article.
If you have any questions please let me know in the comments or you can reach me on Twitter or Email me.
If you like this kind of content then you can subscribe to my blog and be the first one to get notified when any new content is published.
If you like this article, please like and share it with your friends/colleagues and help me to spread the word.
Resources
- https://www.generacodice.com/en/articolo/3054844/how-to-show-current-git-branch-in-an-android-application-programatically
- https://stackoverflow.com/questions/18332474/how-to-set-versionname-in-apk-filename-using-gradle/28992851#28992851
Photo by Ravi Roshan on Unsplash
Social Profiles