52

I just upgraded my app's compileSdkVersion to 28 (Pie).

I'm getting a compilation warning:

warning: [deprecation] versionCode in PackageInfo has been deprecated

The warning is coming from this code:

final PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
int versionCode = info.versionCode;

I looked at the documentation, but it doesn't say anything about how to resolve this issue or what should be used instead of the deprecated field.

Doron Yakovlev Golani
  • 5,188
  • 9
  • 36
  • 60
  • See also [this previous SO question](https://stackoverflow.com/questions/52630237/what-is-versioncodemajor-and-what-is-the-difference-with-versioncode/53007631#53007631) – shadowsheep Jul 13 '19 at 20:38

6 Answers6

66

It says what to do on the Java doc (I recommend not using the Kotlin documentation for much; it's not really maintained well):

versionCode

This field was deprecated in API level 28. Use getLongVersionCode() instead, which includes both this and the additional versionCodeMajor attribute. The version number of this package, as specified by the tag's versionCode attribute.

This is an API 28 method, though, so consider using PackageInfoCompat. It has one static method:

getLongVersionCode(PackageInfo info)
Community
  • 1
  • 1
TheWanderer
  • 16,775
  • 6
  • 49
  • 63
  • 1
    See also [this SO question](https://stackoverflow.com/questions/52630237/what-is-versioncodemajor-and-what-is-the-difference-with-versioncode/53007631#53007631) – shadowsheep Jul 13 '19 at 20:37
51

My recommended solution:

Include this in your main build.gradle :

implementation 'androidx.appcompat:appcompat:1.0.2'

then just use this code:

PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
long longVersionCode= PackageInfoCompat.getLongVersionCode(pInfo);
int versionCode = (int) longVersionCode; // avoid huge version numbers and you will be ok

In case you have problems adding appcompat library then just use this alternative solution:

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;
}
JerabekJakub
  • 5,268
  • 4
  • 26
  • 33
Blackd
  • 833
  • 6
  • 10
10

Just for others using Xamarin, my answer was:

public long GetBuild()
{
    var context = global::Android.App.Application.Context;
    PackageManager manager = context.PackageManager;
    PackageInfo info = manager.GetPackageInfo(context.PackageName, 0);

    return info.LongVersionCode;
}
Phil
  • 1,852
  • 2
  • 28
  • 55
  • Note you can also support older SDKs by wrapping this code with a check of the SDK version: `if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.P)` – Ken Pespisa Jul 31 '20 at 15:21
  • it's an Int not a long!! – user1034912 Sep 10 '21 at 08:55
  • no it's not @user1034912 . It's even in the variable name 'longVersionCode' rather than the older 'versioncode'. That's actually the whole point of this issue, it used to be int, then changed to long. – Phil Sep 12 '21 at 17:22
6

Here the solution in kotlin:

val versionCode: Long =
    if (Build.VERSION.SDK_INT >= VERSION_CODES.P) {
           packageManager.getPackageInfo(packageName, 0).longVersionCode
    } else {
            packageManager.getPackageInfo(packageName, 0).versionCode.toLong()
    }
Noelia
  • 3,698
  • 4
  • 17
  • 22
2

In Kotlin simple code to get versionCode in PackageInfo

val packageInfo = this.packageManager.getPackageInfo(this.packageName, 0)
val verCode = PackageInfoCompat.getLongVersionCode(packageInfo).toInt()
Rohit S
  • 714
  • 5
  • 7
0

As of API 33, from a library:

<applicationContext>.run {
    val info =
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) 
            packageManager.getPackageInfo(packageName, 0)
        else packageManager.getPackageInfo(packageName,
            PackageManager.PackageInfoFlags.of(0))
    PackageInfoCompat.getLongVersionCode(info)
}

From your own app, this is still a valid answer: https://stackoverflow.com/a/21119027/2115403

rtsketo
  • 1,168
  • 11
  • 18