Skip to content

[Mono.Android] Report obsolete APIs as compile errors - #13109

Merged
simonrozsival merged 3 commits into
mainfrom
simonrozsival-remove-obsolete-classes
Oct 14, 2026
Merged

simonrozsival merged 3 commits into
mainfrom
simonrozsival-remove-obsolete-classes

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Pull Request
title and
description
should follow the
commit-messages.md workflow documentation, and in particular should include:

  • Useful description of why the change is necessary. Removing these API definitions can prevent the trimmer from resolving third-party assemblies that already reference them, even when the APIs are not otherwise used. Keep the types in both reference and runtime assemblies for binary compatibility, while marking obsolete APIs with Obsolete(error: true) so newly compiled C# cannot use them. Constructors intentionally remain non-throwing so existing attribute/reflection consumers are not broken at runtime.

    Kept and marked obsolete errors:

    • Android.Runtime.PreserveAttribute (use DynamicDependencyAttribute), Android.Runtime.TypeManager (use Java.Interop.TypeManager), and Android.LinkerSafeAttribute (use AssemblyMetadata("IsTrimmable", "True")).
    • Java.Interop.AndroidEventHelper (use Java.Interop.EventHelper), Android.Widget.ItemEventArgs (use AdapterView.ItemClickEventArgs), and Java.Interop.DoNotPackageAttribute (use @(AndroidExternalJavaLibrary); the old packaging behavior remains unsupported).
    • Android.ReferenceFilesAttribute, Android.IncludeAndroidResourcesFromAttribute, Android.NativeLibraryReferenceAttribute, and Java.Interop.JavaLibraryReferenceAttribute remain available for existing binaries; their package-download behavior has been unsupported since .NET for Android 10.2.
    • AudioRecord.RoutingChangedEventArgs and AudioTrack.RoutingChangedEventArgs remain for binary compatibility; these were incorrectly generated, unused wrappers. The underlying platform routing APIs remain bound, and the real event args are AudioRoutingOnRoutingChangedEventArgs.

    Deprecated Java/Android platform bindings are not removed while the corresponding APIs remain in the platform libraries. The release note and XA0121/XA0122 documentation now describe the compatibility behavior accurately.

  • Links to issues fixed: N/A; this change follows up on PR review feedback.

  • Unit tests: make leeroy passed. ./dotnet-local.sh test src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Xamarin.Android.Build.Tests.csproj --filter 'Name~WarnWithReferenceToLegacyPreserveAttribute' passed (1/1); the build's API compatibility check reported 0 issues. git diff --check passed.

Remove public types already marked obsolete by .NET 9 or earlier. This lets .NET for Android 12 stop shipping stale compatibility APIs while keeping bindings for platform APIs that remain available. Document migration guidance and update the API snapshots, compatibility baseline, linker configuration, and diagnostics.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 13, 2026 09:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

An existing binding test now references a removed type, and two documentation claims do not match the implementation.

3 open findings
What changed in this PR

Removes obsolete Mono.Android compatibility APIs for .NET for Android 12 and updates compatibility records, linker configuration, tests, and migration documentation.

Changes:

  • Removes twelve obsolete public types and related build/linker entries.
  • Records intentional API breaks across all supported API snapshots.
  • Updates legacy diagnostics, regression coverage, and migration guidance.
File Description
tests/​api-compatibility/​api-compat-exclude-attributes.txt Stops excluding the removed preserve attribute.
tests/​api-compatibility/​acceptable-breakages-vReference-net11.0.txt Records intentional removals.
src/​Xamarin.Android.Build.Tasks/​Xamarin.Android.Build.Tasks.csproj Removes obsolete linked attribute sources.
src/​Xamarin.Android.Build.Tasks/​Tests/​Xamarin.Android.Build.Tests/​Tasks/​LinkerTests.cs Tests legacy PreserveAttribute detection.
src/​Mono.Android/​PublicAPI/​API-37/​PublicAPI.Shipped.txt Updates API 37 surface.
src/​Mono.Android/​PublicAPI/​API-37.1/​PublicAPI.Shipped.txt Updates API 37.1 surface.
src/​Mono.Android/​PublicAPI/​API-37.2/​PublicAPI.Shipped.txt Updates API 37.2 surface.
src/​Mono.Android/​PublicAPI/​API-36/​PublicAPI.Shipped.txt Updates API 36 surface.
src/​Mono.Android/​PublicAPI/​API-36.1/​PublicAPI.Shipped.txt Updates API 36.1 surface.
src/​Mono.Android/​PublicAPI/​API-35/​PublicAPI.Shipped.txt Updates API 35 surface.
src/​Mono.Android/​Mono.Android.csproj Excludes removed source files.
src/​Mono.Android/​Java.Interop/​JavaLibraryReferenceAttribute.cs Removes obsolete attribute.
src/​Mono.Android/​Java.Interop/​DoNotPackageAttribute.cs Removes obsolete packaging attribute.
src/​Mono.Android/​Java.Interop/​AndroidEventHelper.cs Removes obsolete event helper.
src/​Mono.Android/​ILLink/​ILLink.LinkAttributes.xml Removes stale preserve metadata.
src/​Mono.Android/​Android/​ReferenceFilesAttribute.cs Removes unsupported base attribute.
src/​Mono.Android/​Android/​NativeLibraryReferenceAttribute.cs Removes unsupported native-library attribute.
src/​Mono.Android/​Android/​LinkerSafeAttribute.cs Removes obsolete trimming attribute.
src/​Mono.Android/​Android/​IncludeAndroidResourcesFromAttribute.cs Removes unsupported resource attribute.
src/​Mono.Android/​Android.Widget/​AdapterView.cs Removes obsolete event arguments.
src/​Mono.Android/​Android.Runtime/​TypeManager.cs Removes forwarding compatibility API.
src/​Mono.Android/​Android.Runtime/​PreserveAttribute.cs Removes obsolete preservation attribute.
src/​Mono.Android/​Android.Media/​AudioTrack.cs Removes invalid nested event arguments.
src/​Mono.Android/​Android.Media/​AudioRecord.cs Removes invalid nested event arguments.
src/​Microsoft.Android.Sdk.ILLink/​PreserveLists/​Mono.Android.xml Removes deleted types from preservation.
Documentation/​release-notes/​obsolete-api-removals.md Documents breaking changes and migrations.
Documentation/​docs-mobile/​messages/​xa0122.md Updates DoNotPackage guidance.
Documentation/​docs-mobile/​messages/​xa0121.md Updates legacy attribute guidance.
Documentation/​docs-mobile/​features/​layout-code-behind/​index.md Replaces obsolete preservation example.

🧠 Review effort: Balanced

Comment thread src/Mono.Android/Mono.Android.csproj Resolved
Remove the obsolete attribute from the embedded-jar binding test so it exercises the supported binding build without relying on removed functionality. Align the layout sample with generated output and clarify that XA0122 is historical.

Co-authored-by: Copilot App <[email protected]>
@simonrozsival simonrozsival added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Oct 13, 2026
@simonrozsival
simonrozsival enabled auto-merge (squash) October 13, 2026 14:03
@simonrozsival

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #13109

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Needs Changes — 0 errors, 1 warning, 0 suggestions.

The API-removal implementation is coherent, the compatibility exceptions cover the removed public types, and the legacy PreserveAttribute diagnostic has regression coverage. The migration note needs correction before publication: DynamicDependencyAttribute cannot directly replace every supported PreserveAttribute placement or its AllMembers/Conditional semantics.

CI is green: Azure DevOps build #1633243 and all reported GitHub checks succeeded.

Generated by Android PR Reviewer for #13109 · copilot · gpt56 · 123.5 AIC · ⌖ 11.1 AIC · ⊞ 26.4K
Comment /review to run again

platform libraries.

- `Android.Runtime.PreserveAttribute` is replaced by
`System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🤖 ⚠️ Documentation — DynamicDependencyAttribute is not a drop-in replacement for all uses of PreserveAttribute. PreserveAttribute could be placed on types, properties, events, interfaces, and delegates and also exposed AllMembers and Conditional; DynamicDependencyAttribute can only be placed on constructors, fields, and methods and needs an explicit dependency/member selection. For example, replacing [Preserve (AllMembers = true)] on a class with the documented attribute does not compile. Please make this migration usage-specific (including how to map AllMembers and Conditional) instead of presenting one universal replacement.

Rule: Migration guidance must preserve API semantics

| AttributeTargets.Event
| AttributeTargets.Interface
| AttributeTargets.Delegate)]
public sealed class PreserveAttribute : Attribute {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In dotnet/macios we've tried to remove APIs before, and we've always gotten burned and had to bring them back. The big problem is the trimmer: if a third-party NuGet uses the [Preserve] attribute (or any removed APIs really), the trimmer will fail and the project won't build (even if the API in question is never actually used).

In other words: this might break a number of existing NuGets.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder if there is a way to remove them from the "ref" pack -- the Mono.Android.dll reference assembly.

But leave them in the runtime pack's Mono.Android.dll passed to the trimmer.

This would leave the trimmer working for existing libraries, but you couldn't use them from C# code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The big problem is the trimmer: if a third-party NuGet uses the [Preserve] attribute (or any removed APIs really), the trimmer will fail and the project won't build (even if the API in question is never actually used).

I see. Then let's just make them [Obsolete("...", error: true)] + throw in the constructor. That's good enough I think.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

But leave them in the runtime pack's Mono.Android.dll passed to the trimmer.

I think that would work but I don't think that's necessary.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We’ve updated #13109 to keep the legacy API types available for trimmer and binary compatibility, while marking obsolete APIs as compile-time errors. We’re leaving their constructors non-throwing to avoid breaking existing reflection/attribute consumers.

Auto-replied by the GitHub Copilot app

Keep the obsolete API types in Mono.Android so previously compiled libraries can still resolve attribute and member references during trimming. Mark the remaining obsolete declarations as errors for newly compiled source, update compatibility records and migration docs, and retain constructor behavior so legacy reflection remains unaffected.

Co-authored-by: Copilot App <[email protected]>
@simonrozsival simonrozsival changed the title [Mono.Android] Remove APIs obsoleted by .NET 9 [Mono.Android] Report obsolete APIs as compile errors Oct 13, 2026
@simonrozsival

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@simonrozsival
simonrozsival merged commit 5379844 into main Oct 14, 2026
41 checks passed
@simonrozsival
simonrozsival deleted the simonrozsival-remove-obsolete-classes branch October 14, 2026 18:19
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants