I have various implementations of a partial class DeviceServices
to provide platform specific implementations of certain device or OS specific features for Android, iOS and so on. My app is targeting API level 33.0 and the minimum version is API level 21.0.
Some APIs are specific to certain Android versions and higher, so I want to make sure that they only get called on the supported version. However, I always receive the following warning (and similar ones depending on the API being used):
warning CA1416: This call site is reachable on: 'Android' 21.0 and later. 'WindowInsets.Type.SystemBars()' is only supported on: 'android' 30.0 and later.
The following code to hide and show the system bars works on all my devices and the emulators that I have tried so far, but I am concerned about earlier Android versions. I still receive the warning above with it despite checking for the correct target API version:
static partial class DeviceServices
{
private static Activity _activity;
public static void SetActivity(Activity activity)
{
_activity = activity;
}
public static partial void HideSystemControls()
{
#if ANDROID30_0_OR_GREATER
if (Build.VERSION.SdkInt >= BuildVersionCodes.R) //R == API level 30.0
{
_activity?.Window?.InsetsController?.Hide(WindowInsets.Type.SystemBars());
}
#endif
}
public static partial void ShowSystemControls()
{
#if ANDROID30_0_OR_GREATER
if (Build.VERSION.SdkInt >= BuildVersionCodes.R) //R == API level 30.0
{
_activity?.Window?.InsetsController?.Show(WindowInsets.Type.SystemBars());
}
#endif
}
}
So, what is the correct way to do this? I'm not sure how to proceed. I've used platform-specific APIs plenty of times and never had any problems with it before, but this warning concerns me. I had a look at the support article about this warning already, too, but I didn't find it very helpful: https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1416. I also don't want to suppress the warnings. Maybe I am missing something here or can I simply ignore the warning in this case?
Update:
I am using Visual Studio 2022 17.4 Preview 2.1 and .NET 7.0 RC1.
I have also tried calling the APIs directly from within the MainActivity
, but keep receiving the same warnings after a rebuild.
Update 2:
Here is a sample repository where the problem can be reproduced, just uncomment the following code block in the MainActivity.cs file:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//#if ANDROID30_0_OR_GREATER
// if (Build.VERSION.SdkInt >= BuildVersionCodes.R) //R == API level 30.0
// {
// Window?.InsetsController?.Hide(WindowInsets.Type.SystemBars());
// }
//#endif
}