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 co
Solution 1:
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)
Solution 2:
My recommended solution:
Include this in your main build.gradle :
implementation 'androidx.appcompat:appcompat:1.0.2'then just use this code:
PackageInfopInfo= context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
long longVersionCode= PackageInfoCompat.getLongVersionCode(pInfo);
intversionCode= (int) longVersionCode; // avoid huge version numbers and you will be okIn case you have problems adding appcompat library then just use this alternative solution:
finalPackageInfopInfo= 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;
}
Solution 3:
Just for others using Xamarin, my answer was:
publiclongGetBuild()
{
varcontext= global::Android.App.Application.Context;
PackageManagermanager= context.PackageManager;
PackageInfoinfo= manager.GetPackageInfo(context.PackageName, 0);
return info.LongVersionCode;
}
Solution 4:
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()
}
Post a Comment for "Android Sdk 28 - Versioncode In Packageinfo Has Been Deprecated"