Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] set %(DefineConstantsOnly) for older …
Browse files Browse the repository at this point in the history
…API levels (#8777)

Fixes: #8331
Context: #8569
Context: dotnet/sdk@25b360d

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.
  • Loading branch information
jonathanpeppers committed May 9, 2024
1 parent 1888e36 commit b340282
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 (); // </AndroidSdkSupportedTargetPlatformVersion>
}
writer.WriteStartElement ("SdkSupportedTargetPlatformVersion");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and
<PropertyGroup>
<AndroidNETSdkVersion>@ANDROID_PACK_VERSION_LONG@</AndroidNETSdkVersion>
<XamarinAndroidVersion>@ANDROID_PACK_VERSION_LONG@</XamarinAndroidVersion>
<_AndroidLatestStableApiLevel>@ANDROID_LATEST_STABLE_API_LEVEL@</_AndroidLatestStableApiLevel>
</PropertyGroup>
<PropertyGroup>
<_AndroidTargetingPackId Condition="$(TargetPlatformVersion.EndsWith('.0'))">$(TargetPlatformVersion.Substring(0, $(TargetPlatformVersion.LastIndexOf('.0'))))</_AndroidTargetingPackId>
<_AndroidTargetingPackId Condition="'$(_AndroidTargetingPackId)' == ''">$(TargetPlatformVersion)</_AndroidTargetingPackId>
<_AndroidRuntimePackId Condition=" '$(_AndroidTargetingPackId)' &lt; '@ANDROID_LATEST_STABLE_API_LEVEL@' ">@ANDROID_LATEST_STABLE_API_LEVEL@</_AndroidRuntimePackId>
<!-- NOTE: adjust if a TargetFramework supports multiple API levels -->
<_AndroidErrorOnTargetPlatformVersion Condition=" '$(_AndroidTargetingPackId)' != '$(_AndroidLatestStableApiLevel)' ">$(_AndroidTargetingPackId)</_AndroidErrorOnTargetPlatformVersion>
<_AndroidTargetingPackId Condition=" '$(_AndroidTargetingPackId)' != '$(_AndroidLatestStableApiLevel)' ">$(_AndroidLatestStableApiLevel)</_AndroidTargetingPackId>
<_AndroidRuntimePackId Condition=" '$(_AndroidRuntimePackId)' == '' ">$(_AndroidTargetingPackId)</_AndroidRuntimePackId>
<_AndroidRuntimePackId Condition=" '$(_AndroidRuntimePackId)' != '$(_AndroidLatestStableApiLevel)' ">$(_AndroidLatestStableApiLevel)</_AndroidRuntimePackId>
</PropertyGroup>
<ItemGroup>
<KnownFrameworkReference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,20 @@ public void IfAndroidJarDoesNotExistThrowXA5207 ([Values(true, false)] bool buil
Directory.Delete (AndroidSdkDirectory, recursive: true);
}

[Test]
public void InvalidTargetPlatformVersion ([Values ("android33", "android99.0")] string platformVersion)
{
const string targetFramework = "net9.0";
var project = new XamarinAndroidApplicationProject {
TargetFramework = $"{targetFramework}-{platformVersion}",
};
using var builder = CreateApkBuilder ();
builder.ThrowOnBuildFailure = false;
Assert.IsFalse (builder.Build (project), "build should fail");

Assert.IsTrue (builder.LastBuildOutput.ContainsText ("error NETSDK1140:"), "NETSDK1140 should have been raised.");
}

[Test]
public void XA4212 ()
{
Expand Down

0 comments on commit b340282

Please sign in to comment.