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
?
I don't see the need to get it from package manager
There are some ways to get
versionCode
andversionName
programmatically.PackageManager
. This is the best way for most cases.BuildConfig.java
. But notice, that if you'll access this values in library it will return library version, not apps one, that uses this library. So use only in non-library projects!There are some details, except of using second way in library project. In new android gradle plugin (3.0.0+) some functionalities removed. So, for now, i.e. setting different version for different flavors not working correct.
Incorrect way:
Code above will correctly set values in
BuildConfig
, but fromPackageManager
you'll receive0
andnull
if you didn't set version indefault
configuration. So your app will have0
version code on device.There is a workaround - set version for output
apk
file manually:In Activity file
I hope it will help you...!
Here is the method for getting the version code:
Someone who does’t need BuildConfig info for application's UI however wants to use these info for setting a CI job configuration or others, like me.
There is a automatically generated file, BuildConfig.java, under your project directory as long as you build your project successfully.
{WORKSPACE}/build/generated/source/buildConfig/{debug|release}/{PACKAGE}/BuildConfig.java
Split information you need by python script or other tools. Here’s an example:
Please excuse my limited English ability, but hope this helps.
A very simple way is :