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

[XABT] Prefer SupportedOSPlatformVersion over minSdkVersion #8026

Merged
merged 15 commits into from
Jun 1, 2023
Merged
2 changes: 1 addition & 1 deletion Configuration.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<TargetFrameworkVersion Condition=" '$(TargetFrameworkVersion)' == '' And '$(UsingMicrosoftNETSdk)' != 'true' And '$(_StandardLibraryPath)' != '' ">v4.7.2</TargetFrameworkVersion>
<TargetFrameworkVersion Condition=" '$(TargetFrameworkVersion)' == '' And '$(UsingMicrosoftNETSdk)' != 'true' ">v4.7.1</TargetFrameworkVersion>
<AndroidJavaRuntimeApiLevel Condition="'$(AndroidJavaRuntimeApiLevel)' == ''">26</AndroidJavaRuntimeApiLevel>
<!-- The min API level supported by Microsoft.Android.Sdk, should refactor/remove when this value is the same as $(AndroidFirstApiLevel) -->
<!-- The min API level supported by Microsoft.Android.Sdk -->
<AndroidMinimumDotNetApiLevel Condition="'$(AndroidMinimumDotNetApiLevel)' == ''">21</AndroidMinimumDotNetApiLevel>
<!-- *Latest* *stable* API level binding that we support; used when building src/Xamarin.Android.Build.Tasks -->
<AndroidLatestStableApiLevel Condition="'$(AndroidLatestStableApiLevel)' == ''">33</AndroidLatestStableApiLevel>
Expand Down
4 changes: 3 additions & 1 deletion Documentation/guides/messages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ or 'Help->Report a Problem' in Visual Studio for Mac.
+ [XA1032](xa1032.md):Failed to resolve '{0}' from '{1}'. Please check your `AndroidHttpClientHandlerType` setting.
+ [XA1033](xa1033.md): Could not resolve '{0}'. Please check your `AndroidHttpClientHandlerType` setting.
+ [XA1035](xa1035.md): The 'BundleAssemblies' property is deprecated and it has no effect on the application build. Equivalent functionality is implemented by the 'AndroidUseAssemblyStore' and 'AndroidEnableAssemblyCompression' properties.
+ [XA1036](xa1036.md): Microsoft.Android 34.0.0 does not support a deployment target of '19' (the minimum is '21'). Please increase the $(SupportedOSPlatformVersion) property value in your project file.
+ [XA1037](xa1037.md): AndroidManifest.xml //uses-sdk/@android:minSdkVersion '29' does not match the $(SupportedOSPlatformVersion) value '21' in the project file (if there is no $(SupportedOSPlatformVersion) value in the project file, then a default value has been assumed). Either change the value in the AndroidManifest.xml to match the $(SupportedOSPlatformVersion) value, or remove the value in the AndroidManifest.xml (and add a $(SupportedOSPlatformVersion) value to the project file if it doesn't already exist).

## XA2xxx: Linker

Expand Down Expand Up @@ -163,7 +165,7 @@ or 'Help->Report a Problem' in Visual Studio for Mac.
+ XA4213: The type '{type}' must provide a public default constructor
+ [XA4214](xa4214.md): The managed type \`Library1.Class1\` exists in multiple assemblies: Library1, Library2. Please refactor the managed type names in these assemblies so that they are not identical.
+ [XA4215](xa4215.md): The Java type \`com.contoso.library1.Class1\` is generated by more than one managed type. Please change the \[Register\] attribute so that the same Java type is not emitted.
+ [XA4216](xa4216.md): AndroidManifest.xml //uses-sdk/@android:minSdkVersion '{min_sdk?.Value}' is less than API-{XABuildConfig.NDKMinimumApiAvailable}, this configuration is not supported.
+ [XA4216](xa4216.md): AndroidManifest.xml //uses-sdk/@android:minSdkVersion '{min_sdk?.Value}' is less than API-{AndroidMinimumSupportedApiLevel}, this configuration is not supported.
+ XA4217: Cannot override Kotlin-generated method '{method}' because it is not a valid Java method name. This method can only be overridden from Kotlin.
+ [XA4218](xa4218.md): Unable to find //manifest/application/uses-library at path: {path}
+ XA4219: Cannot find binding generator for language {language} or {defaultLanguage}.
Expand Down
18 changes: 18 additions & 0 deletions Documentation/guides/messages/xa1036.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: Xamarin.Android error XA1036
description: XA1036 error code
ms.date: 5/17/2023
---
# Xamarin.Android error XA1036

## Example messages

```
error XA1036: Microsoft.Android 32.0.46 does not support a deployment target of '19' (the minimum is '21'). Please increase the $(SupportedOSPlatformVersion) property value in your project file.
```

## Solution

Edit your csproj directly and increase the 'SupportedOSPlatformVersion'
property value to something greater than the minimum version that the
Microsoft.Android.Sdk that you are using supports.
26 changes: 26 additions & 0 deletions Documentation/guides/messages/xa1037.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: Xamarin.Android error XA1037
description: XA1037 error code
ms.date: 5/17/2023
---
# Xamarin.Android error XA1037

## Example messages

```
error XA1037: AndroidManifest.xml //uses-sdk/@android:minSdkVersion '29' does not match the $(SupportedOSPlatformVersion) value '21' in the project file (if there is no $(SupportedOSPlatformVersion) value in the project file, then a default value has been assumed). Either change the value in the AndroidManifest.xml to match the $(SupportedOSPlatformVersion) value, or remove the value in the AndroidManifest.xml (and add a $(SupportedOSPlatformVersion) value to the project file if it doesn't already exist).
pjcollins marked this conversation as resolved.
Show resolved Hide resolved
```

## Issue

This error indicates that you have a mismatch between the minSdkVersion
element set in your AndroidManifest.xml and the $(SupportedOSPlatformVersion)
property value in your project file.


## Solution

Raise the value of `//uses-sdk/@android:minSdkVersion` in *AndroidManifest.xml*
to match the $(SupportedOSPlatformVersion), or remove `//uses-sdk/@android:minSdkVersion`
from *AndroidManifest.xml* and set $(SupportedOSPlatformVersion) in your project
file if it is unset.
15 changes: 0 additions & 15 deletions build-tools/scripts/NDKVersionInfo.targets

This file was deleted.

2 changes: 1 addition & 1 deletion build-tools/scripts/XABuildConfig.cs.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Xamarin.Android.Tools
public const string XamarinAndroidVersion = "@XAMARIN_ANDROID_VERSION@";
public const string XamarinAndroidCommitHash = "@XAMARIN_ANDROID_COMMIT_HASH@";
public const string XamarinAndroidBranch = "@XAMARIN_ANDROID_BRANCH@";
public const int NDKMinimumApiAvailable = @NDK_MINIMUM_API_AVAILABLE@;
public const int AndroidMinimumDotNetApiLevel = @ANDROID_DEFAULT_MINIMUM_DOTNET_API_LEVEL@;
public const int AndroidLatestStableApiLevel = @ANDROID_LATEST_STABLE_API_LEVEL@;
public const int AndroidLatestUnstableApiLevel = @ANDROID_LATEST_UNSTABLE_API_LEVEL@;
public const int AndroidDefaultTargetDotnetApiLevel = @ANDROID_DEFAULT_TARGET_DOTNET_API_LEVEL@;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ static class KnownProperties
public const string AndroidCmakeUrlPrefix = "AndroidCmakeUrlPrefix";
public const string AndroidCmakeVersion = "AndroidCmakeVersion";
public const string AndroidCmakeVersionPath = "AndroidCmakeVersionPath";
public const string AndroidMinimumDotNetApiLevel = "AndroidMinimumDotNetApiLevel";
public const string AndroidDefaultTargetDotnetApiLevel = "AndroidDefaultTargetDotnetApiLevel";
public const string AndroidLatestStableApiLevel = "AndroidLatestStableApiLevel";
public const string AndroidLatestUnstableApiLevel = "AndroidLatestUnstableApiLevel";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Xamarin.Android.Prepare
properties.Add (KnownProperties.AndroidCmakeUrlPrefix, StripQuotes ("@AndroidCmakeUrlPrefix@"));
properties.Add (KnownProperties.AndroidCmakeVersion, StripQuotes ("@AndroidCmakeVersion@"));
properties.Add (KnownProperties.AndroidCmakeVersionPath, StripQuotes (@"@AndroidCmakeVersionPath@"));
properties.Add (KnownProperties.AndroidMinimumDotNetApiLevel, StripQuotes ("@AndroidMinimumDotNetApiLevel@"));
properties.Add (KnownProperties.AndroidDefaultTargetDotnetApiLevel, StripQuotes ("@AndroidDefaultTargetDotnetApiLevel@"));
properties.Add (KnownProperties.AndroidLatestStableApiLevel, StripQuotes ("@AndroidLatestStableApiLevel@"));
properties.Add (KnownProperties.AndroidLatestUnstableApiLevel, StripQuotes ("@AndroidLatestUnstableApiLevel@"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ GeneratedFile Get_XABuildConfig_cs (Context context)
var replacements = new Dictionary<string, string> (StringComparer.Ordinal) {
{ "@NDK_REVISION@", context.BuildInfo.NDKRevision },
{ "@NDK_RELEASE@", BuildAndroidPlatforms.AndroidNdkVersion },
{ "@NDK_MINIMUM_API_AVAILABLE@", context.BuildInfo.NDKMinimumApiAvailable },
{ "@NDK_VERSION_MAJOR@", context.BuildInfo.NDKVersionMajor },
{ "@NDK_VERSION_MINOR@", context.BuildInfo.NDKVersionMinor },
{ "@NDK_VERSION_MICRO@", context.BuildInfo.NDKVersionMicro },
Expand All @@ -177,6 +176,7 @@ GeneratedFile Get_XABuildConfig_cs (Context context)
{ "@NDK_X86_API@", BuildAndroidPlatforms.NdkMinimumAPILegacy32.ToString ().ToString () },
{ "@NDK_X86_64_API@", BuildAndroidPlatforms.NdkMinimumAPI.ToString ().ToString () },
{ "@XA_SUPPORTED_ABIS@", context.Properties.GetRequiredValue (KnownProperties.AndroidSupportedTargetJitAbis).Replace (':', ';') },
{ "@ANDROID_DEFAULT_MINIMUM_DOTNET_API_LEVEL@", context.Properties.GetRequiredValue (KnownProperties.AndroidMinimumDotNetApiLevel) },
{ "@ANDROID_DEFAULT_TARGET_DOTNET_API_LEVEL@", context.Properties.GetRequiredValue (KnownProperties.AndroidDefaultTargetDotnetApiLevel) },
{ "@ANDROID_LATEST_STABLE_API_LEVEL@", context.Properties.GetRequiredValue (KnownProperties.AndroidLatestStableApiLevel) },
{ "@ANDROID_LATEST_UNSTABLE_API_LEVEL@", context.Properties.GetRequiredValue (KnownProperties.AndroidLatestUnstableApiLevel) },
Expand Down
1 change: 1 addition & 0 deletions build-tools/xaprepare/xaprepare/xaprepare.targets
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<Replacement Include="@AndroidCmakeUrlPrefix@=$(AndroidCmakeUrlPrefix)" />
<Replacement Include="@AndroidCmakeVersion@=$(AndroidCmakeVersion)" />
<Replacement Include="@AndroidCmakeVersionPath@=$(AndroidCmakeVersionPath)" />
<Replacement Include="@AndroidMinimumDotNetApiLevel@=$(AndroidMinimumDotNetApiLevel)" />
<Replacement Include="@AndroidDefaultTargetDotnetApiLevel@=$(AndroidDefaultTargetDotnetApiLevel)" />
<Replacement Include="@AndroidLatestStableApiLevel@=$(AndroidLatestStableApiLevel)" />
<Replacement Include="@AndroidLatestUnstableApiLevel@=$(AndroidLatestUnstableApiLevel)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
-->
<_GetChildProjectCopyToPublishDirectoryItems>false</_GetChildProjectCopyToPublishDirectoryItems>
<UseMonoRuntime Condition=" '$(UseMonoRuntime)' == '' ">true</UseMonoRuntime>

<!-- Use $(AndroidMinimumSupportedApiLevel) for $(SupportedOSPlatformVersion) if unset -->
<SupportedOSPlatformVersion Condition=" '$(SupportedOSPlatformVersion)' == '' ">$(AndroidMinimumSupportedApiLevel)</SupportedOSPlatformVersion>
jonpryor marked this conversation as resolved.
Show resolved Hide resolved
<!-- $(SupportedOSPlatformVersion) must be '21.0', but we should support integer values like '21' -->
<SupportedOSPlatformVersion Condition=" '$(SupportedOSPlatformVersion)' != '' and !$(SupportedOSPlatformVersion.Contains('.')) ">$(SupportedOSPlatformVersion).0</SupportedOSPlatformVersion>

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,19 @@ Please change the value to an assembly-qualifed type name which inherits from '{
<data name="XA1035" xml:space="preserve">
<value>The 'BundleAssemblies' property is deprecated and it has no effect on the application build. Equivalent functionality is implemented by the 'AndroidUseAssemblyStore' and 'AndroidEnableAssemblyCompression' properties.</value>
<comment>The following are literal names and should not be translated: 'BundleAssemblies', 'AndroidUseAssemblyStore', 'AndroidEnableAssemblyCompression'</comment>
</data>
<data name="XA1036" xml:space="preserve">
<value>Microsoft.Android {0} does not support a deployment target of '{1}' (the minimum is '{2}'). Please increase the $(SupportedOSPlatformVersion) property value in your project file.</value>
<comment>The following are literal names and should not be translated: Microsoft.Android, $(SupportedOSPlatformVersion)
{0} - The version of the SDK
{1} - The value of the property
{2} - The minimum SupportedOSPlatformVersion supported by this version of the SDK</comment>
</data>
<data name="XA1037" xml:space="preserve">
<value>AndroidManifest.xml //uses-sdk/@android:minSdkVersion '{0}' does not match the $(SupportedOSPlatformVersion) value '{1}' in the project file (if there is no $(SupportedOSPlatformVersion) value in the project file, then a default value has been assumed). Either change the value in the AndroidManifest.xml to match the $(SupportedOSPlatformVersion) value, or remove the value in the AndroidManifest.xml (and add a $(SupportedOSPlatformVersion) value to the project file if it doesn't already exist).</value>
pjcollins marked this conversation as resolved.
Show resolved Hide resolved
<comment>The following are literal names and should not be translated: AndroidManifest.xml, //uses-sdk/@android:minSdkVersion, $(SupportedOSPlatformVersion)
{0} - The minimum SDK version number
{1} - The SupportedOSPlatformVersion property value</comment>
</data>
<data name="XA2000" xml:space="preserve">
<value>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 Xamarin.Android once .NET 6 is released.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,10 @@ void Run (DirectoryAssemblyResolver res, bool useMarshalMethods)
Log.LogCodedError ("XA4215", Properties.Resources.XA4215_Details, kvp.Key, typeName);
}

// NOTE: $(SupportedOSPlatformVersion) will potentially be 21.0
// NOTE: $(SupportedOSPlatformVersion) will potentially be 21.0, but could also be an int
if (SupportedOSPlatformVersion.IndexOf (".", StringComparison.OrdinalIgnoreCase) == -1) {
pjcollins marked this conversation as resolved.
Show resolved Hide resolved
SupportedOSPlatformVersion += ".0";
}
string minSdkVersion = null;
if (Version.TryParse (SupportedOSPlatformVersion, out var version)) {
minSdkVersion = version.Major.ToString ();
Expand Down
39 changes: 29 additions & 10 deletions src/Xamarin.Android.Build.Tasks/Tasks/GetJavaPlatformJar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class GetJavaPlatformJar : AndroidTask

public bool DesignTimeBuild { get; set; }

public string SupportedOSPlatformVersion { get; set; }

[Output]
public string JavaPlatformJarPath { get; set; }

Expand Down Expand Up @@ -58,22 +60,39 @@ public override bool RunTask ()
platform = target_sdk.Value;

var min_sdk = uses_sdk.Attribute (androidNs + "minSdkVersion");
if (min_sdk != null && (!int.TryParse (min_sdk.Value, out int minSdkVersion) || minSdkVersion < XABuildConfig.NDKMinimumApiAvailable)) {
Log.LogWarningForXmlNode (
code: "XA4216",
file: AndroidManifest,
node: min_sdk,
message: Properties.Resources.XA4216_MinSdkVersion,
messageArgs: new object [] { min_sdk?.Value, XABuildConfig.NDKMinimumApiAvailable }
);
if (min_sdk != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside: the amount of indentation going on here has my crying out for a helper method 3 levels of indentation ago…

// NOTE: $(SupportedOSPlatformVersion) will potentially be a version, but could also be an int
if (SupportedOSPlatformVersion.IndexOf (".", StringComparison.OrdinalIgnoreCase) == -1) {
pjcollins marked this conversation as resolved.
Show resolved Hide resolved
SupportedOSPlatformVersion += ".0";
}
int supportedOsPlatformVersionAsInt = 0;
if (Version.TryParse (SupportedOSPlatformVersion, out var version)) {
supportedOsPlatformVersionAsInt = version.Major;
}

var failedToParseMinSdk = !int.TryParse (min_sdk.Value, out int minSdkVersion);

if (failedToParseMinSdk || minSdkVersion != supportedOsPlatformVersionAsInt) {
Log.LogCodedError ("XA1037", Properties.Resources.XA1037, min_sdk?.Value, SupportedOSPlatformVersion);
jonpryor marked this conversation as resolved.
Show resolved Hide resolved
}

if (failedToParseMinSdk || minSdkVersion < XABuildConfig.AndroidMinimumDotNetApiLevel) {
Log.LogWarningForXmlNode (
code: "XA4216",
file: AndroidManifest,
node: min_sdk,
message: Properties.Resources.XA4216_MinSdkVersion,
messageArgs: new object [] { min_sdk?.Value, XABuildConfig.AndroidMinimumDotNetApiLevel }
);
}
}
if (target_sdk != null && (!int.TryParse (target_sdk.Value, out int targetSdkVersion) || targetSdkVersion < XABuildConfig.NDKMinimumApiAvailable)) {
if (target_sdk != null && (!int.TryParse (target_sdk.Value, out int targetSdkVersion) || targetSdkVersion < XABuildConfig.AndroidMinimumDotNetApiLevel)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar/different to https://github.com/xamarin/xamarin-android/pull/8026/files#r1199283399 , if targetSdkVersion is < $(AndroidMinimumDotNetApiLevel), should this be an error?

This doesn't feel reasonable, and I wonder what Java would do with:

<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="19" />

Maybe this should remain a warning? This just feels bananas to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe both XA4216 warnings should be repurposed and converted to errors?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that Android studio does not produce a warning or error when targetSdkVersion < minSdkVersion, so we can probably leave the targetSdkVersion condition untouched.

I'll try to merge XA1036 into XA4216_MinSdkVersion however, as keeping that warning is redundant.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've repurposed XA4216, it will continue to warn in the case of targetSdkVersion, but will now produce errors for:

  • SupportedOSPlatformVersion < minimum version supported
  • minSdkVersion < minimum version supported

Log.LogWarningForXmlNode (
code: "XA4216",
file: AndroidManifest,
node: target_sdk,
message: Properties.Resources.XA4216_TargetSdkVersion,
messageArgs: new object [] { target_sdk?.Value, XABuildConfig.NDKMinimumApiAvailable }
messageArgs: new object [] { target_sdk?.Value, XABuildConfig.AndroidMinimumDotNetApiLevel }
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public void BuildAotApplicationWithNdkAndBundleAndÜmläüts (string supportedAb
// Set //uses-sdk/@android:minSdkVersion so that LLVM uses the right libc.so
proj.AndroidManifest = $@"<?xml version=""1.0"" encoding=""utf-8""?>
<manifest xmlns:android=""http://schemas.android.com/apk/res/android"" android:versionCode=""1"" android:versionName=""1.0"" package=""{proj.PackageName}"">
<uses-sdk android:minSdkVersion=""{Xamarin.Android.Tools.XABuildConfig.NDKMinimumApiAvailable}"" />
<uses-sdk android:minSdkVersion=""{Xamarin.Android.Tools.XABuildConfig.AndroidMinimumDotNetApiLevel}"" />
<application android:label=""{proj.ProjectName}"">
</application>
</manifest>";
Expand Down
Loading