Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get application version programmatically in android [duplicate]

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?

like image 998
Fahad Ali Shaikh Avatar asked Jan 06 '11 14:01

Fahad Ali Shaikh


People also ask

How do I find the version of an app on my Android?

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.

What is versionCode and versionName in Android?

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.

How do you check app is installed or not in Android programmatically in Android 11?

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.

What is build version Sdk_int?

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.


3 Answers

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"
}
like image 96
Sam Dozor Avatar answered Nov 25 '22 22:11

Sam Dozor


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;
like image 23
plus- Avatar answered Nov 26 '22 00:11

plus-


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;
}
like image 38
scottyab Avatar answered Nov 26 '22 00:11

scottyab