I need to figure out how to get or make a build number for my Android application. I need the build number to display in the UI.
Do I have to do something with
BuildConfig.VERSION_NAME
Yep, it's that easy now.
If you're getting a empty string for BuildConfig.VERSION_NAME then read on.
I kept getting an empty string for BuildConfig.VERSION_NAME because I wasn't setting the versionName in my Grade build file (I migrated from ANT to Gradle). So, here are instructions for ensuring you're setting your VERSION_NAME via Gradle.
build.gradle
def versionMajor = 3
def versionMinor = 0
def versionPatch = 0
def versionBuild = 0 // bump for dogfood builds, public betas, etc.
android {
defaultConfig {
versionCode versionMajor * 10000 + versionMinor * 1000 + versionPatch * 100 + versionBuild
versionName "${versionMajor}.${versionMinor}.${versionPatch}"
}
}
Note: This is from the masterful Jake Wharton.
versionName and versionCode from AndroidManifest.xmlAnd since you've set the versionName and versionCode in the build.gradle file now, you can also remove them from your AndroidManifest.xml file, if they are there.