This question here describes my problem except I am working with Xamarin.
UPDATE: For clarity, the issue I see is that the first listed call below will not compile in Xamarin for the latest SDK. And the second crashes on any Android build less than 13. I have to support versions back to 9 so I need a way to detect packages that work in all versions since 9.
CALL 1: Fails to compile when targeting Android 13
packageinfo = PackageManager.GetPackageInfo(PackageName, 0);
CALL 2: Crashes on any Android version less than 13
packageinfo = PackageManager.GetPackageInfo(PackageName, PackageManager.PackageInfoFlags.Of(0));
The linked question solves the issue via a suppression mechanism:
fun PackageManager.getPackageInfoCompat(packageName: String, flags: Int = 0): PackageInfo =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(flags.toLong()))
} else {
@Suppress("DEPRECATION") getPackageInfo(packageName, flags)
}
This question is can this be done somehow in Xamarin via conditional compilation?
An "if" check of the SDK doesn't work as the block with the deprecated code in won't compile.
If not, is there another way a package can be detected that works in both 13 and pre-13?
In the Xamarin, you can try the following code:
PackageInfo packageinfo;
if(Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Tiramisu)
{
packageinfo = PackageManager.GetPackageInfo(PackageName, PackageManager.PackageInfoFlags.Of(0));
}
else
{
packageinfo = PackageManager.GetPackageInfo(PackageName, 0);
}