Showing posts with label package-info. Show all posts
Showing posts with label package-info. Show all posts

versioncode deprecated android

versionCode

public int versionCode
pinfo.versioncode was deprecated in API level 28 (Added in API level 1).
Use getLongVersionCode() instead, which includes both this and the additional versionCodeMajor attribute. The version number of this package, as specified by the <manifest> tag's versionCode attribute.
'versionCode' is deprecated as of API 28: Android 9.0 (Pie)

getLongVersionCode is an API 28 method, though, so consider using PackageInfoCompat. It has one static method : getLongVersionCode(PackageInfo info)

PackageInfoCompat.getLongVersionCode (PackageInfo info)

public static long getLongVersionCode (PackageInfo info)
Return R.attr.versionCode and R.attr.versionCodeMajor combined together as a single long value. The versionCodeMajor is placed in the upper 32 bits on Android P or newer, otherwise these bits are all set to 0.

Recommended solution:
If your have included below appcompat dependency this in your app level build.gradle :
implementation 'androidx.appcompat:appcompat:1.0.2'

then just use this function code:
public static long appVersionCode() {
try {
    PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
    long longVersionCode= PackageInfoCompat.getLongVersionCode(pInfo);
    return  (int) longVersionCode;
    } catch (PackageManager.NameNotFoundException e) {
          e.printStackTrace();
  }
   return 0;
}

Alternative solution in case you have not used appcompat library then just use as below :
final PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
int versionCode;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    versionCode = (int) pInfo.getLongVersionCode(); // avoid huge version numbers and you will be ok
} else {
    //noinspection deprecation
    versionCode = pInfo.versionCode;
}

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)


Popular Posts