Skip to content

System.Obsolete attribute used instead of ObsoletedOSPlatform attribute #7558

@tipa

Description

@tipa

Android application type

Android for .NET (net6.0-android, etc.)

Affected platform version

VS 2022 17.4.1, .NET 7

Description

I am using this property to get the installed app version:

public static string VersionString => (OperatingSystem.IsAndroidVersionAtLeast(33) ?
   Application.Context.PackageManager.GetPackageInfo(PackageName, PackageManager.PackageInfoFlags.Of(0)) :
   Application.Context.PackageManager.GetPackageInfo(PackageName, 0)).VersionName;

The old (now deprecated) GetPackageInfo method still emits this warning, even though OperatingSystem.IsAndroidVersionAtLeast is used:
Warning CS0618 'PackageManager.GetPackageInfo(string, PackageInfoFlags)' is obsolete: 'deprecated'

This is because of this attribute:
[System.Obsolete("deprecated")]

I think this attribute should be used instead:
[ObsoletedOSPlatform("android30.0")]

I know this is rather nitpicking, but there might be other usages of the old Obsolete attribute that could be replaced with the ObsoletedOSPlatform attribute. In the PackageManager class alone there are also these methods currently annotated with the old attribute:
https://developer.android.com/reference/android/content/pm/PackageManager#addPreferredActivity(android.content.IntentFilter,%20int,%20android.content.ComponentName[],%20android.content.ComponentName)
https://developer.android.com/reference/android/content/pm/PackageManager#getPackageGids(java.lang.String,%20int)
https://developer.android.com/reference/android/content/pm/PackageManager#removePackageFromPreferred(java.lang.String)
https://developer.android.com/reference/android/content/pm/PackageManager#addPackageToPreferred(java.lang.String)

Steps to Reproduce

  1. Create new .NET 6 project
  2. Add property as shown above
  3. Build app / check warnings window

Did you find any workaround?

No response

Relevant log output

No response

Metadata

Metadata

Assignees

Labels

Area: Mono.AndroidIssues with the Android API binding (Mono.Android.dll).

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions