Home > Back-end >  Is there a replacement for the PackageManager.GetPackageInfo(PackageName, 0) in Xamarin?
Is there a replacement for the PackageManager.GetPackageInfo(PackageName, 0) in Xamarin?

Time:11-19

I have a Xamarin Android App in which I need to check the VersionName. Here is the function call:

var appVersion = PackageManager.GetPackageInfo(PackageName, 0).VersionName;

It has been working fine. All of a sudden with the last Android OS update, the function call is deprecated. I prefer not to use Xamarin Essentials. Doing that forces me to use older Nuget packages. Does anyone know what the preferred way will be to get the Version Name in Xamarin going forward?

Thanks, Jim

CodePudding user response:

As far as I remember it was moved to PackageInfoCompat

So try something like this:

    private int GetVersion(Context context)
    {
        PackageInfo pInfo = context.PackageManager.GetPackageInfo(context.PackageName, 0);
        var longVersionCode = PackageInfoCompat.GetLongVersionCode(pInfo);
        int versionCode = (int)longVersionCode;
        return versionCode;
    }

It's off the top of my head so you might have to edit a thing or two

Good luck.

CodePudding user response:

You need:

if (Build.VERSION.SdkInt >= BuildVersionCodes.Tiramisu)
       packageInfo = packageManager.GetPackageInfo(packageName, PackageManager.PackageInfoFlags.Of(0));
else
    {
       #pragma warning disable CS0618 // Type or member is obsolete
       packageInfo = packageManager.GetPackageInfo(packageName, 0); 
       #pragma warning restore CS0618 // Type or member is obsolete
   }
    
if (packageInfo != null)
   versionName = packageInfo.VersionName;
  • Related