Repository navigation
[Mono.Android] Report obsolete APIs as compile errors - #13109
Conversation
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]>
There was a problem hiding this comment.
🟡 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
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]>
|
/review |
|
✅ Android PR Reviewer completed successfully!
|
There was a problem hiding this comment.
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`. |
There was a problem hiding this comment.
🤖 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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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]>
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |


Pull Request
title and
description
should follow the
commit-messages.mdworkflow 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(useDynamicDependencyAttribute),Android.Runtime.TypeManager(useJava.Interop.TypeManager), andAndroid.LinkerSafeAttribute(useAssemblyMetadata("IsTrimmable", "True")).Java.Interop.AndroidEventHelper(useJava.Interop.EventHelper),Android.Widget.ItemEventArgs(useAdapterView.ItemClickEventArgs), andJava.Interop.DoNotPackageAttribute(use@(AndroidExternalJavaLibrary); the old packaging behavior remains unsupported).Android.ReferenceFilesAttribute,Android.IncludeAndroidResourcesFromAttribute,Android.NativeLibraryReferenceAttribute, andJava.Interop.JavaLibraryReferenceAttributeremain available for existing binaries; their package-download behavior has been unsupported since .NET for Android 10.2.AudioRecord.RoutingChangedEventArgsandAudioTrack.RoutingChangedEventArgsremain for binary compatibility; these were incorrectly generated, unused wrappers. The underlying platform routing APIs remain bound, and the real event args areAudioRoutingOnRoutingChangedEventArgs.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 leeroypassed../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 --checkpassed.