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 AndroidManifest.xml
?
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 AndroidManifest.xml
?
First:
and then use this:
try this one:
Using Gradle and BuildConfig
Getting the VERSION_NAME from BuildConfig
Yep, it's that easy now.
Is It Returning an Empty String for VERSION_NAME?
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 theversionName
in my Grade build file (I migrated from ANT to Gradle). So, here are instructions for ensuring you're setting yourVERSION_NAME
via Gradle.build.gradle
Note: This is from the masterful Jake Wharton.
Removing
versionName
andversionCode
fromAndroidManifest.xml
And since you've set the
versionName
andversionCode
in thebuild.gradle
file now, you can also remove them from yourAndroidManifest.xml
file, if they are there.As I had to get only version code and check whether app is updated or not, if yes, I had to launch the playstore to get updated one. I did this way.
Then I saved version code using shared preference by creating an util class.
For xamarin users, use this code to get version name and code
1) Version Name:
2) Version Code:
This code was mentioned above in pieces but here it is again all included. You need a try/catch block because it may throw a "NameNotFoundException".
I hope this simplifies things for someone down the road. :)