Skip to content

Decorate all APIs with correct SupportedOSPlatform attributes #5338

Description

@marek-safar

As part of netcore migration, we need to correctly annotate all APIs with .NET 6 to work correctly with existing tooling (primary existing analyzers). The attributes already exist in .NET5 and could be extended if necessary.

Available attributes are listed at https://github.com/dotnet/designs/blob/main/accepted/2020/platform-checks/platform-checks.md#attributes

Activity

  1. AmrAlSayed0 commented on Nov 27, 2020

    @AmrAlSayed0

    After reading the Platform compatibility analyzer doc page I saw attributes like [SupportedOSPlatform("Android6.0")]. I'm curious if the effort for this issue is going to include the android only API? For example:

    //This is the C# class that is a binding of android.view.View
    //(https://developer.android.com/reference/android/view/View.html)
    public class View
    {
        [SupportedOSPlatform("Android8.0")] // This means that this property can only be used on Android 8.0 or above
        //which corresponds to API 26 or above since this is the version it was added in
        //(https://developer.android.com/reference/android/view/View.html#getTooltipText())
        public string TooltipText { get; set; }
    }

    Or is only the net5.0 API going to be annotated?

  2. marek-safar commented on Nov 27, 2020

    @marek-safar
    ContributorAuthor

    This issue is for Android APIs only

  3. added this to the One .NET milestone on Dec 1, 2020
  4. jonathanpeppers commented on Dec 2, 2020

    @jonathanpeppers
    Member

    I think the attributes will actually be the API level such as:

    [SupportedOSPlatform("android30")]

    See: dotnet/runtime#43531

    We'll have to see if we have to see if we have to put android30.0, I but I think we should try leaving off the .0.

  5. jonathanpeppers commented on Dec 2, 2020

    @jonathanpeppers
    Member

    Correction: https://docs.microsoft.com/dotnet/standard/analyzers/platform-compat-analyzer#how-the-analyzer-determines-platform-dependency

    major.minor is required

    So I think we will have to use android30.0, etc.

  6. jonpryor commented on Dec 2, 2020

    @jonpryor
    Contributor

    Additionally, SupportedOSPlatform is only available in .NET 5+; thus, generator output will need to wrap it into a #if NET5 block:

    	// Metadata.xml XPath class reference: path="/api/package[@name='android.app']/class[@name='Activity']"
    	[global::Android.Runtime.Register ("android/app/Activity", DoNotGenerateAcw=true)]
    #if NET5_0_OR_GREATER
    	[global: System.Runtime.Versioning.SupportedOSPlatform ("android")]
    #endif  // NET5_0_OR_GREATER
    	public partial class Activity {
    		public virtual unsafe bool IsActivityTransitionRunning {
    			// Metadata.xml XPath method reference: path="/api/package[@name='android.app']/class[@name='Activity']/method[@name='isActivityTransitionRunning' and count(parameter)=0]"
    			[Register ("isActivityTransitionRunning", "()Z", "GetIsActivityTransitionRunningHandler", ApiSince = 26)]
    #if NET5_0_OR_GREATER
    			[global::System.Runtime.Versioning.SupportedOSPlatform ("android26.0")]
    #endif  // NET5_0_OR_GREATER
    			get {…}
    		}
    	}

    Related: https://github.com/dotnet/designs/blob/main/accepted/2020/or-greater-defines/or-greater-defines.md#doc-changes

  7. jpobst commented on Dec 2, 2020

    @jpobst
    Contributor

    I think it's probably easier to define our own [SupportedOSPlatform] attribute and include it if not targeting net5.0+.

  8. marek-safar commented on Dec 2, 2020

    @marek-safar
    ContributorAuthor

    I think it's probably easier to define our own [SupportedOSPlatform] attribute and include it if not targeting net5.0+.

    You could public [Conditional("NEVER")] on the local version of SupportedOSPlatformAttribute to be always removed by compiler for non-netcore builds

  9. jpobst commented on Dec 2, 2020

    @jpobst
    Contributor

    Implementation details aside, the good news is we do already have this data and already put it in our bindings, just in a different place (ApiSince), so it won't be hard to add the new annotations.

    [global::Android.Runtime.Register ("android/accessibilityservice/FingerprintGestureController$FingerprintGestureCallback", DoNotGenerateAcw=true, ApiSince = 26)]
    public abstract partial class FingerprintGestureCallback : Java.Lang.Object
    {
      [Register (".ctor", "()V", "", ApiSince = 26)]
      public unsafe FingerprintGestureCallback () : base (IntPtr.Zero, JniHandleOwnership.DoNotTransfer)
      {
        ...
      }
    }
    

    We will probably need to manually audit our hand-bound code also.

  10. jpobst commented on May 26, 2021

    @jpobst
    Contributor

    This has been implemented in Java.Interop:
    dotnet/java-interop@412e974

    And bumped in XA:
    #5945

  11. ghost locked as resolved and limited conversation to collaborators on Jun 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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