From 16be72c9ab1a3d7c184f38bef093a704b11a6697 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Thu, 10 Sep 2026 11:48:57 -0500 Subject: [PATCH 1/2] [r8] Validate obfuscation mode in task Validate AndroidR8ObfuscationMode only when R8 actually executes, avoiding fragile application-property target conditions while reporting XA1050 for unsupported values. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- Documentation/docs-mobile/TOC.yml | 2 ++ Documentation/docs-mobile/messages/index.md | 1 + Documentation/docs-mobile/messages/xa1050.md | 34 +++++++++++++++++++ .../Properties/Resources.Designer.cs | 9 +++++ .../Properties/Resources.resx | 5 +++ src/Xamarin.Android.Build.Tasks/Tasks/R8.cs | 5 +++ .../Tasks/R8Tests.cs | 17 ++++++++++ 7 files changed, 73 insertions(+) create mode 100644 Documentation/docs-mobile/messages/xa1050.md diff --git a/Documentation/docs-mobile/TOC.yml b/Documentation/docs-mobile/TOC.yml index 3faf90e458e..b4fe476a5fd 100644 --- a/Documentation/docs-mobile/TOC.yml +++ b/Documentation/docs-mobile/TOC.yml @@ -272,6 +272,8 @@ href: messages/xa1048.md - name: XA1049 href: messages/xa1049.md + - name: XA1050 + href: messages/xa1050.md - name: "XA2xxx: Linker" items: - name: "XA2xxx: Linker" diff --git a/Documentation/docs-mobile/messages/index.md b/Documentation/docs-mobile/messages/index.md index 88e7609ec63..abe2d010109 100644 --- a/Documentation/docs-mobile/messages/index.md +++ b/Documentation/docs-mobile/messages/index.md @@ -159,6 +159,7 @@ Either change the value in the AndroidManifest.xml to match the $(SupportedOSPla + [XA1047](xa1047.md): Required attribute '{0}' missing from element '{1}'; {2} line {3}. + [XA1048](xa1048.md): '{0}' does not contain an <instrumentation> element. + [XA1049](xa1049.md): The 'AndroidEnableMarshalMethods' and 'PublishReadyToRun' MSBuild properties cannot both be set to 'true'. ++ [XA1050](xa1050.md): The 'AndroidR8ObfuscationMode' MSBuild property has an invalid value. ## XA2xxx: Linker diff --git a/Documentation/docs-mobile/messages/xa1050.md b/Documentation/docs-mobile/messages/xa1050.md new file mode 100644 index 00000000000..1f984daa7ba --- /dev/null +++ b/Documentation/docs-mobile/messages/xa1050.md @@ -0,0 +1,34 @@ +--- +title: .NET for Android error XA1050 +description: XA1050 error code +ms.date: 09/10/2026 +f1_keywords: + - "XA1050" +--- + +# .NET for Android error XA1050 + +## Example messages + +```dotnetcli +error XA1050: The 'AndroidR8ObfuscationMode' MSBuild property has an invalid value of 'private-member'. Valid values are 'disabled' and 'private-members'. +``` + +## Issue + +The [`$(AndroidR8ObfuscationMode)`](../building-apps/build-properties.md#androidr8obfuscationmode) +MSBuild property contains an unsupported value. This property is validated when +R8 runs. + +## Solution + +Set `$(AndroidR8ObfuscationMode)` to one of the supported values: + +```xml + + private-members + +``` + +Use `disabled` to preserve all Java names and use the legacy non-optimizing +Android R8 configuration. diff --git a/src/Xamarin.Android.Build.Tasks/Properties/Resources.Designer.cs b/src/Xamarin.Android.Build.Tasks/Properties/Resources.Designer.cs index c813ec95a02..13986a46813 100644 --- a/src/Xamarin.Android.Build.Tasks/Properties/Resources.Designer.cs +++ b/src/Xamarin.Android.Build.Tasks/Properties/Resources.Designer.cs @@ -1024,6 +1024,15 @@ public static string XA1049 { return ResourceManager.GetString("XA1049", resourceCulture); } } + + /// + /// Looks up a localized string similar to The 'AndroidR8ObfuscationMode' MSBuild property has an invalid value of '{0}'. Valid values are 'disabled' and 'private-members'.. + /// + public static string XA1050 { + get { + return ResourceManager.GetString("XA1050", resourceCulture); + } + } /// /// Looks up a localized string similar to Use of AppDomain.CreateDomain() detected in assembly: {0}. .NET 6 and higher will only support a single AppDomain, so this API will no longer be available in .NET for Android once .NET 6 is released.. diff --git a/src/Xamarin.Android.Build.Tasks/Properties/Resources.resx b/src/Xamarin.Android.Build.Tasks/Properties/Resources.resx index daf6297d574..c85742d1345 100644 --- a/src/Xamarin.Android.Build.Tasks/Properties/Resources.resx +++ b/src/Xamarin.Android.Build.Tasks/Properties/Resources.resx @@ -1149,6 +1149,11 @@ To use a custom JDK path for a command line build, set the 'JavaSdkDirectory' MS The 'AndroidEnableMarshalMethods' and 'PublishReadyToRun' MSBuild properties cannot both be set to 'true'. Set one property to 'false'. The following are literal names and should not be translated: AndroidEnableMarshalMethods, PublishReadyToRun, MSBuild, true, false. + + The 'AndroidR8ObfuscationMode' MSBuild property has an invalid value of '{0}'. Valid values are 'disabled' and 'private-members'. + The following are literal names and should not be translated: AndroidR8ObfuscationMode, MSBuild, disabled, private-members. +{0} - The invalid value of the AndroidR8ObfuscationMode MSBuild property. + Java dependency '{0}' is not satisfied. The following are literal names and should not be translated: Java. diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/R8.cs b/src/Xamarin.Android.Build.Tasks/Tasks/R8.cs index ad89bebeb29..207a66de83f 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/R8.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/R8.cs @@ -49,6 +49,11 @@ public class R8 : D8 public override bool RunTask () { + if (!string.Equals (ObfuscationMode, "disabled", StringComparison.OrdinalIgnoreCase) && + !string.Equals (ObfuscationMode, "private-members", StringComparison.OrdinalIgnoreCase)) { + Log.LogCodedError ("XA1050", Properties.Resources.XA1050, ObfuscationMode); + return false; + } try { return base.RunTask (); } finally { diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/R8Tests.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/R8Tests.cs index 1362ba24aed..7d99cfb60fd 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/R8Tests.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/R8Tests.cs @@ -1,4 +1,6 @@ +using System.Collections.Generic; using System.IO; +using Microsoft.Build.Framework; using NUnit.Framework; using Xamarin.Android.Tasks; @@ -77,5 +79,20 @@ public void WriteDisabledObfuscationRules () Assert.AreEqual ("-dontobfuscate" + System.Environment.NewLine, writer.ToString ()); } + + [Test] + public void InvalidObfuscationModeReportsXA1050 () + { + var errors = new List (); + var task = new R8 { + BuildEngine = new MockBuildEngine (TestContext.Out, errors), + ObfuscationMode = "private-member", + }; + + Assert.IsFalse (task.Execute (), "Task should reject an invalid obfuscation mode."); + Assert.AreEqual (1, errors.Count, "Task should report one error."); + Assert.AreEqual ("XA1050", errors [0].Code); + StringAssert.Contains ("'private-member'", errors [0].Message); + } } } From 5badb5733cb6f43d3a4d4143056a92f3eb60211f Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Thu, 10 Sep 2026 12:36:10 -0500 Subject: [PATCH 2/2] [r8] Validate obfuscation mode before R8 builds Move XA1050 validation to the MSBuild target introduced by the release-branch fix and gate it on AndroidLinkTool=r8, so unrelated builds are unaffected. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- src/Xamarin.Android.Build.Tasks/Tasks/R8.cs | 5 ----- .../PackagingTest.cs | 21 +++++++++++++++++++ .../Tasks/R8Tests.cs | 17 --------------- .../Xamarin.Android.Common.targets | 9 ++++++++ 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/R8.cs b/src/Xamarin.Android.Build.Tasks/Tasks/R8.cs index 207a66de83f..ad89bebeb29 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/R8.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/R8.cs @@ -49,11 +49,6 @@ public class R8 : D8 public override bool RunTask () { - if (!string.Equals (ObfuscationMode, "disabled", StringComparison.OrdinalIgnoreCase) && - !string.Equals (ObfuscationMode, "private-members", StringComparison.OrdinalIgnoreCase)) { - Log.LogCodedError ("XA1050", Properties.Resources.XA1050, ObfuscationMode); - return false; - } try { return base.RunTask (); } finally { diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs index f16643f4ba3..c93ff852fb4 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs @@ -62,6 +62,27 @@ public void CheckR8MetadataFilesExist ( } } + [TestCase ("", true)] + [TestCase ("r8", false)] + public void InvalidR8ObfuscationModeIsValidatedOnlyForR8 (string linkTool, bool expectedResult) + { + var proj = new XamarinAndroidApplicationProject { + IsRelease = true, + }; + proj.SetProperty (proj.ReleaseProperties, KnownProperties.AndroidLinkTool, linkTool); + proj.SetProperty (proj.ReleaseProperties, KnownProperties.AndroidR8ObfuscationMode, "private-member"); + + using var builder = CreateApkBuilder (); + builder.Target = "_ValidateAndroidR8ObfuscationMode"; + builder.ThrowOnBuildFailure = false; + builder.AutomaticNuGetRestore = false; + Assert.AreEqual (expectedResult, builder.Build (proj), "Validation result should depend on whether R8 is enabled."); + if (!expectedResult) { + StringAssertEx.Contains ("error XA1050", builder.LastBuildOutput); + StringAssertEx.Contains ("'private-member'", builder.LastBuildOutput); + } + } + [Test] public void CheckR8InfoMessagesToNotBreakTheBuild ([Values (AndroidRuntime.CoreCLR, AndroidRuntime.NativeAOT)] AndroidRuntime runtime) { diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/R8Tests.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/R8Tests.cs index 7d99cfb60fd..1362ba24aed 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/R8Tests.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/R8Tests.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; using System.IO; -using Microsoft.Build.Framework; using NUnit.Framework; using Xamarin.Android.Tasks; @@ -79,20 +77,5 @@ public void WriteDisabledObfuscationRules () Assert.AreEqual ("-dontobfuscate" + System.Environment.NewLine, writer.ToString ()); } - - [Test] - public void InvalidObfuscationModeReportsXA1050 () - { - var errors = new List (); - var task = new R8 { - BuildEngine = new MockBuildEngine (TestContext.Out, errors), - ObfuscationMode = "private-member", - }; - - Assert.IsFalse (task.Execute (), "Task should reject an invalid obfuscation mode."); - Assert.AreEqual (1, errors.Count, "Task should report one error."); - Assert.AreEqual ("XA1050", errors [0].Code); - StringAssert.Contains ("'private-member'", errors [0].Message); - } } } diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets index 711bfc8d1ba..2feb9d14bbf 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets @@ -3078,6 +3078,15 @@ because xbuild doesn't support framework reference assemblies. Condition=" '$(AndroidApplication)' == 'True' "/> + + + +