Skip to content

[release/11.0.1xx] [Build.Tasks] Remove shared Android SDK discovery state (#13145) - #13744

Open
jonathanpeppers wants to merge 2 commits into
release/11.0.1xxfrom
jonathanpeppers-backport-13145-sdk-discovery
Open

jonathanpeppers wants to merge 2 commits into
release/11.0.1xxfrom
jonathanpeppers-backport-13145-sdk-discovery

Conversation

@jonathanpeppers

@jonathanpeppers jonathanpeppers commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 5961c980a43e8be188a70ff6bde3961b7441dd05

Dimension Assessment Rationale
Change confidence 8.5/10 ↑ Outside the PublicAPI files, the cherry-picked lines are identical to #13145 (380 of 380 lines).
↑ Review fix removes the only 2 remaining MonoAndroidHelper.AndroidSdk references (dead test assignments); grep finds no others.
↑ PublicAPI conflicts only add the 3 lines that #13145 adds.
↑ Xamarin.Android.Tools.AndroidSdk-Tests pass locally (net10.0).
↓ Xamarin.Android.Build.Tasks and its tests were not built locally; CI must validate them.

Backport of #13145.

Originally stacked on #13743 (backport of #13133), which is now merged; the base is now release/11.0.1xx.

Why

MonoAndroidHelper.AndroidSdk is mutable static state that task executions in the same MSBuild process share. Concurrent ResolveSdks executions can replace the field while another execution reads it, so one project can get SDK paths from a different project. This change keeps the AndroidSdkInfo object local to each ResolveSdks execution and makes downstream lookups use the explicit resolved SDK paths. See #13145 for the full description.

Conflict resolution

git cherry-pick -x bcdef489af9daa431e5d8f41b16b218432fb907f conflicted only in:

  • src/Xamarin.Android.Tools.AndroidSdk/PublicAPI/net10.0/PublicAPI.Unshipped.txt
  • src/Xamarin.Android.Tools.AndroidSdk/PublicAPI/net11.0/PublicAPI.Unshipped.txt
  • src/Xamarin.Android.Tools.AndroidSdk/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt

The conflict context on main includes a DiscoverInstallationPaths line that does not exist on release/11.0.1xx. I kept the release lines and added only the 3 lines that #13145 adds (GetBuildToolsPaths, GetCommandLineToolsPaths, TryGetPlatformDirectoryFromApiLevel). All other files auto-merged, and their changed lines are identical to #13145.

Release-only fix

On main, #13073 (not in release) already removed two test assignments to MonoAndroidHelper.AndroidSdk. On release/11.0.1xx they still exist, so Xamarin.Android.Build.Tests failed with CS0117 after this backport. A second commit removes these dead assignments in NdkUtilTests.cs and EnvironmentHelper.cs (and the now-unused sdkDir local). No code reads the field, so test behavior does not change.

Validation

  • dotnet test tests/Xamarin.Android.Tools.AndroidSdk-Tests/Xamarin.Android.Tools.AndroidSdk-Tests.csproj -p:AndroidToolsDisableMultiTargeting=false -p:DotNetTargetFrameworkVersion=10.0: passed (429 passed, 0 failed, 19 skipped).
  • git grep -n "MonoAndroidHelper.AndroidSdk\b": only a comment in NdkTools.cs remains (also present on main).
  • Not run locally: full SDK build, Xamarin.Android.Build.Tests (ResolveSdksTaskTests), and device tests. CI runs these.

Pull Request
title and
description
should follow the
commit-messages.md workflow documentation, and in particular should include:

  • Useful description of why the change is necessary.
  • Links to issues fixed
  • Unit tests

@jonathanpeppers jonathanpeppers changed the title jonathanpeppers backport 13145 sdk discovery [release/11.0.1xx] [Build.Tasks] Remove shared Android SDK discovery state (#13145) Nov 23, 2026
Base automatically changed from backport/pr-13133-to-release/11.0.1xx to release/11.0.1xx November 24, 2026 14:21
…state (#13145)

## Why

**The primary motivation is correctness under multithreaded MSBuild execution: remove mutable static SDK discovery state.**  `MonoAndroidHelper.AndroidSdk` is a static field shared by task executions that use the same loaded task assembly in an MSBuild process.  It is not scoped to a project or task invocation.

Concurrent task executions can overwrite that field while another execution is reading it.  For example, one project's `ResolveSdks` can install SDK A in the field, another project's `ResolveSdks` can replace it with SDK B, and the first project's SDK outputs or downstream tool/platform lookups can then come from SDK B.  An explicit SDK directory on the consuming task does not prevent this when the lookup reads the static object instead.

Keep the discovery object local to each `ResolveSdks` execution and make downstream queries use their own resolved SDK paths.  This removes the shared SDK-discovery race rather than serializing task execution with a lock.  Static state is process-local; this is not a claim that separate MSBuild worker processes share the field, nor that all Android tasks become thread-safe.  `SupportedVersions` remains mutable static state and is intentionally deferred to a separate change.  No build-time speedup is claimed, and there is no linked issue.

## Scope

13 related files, based directly on main at `0c3ac2489aaff9543ee1cb419e9da5ce1fdcf9f2`:

- Remove only `MonoAndroidHelper.AndroidSdk` and `RefreshAndroidSdk`; retain `SupportedVersions`, `RefreshSupportedVersions`, all their consumers, and existing legacy API behavior.  This removes one discovery-state hazard, not all task thread-safety hazards.
- Add explicit-path `AndroidSdkInfo` overloads for build tools, command-line tools, and platform lookup.  Existing instance methods share the same implementation; update the netstandard2.0/net10.0/net11.0 public API baselines.  Preserve preferred-version ordering, stable/preview ordering, minor-version restrictions, preview fallback, and API 37 `.0` fallback.
- Construct a local `AndroidSdkInfo` in `ResolveSdks` with coded logging.  Preserve JDK validation inputs and NDK discovery fallbacks; emit absolute SDK/JDK/NDK/command-line-tools paths.
- Use explicit SDK paths in tooling, manifest optional-library lookup, platform-JAR lookup, and jar2xml.  Pass the resolved `_AndroidSdkDirectory` to the existing `GetJavaPlatformJar` input.
- Cover SDK-path independence without JDK discovery, null arguments, ordering, platform fallbacks, missing-path coded errors, absolute outputs, and NDK fallback.

No API metadata cache, static supported-version removal, nested-build forwarding/skipping, new public MSBuild properties, or performance claim is included.

Co-authored-by: Copilot App <[email protected]>

(cherry picked from commit bcdef48)
@jonathanpeppers
jonathanpeppers force-pushed the jonathanpeppers-backport-13145-sdk-discovery branch from 4fecccb to 56dc892 Compare November 24, 2026 14:21
@jonathanpeppers
jonathanpeppers marked this pull request as ready for review November 24, 2026 14:21
Copilot AI balanced review requested due to automatic review settings November 24, 2026 14:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Removing MonoAndroidHelper.AndroidSdk leaves references in two compiled test files, causing the full test suite build to fail with CS0117.

1 open issue
What changed in this PR

Backports #13145 to release/11.0.1xx, removing shared Android SDK discovery state so concurrent MSBuild task executions use their own resolved SDK paths. Shared supported-version state remains unchanged.

Changes:

  • Keep SDK discovery local to ResolveSdks and pass explicit paths to downstream consumers.
  • Add explicit-path SDK lookup APIs and update all three public API baselines.
  • Extend regression tests for SDK isolation, lookup ordering, platform fallbacks, diagnostics, and absolute paths.
File Description
tests/​Xamarin.Android.Tools.AndroidSdk-Tests/​AndroidSdkInfoTests.cs Tests explicit-path lookup isolation, ordering, and fallbacks.
src/​Xamarin.Android.Tools.AndroidSdk/​PublicAPI/​netstandard2.0/​PublicAPI.Unshipped.txt Records three new lookup overloads.
src/​Xamarin.Android.Tools.AndroidSdk/​PublicAPI/​net11.0/​PublicAPI.Unshipped.txt Records three new lookup overloads.
src/​Xamarin.Android.Tools.AndroidSdk/​PublicAPI/​net10.0/​PublicAPI.Unshipped.txt Records three new lookup overloads.
src/​Xamarin.Android.Tools.AndroidSdk/​AndroidSdkInfo.cs Adds explicit-path APIs sharing existing lookup logic.
src/​Xamarin.Android.Build.Tasks/​Xamarin.Android.Tooling.targets Passes the resolved SDK directory to platform-JAR lookup.
src/​Xamarin.Android.Build.Tasks/​Utilities/​MonoAndroidHelper.Nullable.cs Removes shared SDK initialization.
src/​Xamarin.Android.Build.Tasks/​Utilities/​MonoAndroidHelper.cs Removes the SDK field and uses explicit paths for JAR lookup.
src/​Xamarin.Android.Build.Tasks/​Tests/​Xamarin.Android.Build.Tests/​Tasks/​ResolveSdksTaskTests.cs Adds task-level path isolation and error-handling coverage.
src/​Xamarin.Android.Build.Tasks/​Tasks/​ResolveSdksTask.cs Uses local discovery, coded logging, and absolute output paths.
src/​Xamarin.Android.Build.Tasks/​Tasks/​ResolveAndroidTooling.cs Resolves tools from the supplied SDK path.
src/​Xamarin.Android.Build.Tasks/​Tasks/​ReadAndroidManifest.cs Resolves optional libraries from the supplied SDK path.
src/​Xamarin.Android.Build.Tasks/​Tasks/​JarToXml.cs Uses the supplied SDK path for platform-JAR references.

Review effort: Balanced

…ignments

#13145 removed MonoAndroidHelper.AndroidSdk. On main, #13073 had already removed these two test assignments, but release/11.0.1xx still has them, so Xamarin.Android.Build.Tests fails with CS0117. Remove the dead assignments and the unused local.

Co-authored-by: Copilot App <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants