From 3173e2cd234b2b7b914a284d0dbb6fd65ad350ed Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Wed, 15 Oct 2025 09:56:40 -0500 Subject: [PATCH] [xabt] pass in `$(SupportedOSPlatformVersion)` to `` Context: https://github.com/dotnet/android/pull/10194 Context: https://discord.com/channels/732297728826277939/732297837953679412/1427681737957441577 Fixes: https://github.com/dotnet/android/issues/6739 PR #10194 was originally meant to fix #6739, but did not when testing a sample project. The test written in #10194 passed even without the fixes, so we did not have a proper test case. After reviewing the failing project, I noticed `` was passed: AndroidManifestFile = D:\Downloads\test_sdk26\AndroidManifest.xml This is the developer's `AndroidManifest.xml` in their project, which should completely omit `` in favor of the `$(SupportedOSPlatformVersion)` MSBuild property. I suspect the fix for #10194 would have worked if `` was present. `` can run quite early, before we have generated the final `AndroidManifest.xml` file. So, a solution here is to pass in `$(SupportedOSPlatformVersion)` to ``. I also updated the test so it properly tests this scenario. --- .../Android/Xamarin.Android.Aapt2.targets | 2 ++ src/Xamarin.Android.Build.Tasks/Tasks/Aapt2.cs | 4 +++- .../Tasks/Aapt2Link.cs | 17 +++++++++++------ .../Tasks/AndroidResourceTests.cs | 17 ++++++++++++++++- .../Utilities/MonoAndroidHelper.cs | 14 ++++++++++++++ 5 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Aapt2.targets b/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Aapt2.targets index 497e0c0bbb6..411e06d32a9 100644 --- a/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Aapt2.targets +++ b/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Aapt2.targets @@ -166,6 +166,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved. JavaPlatformJarPath="$(JavaPlatformJarPath)" JavaDesignerOutputDirectory="$(ResgenTemporaryDirectory)" CompiledResourceFlatFiles="@(_CompiledFlatFiles)" + SupportedOSPlatformVersion="$(SupportedOSPlatformVersion)" AndroidManifestFile="$(_AndroidManifestAbs)" ManifestFiles="$(ResgenTemporaryDirectory)\AndroidManifest.xml" AdditionalAndroidResourcePaths="@(_LibraryResourceDirectories)" @@ -213,6 +214,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved. 0 }) { var doc = AndroidAppManifest.Load (AndroidManifestFile.ItemSpec, MonoAndroidHelper.SupportedVersions); if (doc.MinSdkVersion.HasValue) { - cmd.Add ("--min-sdk-version"); - cmd.Add (doc.MinSdkVersion.Value.ToString ()); + minSdkVersion = doc.MinSdkVersion.Value.ToString (CultureInfo.InvariantCulture); } } + // Use $(SupportedOSPlatformVersion) if minSdkVersion was not found in the manifest + if (minSdkVersion.IsNullOrEmpty () && MonoAndroidHelper.TryParseApiLevel (SupportedOSPlatformVersion, out Version version)) { + minSdkVersion = version.Major.ToString (CultureInfo.InvariantCulture); + } + if (!minSdkVersion.IsNullOrEmpty ()) { + cmd.Add ("--min-sdk-version"); + cmd.Add (minSdkVersion); + } return cmd.ToArray (); } diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/AndroidResourceTests.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/AndroidResourceTests.cs index f7c5c641d14..2610dd3c65d 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/AndroidResourceTests.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/AndroidResourceTests.cs @@ -5,7 +5,6 @@ using System.IO; using System.Linq; using Microsoft.Build.Framework; -using System.Text; using Xamarin.Android.Tasks; using Microsoft.Build.Utilities; @@ -162,6 +161,22 @@ public void AdaptiveIcon () var proj = new XamarinAndroidApplicationProject { SupportedOSPlatformVersion = "26", AndroidResources = { + new AndroidItem.AndroidResource ("Resources\\values\\colors.xml") { + TextContent = () => """ + + #2C3E50 + #FFFFFF + + """, + }, + new AndroidItem.AndroidResource ("Resources\\drawable\\ic_shortcut_add.xml") { + TextContent = () => """ + + + + + """, + }, new AndroidItem.AndroidResource ("Resources\\mipmap-anydpi-v26\\adaptiveicon.xml") { TextContent = () => """ diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs b/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs index 6634fa4acee..13102b06955 100644 --- a/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs +++ b/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs @@ -595,6 +595,20 @@ public static int ConvertSupportedOSPlatformVersionToApiLevel (string version) return apiLevel; } + public static bool TryParseApiLevel (string apiLevel, out Version version) + { + if (Version.TryParse (apiLevel, out var v)) { + version = v; + return true; + } + if (int.TryParse (apiLevel, out var major)) { + version = new Version (major, 0); + return true; + } + version = null; + return false; + } + #if MSBUILD public static string GetAssemblyAbi (ITaskItem asmItem) {