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
?
Android System SettingsOnce in Settings, tap Apps and notifications (or your phone manufacturer's name it). When you're in there, tap See all xyz apps (where xyz is the number of apps you have installed). Now scroll down until you find the app you want to find the version for.
versionCode — A positive integer used as an internal version number. This number is used only to determine whether one version is more recent than another, with higher numbers indicating more recent versions. This is not the version number shown to users; that number is set by the versionName setting, below.
getPackageInfo(packageName, 0); return true; } catch (PackageManager. NameNotFoundException e) { return false; } } // ... // This will return true on Android 11 if the app is installed, // since we declared it above in the manifest. isPackageInstalled("com. example.
SDK_INT. The SDK version of the software currently running on this hardware device. This value never changes while a device is booted, but it may increase when the hardware manufacturer provides an OTA update.
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"
}
Use:
try {
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
String version = pInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
And you can get the version code by using this
int verCode = pInfo.versionCode;
Slightly shorter version if you just want the version name.
try{
String versionName = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return false;
}
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