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

Conversation

pjcollins
Copy link
Member

@pjcollins pjcollins commented May 9, 2023

Context: #8040

I noticed that we were not writing a valid minSdkVersion value to the
generated AndroidManifest.xml file for projects which included a
manifest that declared a targetSdkVersion. In these cases, we would
always write a minSdkVersion of 19 to the manifest file, as this is
the min version that our NDK supports.

Fix this to always use the value of $(SupportedOSPlatformVersion) as
the minSdkVersion element in the AndroidManifest.xml file. If this
value is not explicitly set in the project file, it will now default to
$(AndroidMinimumSupportedApiLevel) instead of
$(TargetPlatformVersion).

The XA4216 error/warning code has been repurposed.

The warning that would display when the minSdkVersion element in
AndroidManifest.xml was less than our minimum supported API level has
been converted into an error:

error XA4216: The deployment target '19.0' is not supported (the minimum is '21'). Please increase (or remove) the //uses-sdk/@android:minSdkVersion value in your AndroidManifest.xml.

An error condition has been added for when the
$(SupportedOSPlatformVersion) property value is less than
$(AndroidMinimumSupportedApiLevel):

error XA4216: The deployment target '19.0' is not supported (the minimum is '21'). Please increase the $(SupportedOSPlatformVersion) property value in your project file.

An error condition has been added for when the minSdkVersion element
in AndroidManifest.xml does not match $(SupportedOSPlatformVersion):

error XA1036: AndroidManifest.xml //uses-sdk/@android:minSdkVersion '19' does not match the $(SupportedOSPlatformVersion) value '21.0' 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).

A few multidex related tests have been updated/removed as they are only
valid with a minSdkVersion of 20 or lower.

I noticed that we were not writing a valid `minSdkVersion` value to the
generated `AndroidManifest.xml` file for projects which included a
manifest that declared a `targetSdkVersion`.  In these cases, we would
always write a `minSdkVersion` of `19` to the manifest file, as this is
the min version that our NDK supports.

Fix this to always write the value of `$(SupportedOSPlatformVersion)`.
If this value is not explicitly set in the project file, it will default
to `$(TargetPlatformVersion)`.

A couple of multidex related tests have been added to the ignore list as
they are only valid with a minSdkVersion of 20 or lower.
@pjcollins pjcollins changed the title [XABT] Do not prefer NDKMinimumApiAvailable [XABT] Update NDKMinimumApiAvailable to 21 May 15, 2023
@pjcollins pjcollins marked this pull request as draft May 15, 2023 15:44
@pjcollins pjcollins changed the title [XABT] Update NDKMinimumApiAvailable to 21 [XABT] Do not prefer NDKMinimumApiAvailable over SupportedOSPlatformVersion May 15, 2023
@pjcollins pjcollins changed the title [XABT] Do not prefer NDKMinimumApiAvailable over SupportedOSPlatformVersion [XABT] Prefer SupportedOSPlatformVersion over minSdkVersion May 17, 2023
@pjcollins pjcollins marked this pull request as ready for review May 18, 2023 15:04
@pjcollins pjcollins requested a review from grendello as a code owner May 18, 2023 15:04
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…

}
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

@jonpryor
Copy link
Member

"Meta" thought: please rebase atop 04e4bb1 or later, as 04e4bb1 enables a whole slew of CA1305 string-related errors which may (will?) be implicated with all the int.TryParse() calls here.

@pjcollins
Copy link
Member Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@pjcollins
Copy link
Member Author

Looks like the only test failures are InstallAndroidDependenciesTest which are happening elsewhere, I think this should be good to go.

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

if (failedToParseMinSdk || minSdkVersion < XABuildConfig.AndroidMinimumDotNetApiLevel) {
Log.LogCodedError ("XA4216", Properties.Resources.XA4216_MinSdkVersion, min_sdk?.Value, 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.

Is it possible to provide a filename for the error message?

Copy link
Member Author

Choose a reason for hiding this comment

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

We could extend https://github.com/xamarin/xamarin-android-tools/blob/main/src/Microsoft.Android.Build.BaseTasks/MSBuildExtensions.cs#L142 for the AndroidManifest case, but I don't know of an easy way to report what file is setting SupportedOSPlatformVersion (it will likely be set in the project file, but could be in a directory.build.props or any other import).

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 added dotnet/android-tools#209 so that we can better link to the manifest file in this error.

}

if (failedToParseMinSdk || minSdkVersion != supportedOsPlatformVersionAsInt) {
Log.LogCodedError ("XA1036", Properties.Resources.XA1036, min_sdk?.Value, SupportedOSPlatformVersion);
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to provide a filename for the error message?

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 don't know of an easy way to report what file is setting SupportedOSPlatformVersion (it will likely be set in the project file, but could be in a directory.build.props or any other import).

@jonpryor
Copy link
Member

squash-and-merge commit message:

Fixes: https://github.com/xamarin/xamarin-android/issues/8040

I noticed that we were not writing a supported (by us)
`//uses-sdk/@android:minSdkVersion` value to the generated
`AndroidManifest.xml` file for projects which included a manifest
that declared a `targetSdkVersion`.  In these cases, we would
always write a `//uses-sdk/@android:minSdkVersion` value of `21` to
`AndroidManifest.xml`, as this is the minimum version that MonoVM
supports.

Fix this to always use the value of `$(SupportedOSPlatformVersion)`
as the `minSdkVersion` attribute in the `AndroidManifest.xml` file.
If this value is not explicitly set in the project file, it will now
default to `$(AndroidMinimumSupportedApiLevel)` instead of
`$(TargetPlatformVersion)`.

The `XA4216` error/warning code has been expanded+repurposed.

The warning that would display when the `minSdkVersion` attribute in
`AndroidManifest.xml` was less than our minimum supported API level
has been converted into an error:

	error XA4216: The deployment target '19' is not supported (the minimum is '21').
	Please increase (or remove) the //uses-sdk/@android:minSdkVersion value in your project file.


A similar error is now reported when `$(SupportedOSPlatformVersion)`
is less than `$(AndroidMinimumSupportedApiLevel)`

	error XA4216: The deployment target '19' is not supported (the minimum is '21').
	Please increase the $(SupportedOSPlatformVersion) property value in your project file.

An error condition has been added for when
`//uses-sdk/@android:minSdkVersion` in `AndroidManifest.xml` does not
match `$(SupportedOSPlatformVersion)`:

	error XA1036: AndroidManifest.xml //uses-sdk/@android:minSdkVersion '19' does not match the $(SupportedOSPlatformVersion) value '21.0' 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).

A few multidex related tests have been updated/removed as they are
only valid with a minSdkVersion of 20 or lower.

@pjcollins
Copy link
Member Author

One comment for the message:

 In these cases, we would
always write a `//uses-sdk/@android:minSdkVersion` value of `21` to
`AndroidManifest.xml`, as this is the minimum version that MonoVM
supports.

We were previously writing minSdkVersion="19" to manifests, and with these changes we will now default to 21 if a value is not provided.

@jonpryor jonpryor merged commit 941e04c into main Jun 1, 2023
@jonpryor jonpryor deleted the dev/pjc/minsdknet branch June 1, 2023 15:13
grendello added a commit to grendello/xamarin-android that referenced this pull request Jun 2, 2023
* main:
  [XABT] Prefer `SupportedOSPlatformVersion` over `minSdkVersion` (dotnet#8026)
  Bump to xamarin/Java.Interop/main@72b041a (dotnet#8089)
  Bump LibZipSharp to 3.0.0 (dotnet#8061)
  Bump to xamarin/Java.Interop/main@8c9eece (dotnet#8073)
  [profiled-aot] update AOT profile for .NET 8 Preview 5 (dotnet#8077)
  Localized file check-in by OneLocBuild Task (dotnet#8078)
  Bump to dotnet/installer@6150605bd0 8.0.100-preview.6.23276.3 (dotnet#8083)
@github-actions github-actions bot locked and limited conversation to collaborators Jan 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants