javaandroidandroid-9.0-piepackage-info

Android SDK 28 - versionCode in PackageInfo has been deprecated


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.


Solution

  • 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)