Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add [SupportedOSPlatform] attributes to assemblies using ApiSince. #773

Merged
merged 1 commit into from
Jan 11, 2021

Conversation

jpobst
Copy link
Contributor

@jpobst jpobst commented Jan 6, 2021

Context: dotnet/android#5338

.NET 5 provides a new attribute [SupportedOSPlatform] to specify when an API is not available on all platforms or platform versions. This is used to build analyzers to give users warnings if they are trying to use an API when it will not be available on their target platform. This is fundamentally the same as our existing ApiSince, except tooling has actually been built to consume the information.

As such, we need generator support to put this information into Mono.Android.dll.

// Metadata.xml XPath method reference: path="/api/package[@name='android.app']/class[@name='Activity']/method[@name='dismissKeyboardShortcutsHelper' and count(parameter)=0]"
[global::System.Runtime.Versioning.SupportedOSPlatformAttribute ("android24.0")]
[Register ("dismissKeyboardShortcutsHelper", "()V", "", ApiSince = 24)]
public unsafe void DismissKeyboardShortcutsHelper ()
{
	const string __id = "dismissKeyboardShortcutsHelper.()V";
	try {
		_members.InstanceMethods.InvokeNonvirtualVoidMethod (__id, this, null);
	} finally {
	}
}

Some interesting notes:

  • This attribute is only available in .NET 5+, so we include a local version for earlier frameworks so we can compile without needing to #ifdef every attribute.
  • The attribute is marked as [Conditional ("NEVER")] so the attributes will not actually get compiled into the resulting pre-NET5.0 assembly.
  • The attribute cannot be placed on interfaces or fields, so we aren't able to annotate them.
  • Our minimum supported API for .NET 6 is 21, so we only write attributes for API added in versions newer than 21, as API added earlier are always available.

sw.WriteLine ("namespace System.Runtime.Versioning {");
sw.WriteLine (" [System.Diagnostics.Conditional(\"NEVER\")]");
sw.WriteLine (" [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Module | AttributeTargets.Property | AttributeTargets.Struct, AllowMultiple = true, Inherited = false)]");
sw.WriteLine (" public sealed class SupportedOSPlatformAttribute : Attribute {");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are defining this type, in a pre-.NET 5 context, shouldn't this attribute be internal, not public?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, making it public causes type conflicts when a pre-.NET 5 Mono.Android.dll is consumed by a .NET 5+ Android app. Marked internal.

@jpobst jpobst force-pushed the supported-platform branch from c438dfc to 6c2e1a4 Compare January 7, 2021 15:10
@jpobst jpobst marked this pull request as ready for review January 11, 2021 19:59
@jonpryor jonpryor merged commit da12df4 into master Jan 11, 2021
@jonpryor jonpryor deleted the supported-platform branch January 11, 2021 20:31
@jpobst jpobst added this to the 11.2 (16.10 / 8.10) milestone Feb 2, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants