Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 627
Doron Yakovlev Golani Avatar asked Oct 24 '18 20:10

Doron Yakovlev Golani


5 Answers

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)
like image 166
TheWanderer Avatar answered Oct 07 '22 18:10

TheWanderer


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;
}
like image 45
Blackd Avatar answered Oct 07 '22 20:10

Blackd


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;
}
like image 27
Phil Avatar answered Oct 07 '22 18:10

Phil


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()
    }
like image 36
Noelia Avatar answered Oct 07 '22 19:10

Noelia


In Kotlin simple code to get versionCode in PackageInfo

val packageInfo = this.packageManager.getPackageInfo(this.packageName, 0)
val verCode = PackageInfoCompat.getLongVersionCode(packageInfo).toInt()
like image 39
Rohit S Avatar answered Oct 07 '22 19:10

Rohit S