Skip to content

[mt msbuild] Remove shared Android SDK discovery state - #13145

Merged
jonathanpeppers merged 1 commit into
mainfrom
jonathanpeppers-sdk-discovery-isolation
Oct 15, 2026
Merged

jonathanpeppers merged 1 commit into
mainfrom
jonathanpeppers-sdk-discovery-isolation

Conversation

@jonathanpeppers

@jonathanpeppers jonathanpeppers commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

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

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.

Local validation

All commands ran in this PR's own worktree with its locally built Android SDK, pinned .NET SDK 12.0.100-alpha.1.26509.101, and the following process-local environment:

$env:DOTNET_CLI_USE_MSBUILD_SERVER='0'
$env:MSBUILDDISABLENODEREUSE='1'
$env:DOTNET_CLI_TELEMETRY_OPTOUT='1'
$env:TEST_ANDROID_JDK_PATH='C:\Program Files\Android\openjdk\jdk-21.0.8'

Prepare: passed.

.\build.cmd Prepare

SDK helper tests: 21 passed, 2 platform-specific skips, 0 failed.

.\dotnet-local.cmd test tests\Xamarin.Android.Tools.AndroidSdk-Tests\Xamarin.Android.Tools.AndroidSdk-Tests.csproj -p:AndroidToolsDisableMultiTargeting=false -p:DotNetTargetFrameworkVersion=10.0 --filter FullyQualifiedName~AndroidSdkInfoTests -nr:false -v:minimal

Additional helper target-framework/API check: passed for netstandard2.0 and net11.0, 0 warnings/errors. The preceding test command also builds net10.0.

.\dotnet-local.cmd build src\Xamarin.Android.Tools.AndroidSdk\Xamarin.Android.Tools.AndroidSdk.csproj -p:AndroidToolsDisableMultiTargeting=false -p:DotNetTargetFrameworkVersion=11.0 -nr:false -v:minimal

Full local SDK BuildDotNet: passed, 0 errors (92 generated-binding/native/baseline warnings).

& $env:ComSpec /c 'call "C:\Program Files\Microsoft Visual Studio\18\Insiders\Common7\Tools\VsDevCmd.bat" >nul && .\dotnet-local.cmd build Microsoft.Android.slnx -t:BuildDotNet -nr:false -v:minimal -bl:bin\BuildDebug\sdk-discovery-build.binlog > bin\BuildDebug\sdk-discovery-build.log 2>&1'

Focused host regression run against the final rebuilt SDK: 26 passed, 0 skipped/failed. Includes all 18 ResolveSdksTaskTests, three Debug CoreCLR basic-app locale cases, app relocation, manifest modification, a binding build, and both missing-platform-JAR diagnostic cases. Real Android builds use the supported JDK 21 path above; historical task fixtures retain their existing faux JDK versions.

& $env:ComSpec /c '.\dotnet-local.cmd test bin\TestDebug\net10.0\Xamarin.Android.Build.Tests.dll --filter "FullyQualifiedName~ResolveSdksTaskTests|(FullyQualifiedName~BuildTest2.BuildBasicApplication&Name~False&Name~CoreCLR)|(FullyQualifiedName~ManifestTest.ModifyManifest&Name~False)|(FullyQualifiedName~BindingBuildTest.DotNetBuildBinding&Name~CoreCLR)|FullyQualifiedName~IfAndroidJarDoesNotExistThrowXA5207" --logger "trx;LogFileName=sdk-discovery-final-tests.trx" --results-directory bin\TestDebug\TestResults -v:minimal > bin\BuildDebug\sdk-discovery-final-tests.log 2>&1'

git diff --check and parsing the modified targets as XML passed. The build binlog was inspected with binlogtool. No device tests were run: this change only removes host-side SDK discovery state; no runtime/JNI/native behavior changes.

Keep Android SDK discovery local to ResolveSdks instead of storing a
mutable AndroidSdkInfo in MonoAndroidHelper.  Query tool and platform
paths using each consumer's resolved SDK directory, and normalize SDK,
JDK, NDK and command-line tool outputs for task output transport.

Preserve SupportedVersions and RefreshSupportedVersions, discovery
fallbacks, tool ordering and platform API fallback behavior.  Cover
explicit path isolation, missing SDK errors and absolute path outputs.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: c88abce7-9de0-4b64-be95-984d17a5b7a5
Copilot AI balanced review requested due to automatic review settings October 14, 2026 19:02

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.

🔵 Needs a closer look

Cross-task discovery changes and public API additions warrant human sign-off; reported validation was not independently verified.

0 open findings

What changed in this PR

Removes shared Android SDK discovery state so build tasks use their own resolved SDK paths, while retaining supported-version state and existing lookup behavior.

Changes:

  • Adds explicit-path SDK lookup APIs and updates public API baselines.
  • Keeps discovery local to ResolveSdks and passes resolved paths to downstream tasks.
  • Adds regression coverage for SDK isolation, ordering, fallbacks, diagnostics, and absolute paths.
File Description
tests/​Xamarin.Android.Tools.AndroidSdk-Tests/​AndroidSdkInfoTests.cs Tests explicit-path lookup and compatibility.
src/​Xamarin.Android.Tools.AndroidSdk/​PublicAPI/​netstandard2.0/​PublicAPI.Unshipped.txt Records new netstandard2.0 APIs.
src/​Xamarin.Android.Tools.AndroidSdk/​PublicAPI/​net11.0/​PublicAPI.Unshipped.txt Records new net11.0 APIs.
src/​Xamarin.Android.Tools.AndroidSdk/​PublicAPI/​net10.0/​PublicAPI.Unshipped.txt Records new net10.0 APIs.
src/​Xamarin.Android.Tools.AndroidSdk/​AndroidSdkInfo.cs Adds shared implementations for explicit-path lookups.
src/​Xamarin.Android.Build.Tasks/​Xamarin.Android.Tooling.targets Passes the resolved SDK directory.
src/​Xamarin.Android.Build.Tasks/​Utilities/​MonoAndroidHelper.Nullable.cs Removes shared SDK refresh logic.
src/​Xamarin.Android.Build.Tasks/​Utilities/​MonoAndroidHelper.cs Removes SDK state and uses explicit platform paths.
src/​Xamarin.Android.Build.Tasks/​Tests/​Xamarin.Android.Build.Tests/​Tasks/​ResolveSdksTaskTests.cs Tests task isolation, outputs, and diagnostics.
src/​Xamarin.Android.Build.Tasks/​Tasks/​ResolveSdksTask.cs Uses local discovery and absolute output paths.
src/​Xamarin.Android.Build.Tasks/​Tasks/​ResolveAndroidTooling.cs Resolves tools from the task’s SDK path.
src/​Xamarin.Android.Build.Tasks/​Tasks/​ReadAndroidManifest.cs Uses the explicit SDK for optional libraries.
src/​Xamarin.Android.Build.Tasks/​Tasks/​JarToXml.cs Uses the explicit SDK for platform JAR lookup.

🧠 Review effort: Balanced

@jonathanpeppers jonathanpeppers changed the title [Build.Tasks] Remove shared Android SDK discovery state [mt msbuild] Remove shared Android SDK discovery state Oct 14, 2026
@jonathanpeppers

Copy link
Copy Markdown
Member Author

/review

@jonathanpeppers jonathanpeppers added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Oct 15, 2026
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #13145

@github-actions github-actions Bot 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.

✅ LGTM — 0 errors, 0 warnings, 1 suggestion.

The explicit SDK path now flows consistently through SDK discovery, tooling resolution, platform-JAR lookup, manifest optional-library lookup, and jar2xml. The static helper implementations preserve ordering and platform fallback behavior, the API baselines are updated across all target frameworks, and the regression coverage exercises both SDK-root isolation and missing-path diagnostics. All 42 reported checks are green.

Generated by Android PR Reviewer for #13145 · copilot · gpt56 · 151.9 AIC · ⌖ 11.3 AIC · ⊞ 26.4K
Comment /review to run again

@jonathanpeppers
jonathanpeppers merged commit bcdef48 into main Oct 15, 2026
42 checks passed
@jonathanpeppers
jonathanpeppers deleted the jonathanpeppers-sdk-discovery-isolation branch October 15, 2026 17:04
simonrozsival pushed a commit that referenced this pull request Oct 20, 2026
…te (#13201)

`Xamarin.Android.Tasks.MonoAndroidHelper.SupportedVersions` is a mutable process-wide value. One project can replace the API data that another task uses. Remove that value and `RefreshSupportedVersions`, and pass `ReferenceAssemblyPaths` to each consumer.

SDK discovery, Java validation, and legacy-tool cleanup are already merged in #13145, #13133, and #13161. This PR does not repeat those changes. It is the lower prerequisite for a separate nested-build optimization. **SDK and Java resolution call counts are unchanged**, including inner builds.

Cache read-only `AndroidVersions` data through `IBuildEngine4` with `RegisteredTaskObjectLifetime.Build`. The key includes the task assembly identity and the absolute, original input directory list. The cache holds no task logger or mutable current-context pointer. Concurrent creation for an equal key is harmless; correctness does not depend on a cache shared across worker processes or assembly load contexts.

All API-data consumers use explicit inputs: platform JAR and tooling resolution, manifests, aapt2, D8/R8, signing, jar2xml, launch activity/instrumentation, library resources, and dependency calculation. `ManifestDocument` keeps its own version resolver, with a known-version default for standalone use. Aapt2Link and library-resource generation resolve API metadata on the calling thread before yielding.

The concurrency test runs 64 tasks against two different SDK/API datasets that map the same preview ID `Z` to API levels 127 and 128. It checks both JAR paths and API outputs. The aapt2 tests link two ABIs and require exactly one API lookup before `Yield`, with relative reference paths resolved from the task's captured working directory. Tests also check cache reuse, separate build engines, manifest resolver isolation, and library-resource metadata access before `Yield`.
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants