In our Android project we want the filename of our APK to contain information such as the date, git branch name and short commit hash.
We've been setting this as part of our defaultConfig
android {
defaultConfig {
setProperty("archivesBaseName", "$projectName.$branchName.$date.$buildNumber.$versionCode-$versionName-$versionSha")
}
}
This works most of the time but our build sometimes fails with errors pointing to the value being stale (for example after switching branches). Forcing Gradle Sync in Android Studio usually fixes it.
So is there a better, more robust way of making sure that property stays up to date and forces a gradle sync, if necessary?
I was having same issue, they were not always uptodate with the build.
I solved it by using:
project.ext.set("archivesBaseName", "myAppName");
under the defaultConfig block, and I stopped having the problem.
Not sure it will work for the branch, build number or date, but it's worth giving it a shot. Here's my configs, including the defaultConfig Block. For my case I was only using a hard coded versionName and build Number (I call it versionCode because I set it in the manifest dynamically). But maybe you can tweak it to your needs:
defaultConfig {
minSdkVersion 15
targetSdkVersion 22
versionCode System.getenv("BUILD_NUMBER") as Integer ?: 0
versionName "6.2.5." + versionCode
}
project.ext.set("archivesBaseName", "myAppname" + defaultConfig.versionName);
try to call clean.execute()
before
android { ... }
it is the cheapest 'sync' task I can think of :)
clean.execute()
android {
defaultConfig {
setProperty("archivesBaseName", "$projectName.$branchName.$date.$buildNumber.$versionCode-$versionName-$versionSha")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With