how to get build/version code and name of your Android application android programmatically

Version your app

You can define default values for these settings by including them in the defaultConfig {} block, nested inside the android {} block of your module's build.gradle file. You can then override these default values for different versions of your app by defining separate values for individual build types or product flavors. The following build.gradle file shows the versionCode and versionName settings in the defaultConfig {} block, as well as the productFlavors {} block.

android {
  ...
  defaultConfig {
    ...
    versionCode 2
    versionName "1.1"
  }
  productFlavors {
    demo {
      ...
      versionName "1.1-demo"
    }
    full {
      ...
    }

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.
The versionCode is integer value used to easily differentiate between new and old app versions. App developers must increment versionCode value when they release updates to their apps in Android Market, so it can determine if users are using an old version of the app, and offer them to update it.

versionName

A string used as the version number shown to users. This setting can be specified as a raw string or as a reference to a string resource. It is a string containing a regular “release version” as seen in other desktop applications, such as “1.4.5” or “3.7”. The versionName is just a “human readable” version code.


android get app version code programmatically

Check android version name for your application using below code like. Version 1.0 .
private String getApplicationVersion() {
            String versionStr;
        try {
            PackageInfo pInfo = context.getPackageManager().getPackageInfo(getPackageName(), 0);
            versionStr= "Version " + pInfo.versionName;
} catch (Exception e) {
            e.printStackTrace();
        }
return versionStr;
    }

Use the below function to get android app version code:
private String getApplicationVersionCode() {
            String verCode;
        try {
            PackageInfo pInfo = context.getPackageManager().getPackageInfo(getPackageName(), 0);
            int verCode = pInfo.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
return verCode ;
    }

'versionCode' is deprecated as of API 28: Android 9.0 (Pie)


No comments:

Post a Comment

Popular Posts