From 2f6c976e197f8b1376c6063386dd24927f78896b Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Thu, 3 Sep 2026 10:13:53 +0200 Subject: [PATCH 1/3] [xabt] Disable legacy assembly fixups for trimmable typemap Gate the untrimmed legacy resource, abstract method, and keep-alive assembly rewrites behind AndroidEnableLegacyCompatibilityAssemblyFixups. Preserve the existing default for LLVM-IR builds while disabling the fixups by default for the trimmable typemap. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../building-apps/build-properties.md | 13 +++++++ ...crosoft.Android.Sdk.TypeMap.LlvmIr.targets | 1 + ...soft.Android.Sdk.TypeMap.Trimmable.targets | 1 + .../Tasks/LinkAssembliesNoShrink.cs | 38 ++++++++++--------- .../Tasks/LinkerTests.cs | 27 +++++++++++++ .../Xamarin.Android.Common.targets | 2 + 6 files changed, 65 insertions(+), 17 deletions(-) diff --git a/Documentation/docs-mobile/building-apps/build-properties.md b/Documentation/docs-mobile/building-apps/build-properties.md index 475a57bbbdd..82bee080740 100644 --- a/Documentation/docs-mobile/building-apps/build-properties.md +++ b/Documentation/docs-mobile/building-apps/build-properties.md @@ -391,6 +391,19 @@ Store checks: XA1004, XA1005 and XA1006. Disabling these checks is useful for developers who are not targeting the Google Play Store and do not wish to run those checks. +## AndroidEnableLegacyCompatibilityAssemblyFixups + +A boolean property that controls whether untrimmed builds modify referenced +assemblies to support legacy binding and resource designer behavior. These +modifications include adding missing abstract interface methods, updating +legacy resource designer references, and inserting `GC.KeepAlive()` calls into +older Xamarin.Android binding assemblies. Trimmed builds using the `trimmable` +type map do not run these compatibility fixups. + +The default value is `False` when +[`$(AndroidTypeMapImplementation)`](#androidtypemapimplementation) is +`trimmable`, and `True` otherwise. + ## AndroidEnableMarshalMethods A bool property, that determines whether or not LLVM marshal methods are enabled. diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.TypeMap.LlvmIr.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.TypeMap.LlvmIr.targets index 7bb826023f8..00f25c4954e 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.TypeMap.LlvmIr.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.TypeMap.LlvmIr.targets @@ -11,6 +11,7 @@ + true <_RemoveRegisterFlag>$(MonoAndroidIntermediateAssemblyDir)shrunk\shrunk.flag <_BeforeAddStaticResources>$(_BeforeAddStaticResources);_GetMonoPlatformJarPath diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.TypeMap.Trimmable.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.TypeMap.Trimmable.targets index 3ce987089dd..a526701815c 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.TypeMap.Trimmable.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.TypeMap.Trimmable.targets @@ -12,6 +12,7 @@ Condition=" '$(_AndroidRuntime)' == 'NativeAOT' " /> + false <_TypeMapAssemblyName>_Microsoft.Android.TypeMaps <_TrimmableTypeMapPackageNamingPolicy Condition=" '$(_AndroidPackageNamingPolicySetByUser)' == 'true' ">$(AndroidPackageNamingPolicy) <_TrimmableTypeMapPackageNamingPolicy Condition=" '$(_TrimmableTypeMapPackageNamingPolicy)' == '' ">Crc64 diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs b/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs index cde9d9bc8f9..233a6cf88ae 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs @@ -15,27 +15,31 @@ public class LinkAssembliesNoShrink : AssemblyModifierPipeline public bool AddKeepAlives { get; set; } + public bool EnableLegacyCompatibilityAssemblyFixups { get; set; } = true; + public bool UseDesignerAssembly { get; set; } protected override void BuildPipeline (AssemblyPipeline pipeline, MSBuildLinkContext context) { - // FixAbstractMethodsStep - var fixAbstractMethodsStep = new FixAbstractMethodsStep (); - fixAbstractMethodsStep.Initialize (context); - pipeline.Steps.Add (fixAbstractMethodsStep); - - // FixLegacyResourceDesignerStep - if (UseDesignerAssembly) { - var fixLegacyResourceDesignerStep = new FixLegacyResourceDesignerStep (); - fixLegacyResourceDesignerStep.Initialize (context); - pipeline.Steps.Add (fixLegacyResourceDesignerStep); - } - - // AddKeepAlivesStep - if (AddKeepAlives) { - var addKeepAliveStep = new AddKeepAlivesStep (); - addKeepAliveStep.Initialize (context); - pipeline.Steps.Add (addKeepAliveStep); + if (EnableLegacyCompatibilityAssemblyFixups) { + // FixAbstractMethodsStep + var fixAbstractMethodsStep = new FixAbstractMethodsStep (); + fixAbstractMethodsStep.Initialize (context); + pipeline.Steps.Add (fixAbstractMethodsStep); + + // FixLegacyResourceDesignerStep + if (UseDesignerAssembly) { + var fixLegacyResourceDesignerStep = new FixLegacyResourceDesignerStep (); + fixLegacyResourceDesignerStep.Initialize (context); + pipeline.Steps.Add (fixLegacyResourceDesignerStep); + } + + // AddKeepAlivesStep + if (AddKeepAlives) { + var addKeepAliveStep = new AddKeepAlivesStep (); + addKeepAliveStep.Initialize (context); + pipeline.Steps.Add (addKeepAliveStep); + } } // Ensure the task's steps are added diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/LinkerTests.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/LinkerTests.cs index 93ce973c01d..a5d9a80f5a5 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/LinkerTests.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/LinkerTests.cs @@ -22,6 +22,33 @@ public class LinkerTests : BaseTest void Logger (TraceLevel level, string message) => TestContext.WriteLine ($"{level}: {message}"); + [TestCase (false)] + [TestCase (true)] + public void LinkAssembliesNoShrinkLegacyCompatibilityFixups (bool enabled) + { + var task = new TestableLinkAssembliesNoShrink { + AddKeepAlives = true, + BuildEngine = new MockBuildEngine (TestContext.Out), + EnableLegacyCompatibilityAssemblyFixups = enabled, + UseDesignerAssembly = true, + }; + var resolver = new DirectoryAssemblyResolver (Logger, false); + using var pipeline = new AssemblyPipeline (resolver); + var context = new MSBuildLinkContext (resolver, task.Log); + + task.BuildPipelineForTest (pipeline, context); + + Assert.AreEqual (enabled, pipeline.Steps.Any (step => step is FixAbstractMethodsStep)); + Assert.AreEqual (enabled, pipeline.Steps.Any (step => step is FixLegacyResourceDesignerStep)); + Assert.AreEqual (enabled, pipeline.Steps.Any (step => step is AddKeepAlivesStep)); + } + + sealed class TestableLinkAssembliesNoShrink : LinkAssembliesNoShrink + { + public void BuildPipelineForTest (AssemblyPipeline pipeline, MSBuildLinkContext context) => + BuildPipeline (pipeline, context); + } + [Test] public void FixAbstractMethodsStep_SkipDimMembers () { diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets index 0ee09c07b87..6153f39a165 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets @@ -969,6 +969,7 @@ because xbuild doesn't support framework reference assemblies. <_PropertyCacheItems Include="AndroidAddKeepAlives=$(AndroidAddKeepAlives)" /> <_PropertyCacheItems Include="AndroidAotMode=$(AndroidAotMode)" /> <_PropertyCacheItems Include="AndroidEmbedProfilers=$(AndroidEmbedProfilers)" /> + <_PropertyCacheItems Include="AndroidEnableLegacyCompatibilityAssemblyFixups=$(AndroidEnableLegacyCompatibilityAssemblyFixups)" /> <_PropertyCacheItems Include="AndroidEnableProfiledAot=$(AndroidEnableProfiledAot)" /> <_PropertyCacheItems Include="AndroidDexTool=$(AndroidDexTool)" /> <_PropertyCacheItems Include="AndroidLinkTool=$(AndroidLinkTool)" /> @@ -1468,6 +1469,7 @@ because xbuild doesn't support framework reference assemblies. ResolvedUserAssemblies="@(ResolvedUserAssemblies)" SourceFiles="@(ResolvedAssemblies)" DestinationFiles="@(ResolvedAssemblies->'$(MonoAndroidIntermediateAssemblyDir)%(DestinationSubPath)')" + EnableLegacyCompatibilityAssemblyFixups="$(AndroidEnableLegacyCompatibilityAssemblyFixups)" TargetName="$(TargetName)" AddKeepAlives="$(AndroidAddKeepAlives)" UseDesignerAssembly="$(AndroidUseDesignerAssembly)" From 1eeeb749b2bed89996aa840bb8206c312d9d579e Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Thu, 3 Sep 2026 10:45:44 +0200 Subject: [PATCH 2/3] [xabt] Address legacy fixup review feedback Document the untrimmed-only scope, log skipped compatibility fixups, preserve coverage for the common assembly pipeline, and exercise the trimmable default and explicit opt-in through a full build test. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../building-apps/build-properties.md | 9 ++++- .../Tasks/LinkAssembliesNoShrink.cs | 2 ++ .../Tasks/LinkerTests.cs | 35 +++++++++++++++---- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/Documentation/docs-mobile/building-apps/build-properties.md b/Documentation/docs-mobile/building-apps/build-properties.md index 82bee080740..634d78840db 100644 --- a/Documentation/docs-mobile/building-apps/build-properties.md +++ b/Documentation/docs-mobile/building-apps/build-properties.md @@ -51,6 +51,12 @@ object collection. The default value is `True` for Release configuration builds. +For untrimmed builds using the `trimmable` type map, `GC.KeepAlive()` calls are +only inserted when +[`$(AndroidEnableLegacyCompatibilityAssemblyFixups)`](#androidenablelegacycompatibilityassemblyfixups) +is also `True`. Trimmed builds using the `trimmable` type map do not insert +these calls. + ## AndroidAotAdditionalArguments A string property that allows @@ -398,7 +404,8 @@ assemblies to support legacy binding and resource designer behavior. These modifications include adding missing abstract interface methods, updating legacy resource designer references, and inserting `GC.KeepAlive()` calls into older Xamarin.Android binding assemblies. Trimmed builds using the `trimmable` -type map do not run these compatibility fixups. +type map do not run these compatibility fixups, and setting this property to +`True` does not enable them for trimmed builds. The default value is `False` when [`$(AndroidTypeMapImplementation)`](#androidtypemapimplementation) is diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs b/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs index 233a6cf88ae..1db0d896a7a 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs @@ -40,6 +40,8 @@ protected override void BuildPipeline (AssemblyPipeline pipeline, MSBuildLinkCon addKeepAliveStep.Initialize (context); pipeline.Steps.Add (addKeepAliveStep); } + } else { + Log.LogDebugMessage ("Skipping legacy compatibility assembly fixups. Set `AndroidEnableLegacyCompatibilityAssemblyFixups` to `true` to enable them."); } // Ensure the task's steps are added diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/LinkerTests.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/LinkerTests.cs index a5d9a80f5a5..b3e9542d247 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/LinkerTests.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/LinkerTests.cs @@ -38,9 +38,15 @@ public void LinkAssembliesNoShrinkLegacyCompatibilityFixups (bool enabled) task.BuildPipelineForTest (pipeline, context); - Assert.AreEqual (enabled, pipeline.Steps.Any (step => step is FixAbstractMethodsStep)); - Assert.AreEqual (enabled, pipeline.Steps.Any (step => step is FixLegacyResourceDesignerStep)); - Assert.AreEqual (enabled, pipeline.Steps.Any (step => step is AddKeepAlivesStep)); + Assert.AreEqual (enabled, pipeline.Steps.Any (step => step is FixAbstractMethodsStep), + $"{nameof (FixAbstractMethodsStep)} presence should match the compatibility fixup setting."); + Assert.AreEqual (enabled, pipeline.Steps.Any (step => step is FixLegacyResourceDesignerStep), + $"{nameof (FixLegacyResourceDesignerStep)} presence should match the compatibility fixup setting."); + Assert.AreEqual (enabled, pipeline.Steps.Any (step => step is AddKeepAlivesStep), + $"{nameof (AddKeepAlivesStep)} presence should match the compatibility fixup setting."); + Assert.IsTrue (pipeline.Steps.Any (step => step is FindJavaObjectsStep), $"{nameof (FindJavaObjectsStep)} should always run."); + Assert.IsTrue (pipeline.Steps.Any (step => step is SaveChangedAssemblyStep), $"{nameof (SaveChangedAssemblyStep)} should always run."); + Assert.IsTrue (pipeline.Steps.Any (step => step is FindTypeMapObjectsStep), $"{nameof (FindTypeMapObjectsStep)} should always run."); } sealed class TestableLinkAssembliesNoShrink : LinkAssembliesNoShrink @@ -400,23 +406,35 @@ static IEnumerable Get_AndroidAddKeepAlivesData () AddTestData (isRelease: true, setAndroidAddKeepAlivesTrue: false, setLinkModeNone: true, shouldAddKeepAlives: true, runtime); } + AddTestData (isRelease: false, setAndroidAddKeepAlivesTrue: true, setLinkModeNone: false, + shouldAddKeepAlives: false, AndroidRuntime.CoreCLR, typeMapImplementation: "trimmable"); + AddTestData (isRelease: false, setAndroidAddKeepAlivesTrue: true, setLinkModeNone: false, + shouldAddKeepAlives: true, AndroidRuntime.CoreCLR, typeMapImplementation: "trimmable", + enableLegacyCompatibilityAssemblyFixups: true); + return ret; - void AddTestData (bool isRelease, bool setAndroidAddKeepAlivesTrue, bool setLinkModeNone, bool shouldAddKeepAlives, AndroidRuntime runtime) + void AddTestData (bool isRelease, bool setAndroidAddKeepAlivesTrue, bool setLinkModeNone, + bool shouldAddKeepAlives, AndroidRuntime runtime, string? typeMapImplementation = null, + bool enableLegacyCompatibilityAssemblyFixups = false) { ret.Add (new object[] { isRelease, setAndroidAddKeepAlivesTrue, setLinkModeNone, shouldAddKeepAlives, - runtime + runtime, + typeMapImplementation, + enableLegacyCompatibilityAssemblyFixups, }); } } [Test] [TestCaseSource (nameof (Get_AndroidAddKeepAlivesData))] - public void AndroidAddKeepAlives (bool isRelease, bool setAndroidAddKeepAlivesTrue, bool setLinkModeNone, bool shouldAddKeepAlives, AndroidRuntime runtime) + public void AndroidAddKeepAlives (bool isRelease, bool setAndroidAddKeepAlivesTrue, bool setLinkModeNone, + bool shouldAddKeepAlives, AndroidRuntime runtime, string? typeMapImplementation, + bool enableLegacyCompatibilityAssemblyFixups) { if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) { return; @@ -473,6 +491,8 @@ public unsafe bool MyMethod (Android.OS.IBinder windowToken, [global::Android.Ru proj.SetRuntime (runtime); proj.SetProperty ("AllowUnsafeBlocks", "True"); + if (!typeMapImplementation.IsNullOrEmpty ()) + proj.SetProperty ("AndroidTypeMapImplementation", typeMapImplementation); // We don't want `[TargetPlatform ("android35")]` to get set because we don't do AddKeepAlives on .NET for Android assemblies proj.SetProperty ("GenerateAssemblyInfo", "False"); @@ -483,6 +503,9 @@ public unsafe bool MyMethod (Android.OS.IBinder windowToken, [global::Android.Ru if (setLinkModeNone) proj.SetProperty (isRelease ? proj.ReleaseProperties : proj.DebugProperties, "AndroidLinkMode", "None"); + if (enableLegacyCompatibilityAssemblyFixups) + proj.SetProperty ("AndroidEnableLegacyCompatibilityAssemblyFixups", "True"); + using (var b = CreateApkBuilder ()) { Assert.IsTrue (b.Build (proj), "Building a project should have succeded."); From e79504f257d4753da9cda0d6bbb9d305a7a1878f Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Thu, 3 Sep 2026 15:28:21 +0200 Subject: [PATCH 3/3] [xabt] Fix legacy fixup diagnostics build Import the build task logging extensions required by the new diagnostic and clarify that the task property initializer is only the direct-caller fallback. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs b/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs index 1db0d896a7a..fe198ba306a 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs @@ -1,6 +1,7 @@ #nullable enable using System; +using Microsoft.Android.Build.Tasks; using MonoDroid.Tuner; namespace Xamarin.Android.Tasks @@ -15,6 +16,7 @@ public class LinkAssembliesNoShrink : AssemblyModifierPipeline public bool AddKeepAlives { get; set; } + // MSBuild supplies the typemap-specific default. Keep direct task callers backward compatible. public bool EnableLegacyCompatibilityAssemblyFixups { get; set; } = true; public bool UseDesignerAssembly { get; set; }