From 54b8e3ea28224e3c5b7ad365d25f6b4c1d16e3f3 Mon Sep 17 00:00:00 2001 From: Sven Boemer Date: Fri, 9 Jan 2026 11:10:31 -0800 Subject: [PATCH 1/2] Migrate WarnOnPreserveAttribute from ILLink step to MSBuild task Replace the ILLink custom step with an MSBuild task that runs before _RunILLink. This simplifies the build pipeline by removing a dependency on the ILLink step infrastructure while maintaining the same behavior. The new CheckForObsoletePreserveAttribute task uses System.Reflection.Metadata for fast metadata scanning instead of Mono.Cecil. --- .../WarnOnPreserveAttribute.cs | 38 ------------ .../Microsoft.Android.Sdk.ILLink.targets | 10 +-- .../CheckForObsoletePreserveAttribute.cs | 61 +++++++++++++++++++ 3 files changed, 67 insertions(+), 42 deletions(-) delete mode 100644 src/Microsoft.Android.Sdk.ILLink/WarnOnPreserveAttribute.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CheckForObsoletePreserveAttribute.cs diff --git a/src/Microsoft.Android.Sdk.ILLink/WarnOnPreserveAttribute.cs b/src/Microsoft.Android.Sdk.ILLink/WarnOnPreserveAttribute.cs deleted file mode 100644 index ec845dad5ca..00000000000 --- a/src/Microsoft.Android.Sdk.ILLink/WarnOnPreserveAttribute.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using Mono.Cecil; -using Mono.Linker; -using Mono.Linker.Steps; -using Xamarin.Android.Tasks; - -namespace Microsoft.Android.Sdk.ILLink -{ - /// - /// This step provides warnings when an assembly references the obsolete PresrveAttribute. - /// The PreserveAttribute used to indicate to the linker that a type or member should not be trimmed. - /// It had similar functionality to the newer DynamicDependencyAttribute, but was Android-specific and is now obsolete. - /// - public class WarnOnPreserveAttribute : BaseStep { - protected override void ProcessAssembly (AssemblyDefinition assembly) - { - foreach (var module in assembly.Modules) { - foreach (var tr in module.GetTypeReferences ()) { - if (tr is { - Namespace: "Android.Runtime", - Name: "PreserveAttribute", - }) { - Context.LogMessage (MessageContainer.CreateCustomWarningMessage( - Context, - $"Assembly '{assembly.Name.Name}' contains reference to obsolete attribute 'Android.Runtime.PreserveAttribute'. Members with this attribute may be trimmed. Please use System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute instead", - 6001, - new MessageOrigin (), - WarnVersion.ILLink0)); - return; - } - } - } - } - } -} diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.ILLink.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.ILLink.targets index c0c7ff7de09..f0acc0b574c 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.ILLink.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.ILLink.targets @@ -9,6 +9,8 @@ This file contains the .NET 5-specific targets to customize ILLink + + <_TrimmerCustomData Include="SystemIOHashingAssemblyPath" Value="$(_SystemIOHashingAssemblyPath)" /> - <_TrimmerCustomSteps - Include="$(_AndroidLinkerCustomStepAssembly)" - BeforeStep="MarkStep" - Type="Microsoft.Android.Sdk.ILLink.WarnOnPreserveAttribute" />