[Xamarin.Android.Build.Tasks] Add support for 'AndroidEnableRestrictToAttributes'.#7990
Merged
[Xamarin.Android.Build.Tasks] Add support for 'AndroidEnableRestrictToAttributes'.#7990
Conversation
dellis1972
approved these changes
Apr 26, 2023
jonathanpeppers
approved these changes
Apr 26, 2023
jonpryor
reviewed
Apr 27, 2023
|
|
||
| Support for this property was added in .NET 8. | ||
|
|
||
| This property is `True` by default. |
Contributor
There was a problem hiding this comment.
Should we also add:
If you would instead prefer to *remove* members that have the `@RestrictTo` annotation *or* are in non-exported 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) to prevent these types from being bound:
<attr path="//class[@annotated-visibility]"
name="visibility">kotlin-internal</attr>
<attr path="//interface[@annotated-visibility]"
name="visibility">kotlin-internal</attr>
Contributor
There was a problem hiding this comment.
…formatted properly to use XML block formatting, as appropriate.
Contributor
Author
There was a problem hiding this comment.
Updated using <remove-node>.
Contributor
|
We're gonna change the boolean MSBuild property to an "enum" property. |
Contributor
|
WIP commit message (will need updating for enum semantics): Context: https://github.com/xamarin/java.interop/issues/1081
Context: https://github.com/xamarin/java.interop/commit/b274a67f38e2f4012ab8414cba2c778f53f0b13c
Context: https://github.com/xamarin/AndroidX/issues/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 xamarin/AndroidX#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 |
grendello
added a commit
to grendello/xamarin-android
that referenced
this pull request
May 4, 2023
* main: Bump to dotnet/installer@0ce891843a 8.0.100-preview.5.23228.7 (dotnet#7994) [ci] Automatically retry failed emulator tests. (dotnet#7997) [xaprepare] Combine 'AndroidTestDependencies' and 'EmulatorTestDependencies' scenarios. (dotnet#8006) [xaprepare] Provision 'platform-33_r02' (dotnet#8004) [Xamarin.Android.Build.Tasks] Add AndroidEnableRestrictToAttributes (dotnet#7990) [ci] Remove plots-to-appinsights. (dotnet#8002) [Xamarin.Android.Build.Tasks] remove NuGet dependencies (dotnet#8000) Bump to 33.0.56 $(AndroidNet7Version) (dotnet#7998) $(AndroidPackVersionSuffix)=preview.5; net8 is 34.0.0-preview.5 (dotnet#7996)
jonathanpeppers
pushed a commit
that referenced
this pull request
Aug 15, 2023
…7990) 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context: dotnet/java-interop#1081
Context: dotnet/java-interop#1094
Adds support for emitting
[Obsolete]warnings for API marked withandroidx.annotation.RestrictTo. These warnings inform the user that the package owner does not consider an API to be "public". These warnings have a custom warning code (XAOBS001) that can be suppressed if desired.This is enabled by default with the value
obsolete, but can be disabled by setting$(AndroidEnableRestrictToAttributes)todisable.