You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Context: dotnet/java-interop#1081
Context: dotnet/java-interop@b274a67
Context: dotnet/android-libraries#690
Android libraries may use the [`androidx.annotation.RestrictTo`][0]
annotation to mark a `public` Java type as "not public":
// Java
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX)
public class DrawableWrapper extends Drawable {
}
Unfortunately, .NET Android didn't know about this annotation, so all
such annotated types were bound as *`public`* types:
// C# binding
public class DrawableWrapper : Drawable {
}
This is a problem because Google doesn't maintain API compatibility
for types with the `@RestrictTo` annotation. This can result in
undesirable API breakage; see also dotnet/android-libraries#690.
xamarin/java.interop#b274a67f updated `class-parse` to know about the
`@RestrictTo` annotation; when present, an `//*/@annotated-visibility`
attribute is present within `api.xml`:
<class
name="DrawableWrapper"
…
annotated-visibility="LIBRARY_GROUP_PREFIX"
/>
xamarin/java.interop#b274a67f also updated `generator` to support a
`generator --lang-features=restrict-to-attributes`; when present,
types with an `//*/@annotated-visibility` attribute will be marked
as `[Obsolete]` (*not* removed!), in order to maintain API
compatibility with existing ("broken") bindings, so that customers
can migrate away from these types:
// C# binding
[Obsolete
"While this type is 'public', Google considers it internal API and reserves the right to modify or delete it in the future. Use at your own risk.",
DiagnosticId = "XAOBS001")
partial class DrawableWrapper : Drawable {
}
The new `[Obsolete]` usage also specifies a custom warning code of
`XAOBS001` so that the warning can be suppressed if desired.
Add support for a new `$(AndroidEnableRestrictToAttributes)` MSBuild
"enum-style" property. Supported values include:
* `obsolete`: `generator --lang-features=restrict-to-attributes`
will be used and the `[Obsolete]` custom attribute will be placed
on bindings of Java types which have a `@RestrictTo` annotation.
This is the default value.
* `disable`: Java types with a `@RestrictTo` annotation
will *not* be marked as `[Obsolete]`, and will be bound as a
"normal" Java `public` type.
<AndroidEnableRestrictToAttributes>disable</AndroidEnableRestrictToAttributes>
If you would instead prefer that types with `@RestrictTo` not be
bound at all, this can be achieved via Metadata, e.g.
<remove-node path="//*[@annotated-visibility]" />
[0]: https://developer.android.com/reference/androidx/annotation/RestrictTo
Copy file name to clipboardExpand all lines: Documentation/guides/building-apps/build-properties.md
+32Lines changed: 32 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -485,6 +485,38 @@ Support for this property was added in Xamarin.Android 9.4.
485
485
486
486
This property is `False` by default.
487
487
488
+
489
+
## AndroidEnableRestrictToAttributes
490
+
491
+
An enum-style property with valid values of `obsolete` and `disable`.
492
+
493
+
When set to `obsolete`, types and members that are marked with the Java annotation
494
+
`androidx.annotation.RestrictTo`*or* are in non-exported Java packages will
495
+
be marked with an `[Obsolete]` attribute in the C# binding.
496
+
497
+
This `[Obsolete]` attribute has a descriptive message explaining that the
498
+
Java package owner considers the API to be "internal" and warns against its use.
499
+
500
+
This attribute also has a custom warning code `XAOBS001` so that it can be suppressed
501
+
independently of "normal" obsolete API.
502
+
503
+
When set to `disable`, API will be generated as normal with no additional
504
+
attributes. (This is the same behavior as before .NET 8.)
505
+
506
+
Adding `[Obsolete]` attributes instead of automatically removing the API was done to
507
+
preserve API compatibility with existing packages. If you would instead prefer to
508
+
*remove* members that have the `@RestrictTo` annotation *or* are in non-exported
509
+
Java packages, you can use [Transform files](https://learn.microsoft.com/xamarin/android/platform/binding-java-library/customizing-bindings/java-bindings-metadata#metadataxml-transform-file) in addition to
510
+
this property to prevent these types from being bound:
Copy file name to clipboardExpand all lines: src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets
0 commit comments