From 60dbcc9fe482dddc1085c003d3a18209709cbb35 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Fri, 1 Mar 2024 08:49:50 -0600 Subject: [PATCH] [Xamarin.Android.Build.Tasks] set `%(DefineConstantsOnly)` for older API levels (#8777) Fixes: https://github.com/xamarin/xamarin-android/issues/8331 Context: https://github.com/xamarin/xamarin-android/pull/8569 Context: https://github.com/dotnet/sdk/commit/25b360d5799b022ea538a13bac52246f1a1b936a Building for `net9.0-android33` would give a poor error message: error NETSDK1181: Error getting pack version: Pack 'Microsoft.Android.Ref.33' was not present in workload manifests. C:\Program Files\dotnet\sdk\8.0.100-preview.7.23376.3\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.FrameworkReferenceResolution.targets To solve this, you would either change 33 to 34, or just remove the number to rely on the default value. To make this easier: * Automatically switch to 34 if the user specifies 33 or less. * Opt into `%(DefineConstantsOnly)=true` for API levels 21-33 (and not the latest), which allows the .NET SDK to emit the *better* .NET SDK error message instead. So now users will get: (_CheckForInvalidTargetPlatformVersion target) -> dotnet/sdk/9.0.100-alpha.1.23628.5/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.TargetFrameworkInference.targets(243,5): error NETSDK1140: 33.0 is not a valid TargetPlatformVersion for Android. Valid versions include: 34.0 I added a test for this scenario. --- .../GenerateSupportedPlatforms.cs | 3 +++ ...icrosoft.Android.Sdk.BundledVersions.in.targets | 6 +++++- .../Tests/Xamarin.Android.Build.Tests/BuildTest.cs | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/build-tools/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks/GenerateSupportedPlatforms.cs b/build-tools/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks/GenerateSupportedPlatforms.cs index 9d3d6d0e0ae..868020a51ae 100644 --- a/build-tools/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks/GenerateSupportedPlatforms.cs +++ b/build-tools/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks/GenerateSupportedPlatforms.cs @@ -78,6 +78,9 @@ public override bool Execute () .OrderBy (v => v)) { writer.WriteStartElement ("AndroidSdkSupportedTargetPlatformVersion"); writer.WriteAttributeString ("Include", apiLevel.ToString ("0.0", CultureInfo.InvariantCulture)); + if (apiLevel < TargetApiLevel) { + writer.WriteAttributeString ("DefineConstantsOnly", "true"); + } writer.WriteEndElement (); // } writer.WriteStartElement ("SdkSupportedTargetPlatformVersion"); diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/in/Microsoft.Android.Sdk.BundledVersions.in.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/in/Microsoft.Android.Sdk.BundledVersions.in.targets index 607626dc3b1..e5b1d9ee266 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/in/Microsoft.Android.Sdk.BundledVersions.in.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/in/Microsoft.Android.Sdk.BundledVersions.in.targets @@ -10,12 +10,16 @@ WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and @ANDROID_PACK_VERSION_LONG@ @ANDROID_PACK_VERSION_LONG@ + <_AndroidLatestStableApiLevel>@ANDROID_LATEST_STABLE_API_LEVEL@ <_AndroidTargetingPackId Condition="$(TargetPlatformVersion.EndsWith('.0'))">$(TargetPlatformVersion.Substring(0, $(TargetPlatformVersion.LastIndexOf('.0')))) <_AndroidTargetingPackId Condition="'$(_AndroidTargetingPackId)' == ''">$(TargetPlatformVersion) - <_AndroidRuntimePackId Condition=" '$(_AndroidTargetingPackId)' < '@ANDROID_LATEST_STABLE_API_LEVEL@' ">@ANDROID_LATEST_STABLE_API_LEVEL@ + + <_AndroidErrorOnTargetPlatformVersion Condition=" '$(_AndroidTargetingPackId)' != '$(_AndroidLatestStableApiLevel)' ">$(_AndroidTargetingPackId) + <_AndroidTargetingPackId Condition=" '$(_AndroidTargetingPackId)' != '$(_AndroidLatestStableApiLevel)' ">$(_AndroidLatestStableApiLevel) <_AndroidRuntimePackId Condition=" '$(_AndroidRuntimePackId)' == '' ">$(_AndroidTargetingPackId) + <_AndroidRuntimePackId Condition=" '$(_AndroidRuntimePackId)' != '$(_AndroidLatestStableApiLevel)' ">$(_AndroidLatestStableApiLevel)