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
Useful for build systems: there is a file generated with your apk called output.json
which contains an array of information for each generated APK, including the versionName and versionCode.
eg
[
{
"apkInfo": {
"baseName": "x86-release",
"enabled": true,
"filterName": "x86",
"fullName": "86Release",
"outputFile": "x86-release-1.0.apk",
"splits": [
{
"filterType": "ABI",
"value": "x86"
}
],
"type": "FULL_SPLIT",
"versionCode": 42,
"versionName": "1.0"
},
"outputType": {
"type": "APK"
},
"path": "app-x86-release-1.0.apk",
"properties": {}
}
]
If you're using the Gradle plugin/Android Studio, as of version 0.7.0, version code and version name are available statically in BuildConfig
. Make sure you import your app's package, and not another BuildConfig
:
import com.yourpackage.BuildConfig;
...
int versionCode = BuildConfig.VERSION_CODE;
String versionName = BuildConfig.VERSION_NAME;
No Context object needed!
Also make sure to specify them in your build.gradle
file instead of the AndroidManifest.xml
.
defaultConfig {
versionCode 1
versionName "1.0"
}
Always do it with try catch
block:
String versionName = "Version not found";
try {
versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
Log.i(TAG, "Version Name: " + versionName);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
Log.e(TAG, "Exception Version Name: " + e.getLocalizedMessage());
}
val versionCode = BuildConfig.VERSION_CODE
val versionName = BuildConfig.VERSION_NAME
Make sure to import BuildConfig
into your class.
String versionCode = String.valueOf(BuildConfig.VERSION_CODE);
String versionName = String.valueOf(BuildConfig.VERSION_NAME);
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.xml
And 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.
for Api 28 the PackageInfo.versionCode is deprecated so use this code below:
Context context = getApplicationContext();
PackageManager manager = context.getPackageManager();
try {
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
myversionName = info.versionName;
versionCode = (int) PackageInfoCompat.getLongVersionCode(info);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
myversionName = "Unknown-01";
}