Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Xamarin.Android.Build.Tasks] introduce XA1038 #8569

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Documentation/guides/messages/xa1038.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
title: Xamarin.Android error XA1038
description: XA1038 error code
ms.date: 12/6/2023
---
# Xamarin.Android error XA1038

## Example messages

```
error XA1038: $(TargetPlatformVersion) 33 is not a valid target for `net8.0-android` projects. Please update your $(TargetPlatformVersion) to a supported version (e.g. 34).
```

## Issue

This error indicates that you have a mismatch between the
`$(TargetPlatformVersion)` set and what .NET Android supports.

For example:

```xml
<PropertyGroup>
<TargetFramework>net8.0-android33</TargetFramework>
</PropertyGroup>
```

In this case, .NET 8 targets Android 34, which is the default and valid
`$(TargetPlatformVersion)`, but the `$(TargetPlatformVersion)` is set to 33.
`TargetFramework=net8.0-android33` above is shorthand for:

```xml
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetPlatformIdentifier>android</TargetPlatformIdentifier>
<TargetPlatformVersion>33.0</TargetPlatformVersion>
</PropertyGroup>
```

## Solution

Change the value of the `$(TargetPlatformVersion)` property to match a supported
version for the `$(TargetFramework)`, or remove the value entirely to rely on
the default:

```xml
<PropertyGroup>
<TargetFramework>net8.0-android</TargetFramework>
</PropertyGroup>
```
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
7 changes: 7 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1006,4 +1006,11 @@ To use a custom JDK path for a command line build, set the 'JavaSdkDirectory' MS
{0} - The deprecated MSBuild property name
{1} - The numeric version of .NET</comment>
</data>
<data name="XA1038" xml:space="preserve">
<value>$(TargetPlatformVersion) {0} is not a valid target for '{1}' projects. Please update your $(TargetPlatformVersion) to a supported version (e.g. {2}).</value>
<comment>The following are literal names and should not be translated: $(TargetPlatformVersion).
{0} - the invalid $(TargetPlatformVersion) such as 33
{1} - the $(TargetFramework) such as net8.0-android
{2} - the valid $(TargetPlatformVersion) such as 34</comment>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,28 @@ public void IfAndroidJarDoesNotExistThrowXA5207 ([Values(true, false)] bool buil
Directory.Delete (AndroidSdkDirectory, recursive: true);
}

[Test]
public void InvalidTargetPlatformVersion ([Values ("android33", "android35.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 (int.TryParse (platformVersion.Replace ("android", "").Replace (".0", ""), out var apiLevel), "failed to parse API level!");

int maxApiLevel = AndroidSdkResolver.GetMaxInstalledPlatform ();
if (apiLevel > maxApiLevel) {
Assert.IsTrue (builder.LastBuildOutput.ContainsText ("error NETSDK1140:"), "NETSDK1140 should have been raised.");
} else {
var message = $"error XA1038: $(TargetPlatformVersion) {apiLevel} is not a valid target for '{targetFramework}' projects.";
Assert.IsTrue (builder.LastBuildOutput.ContainsText (message), "XA1038 should have been raised.");
}
}

[Test]
public void XA4212 ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,11 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
ResourceName="XA1035"
Condition=" '$(BundleAssemblies)' == 'true' and '$(UsingAndroidNETSdk)' == 'true' "
/>
<AndroidError Code="XA1038"
ResourceName="XA1038"
Condition=" '$(_AndroidErrorOnTargetPlatformVersion)' != '' "
FormatArguments="$(_AndroidErrorOnTargetPlatformVersion);net$(TargetFrameworkVersion.TrimStart('vV'));$(_AndroidLatestStableApiLevel)"
/>
</Target>

<!--
Expand Down
Loading