Repository navigation
[Xamarin.Android.Build.Tasks] Replace ValidateJavaVersion with MSBuild targets - #13472
Conversation
There was a problem hiding this comment.
🔵 Needs a closer look
Validation covers only Core MSBuild, leaving the new Full MSBuild out-of-process TaskHostFactory path on Windows/Visual Studio unverified, including release-file fallback and task-object caching.
No open issues 🎉
What changed in this PR
Moves ValidateJavaVersion into the .NET 12 task assembly so Java version parsing can use source-generated regexes, following the BuildAppBundle migration in #13460.
Changes:
- Migrates the task to
Microsoft.Android.Tasksand updates Full/Core MSBuild registration. - Extracts the shared process helper for compilation into both task assemblies.
- Moves unit tests, adds short and qualified task-name resolution coverage, and retains the tooling integration test.
| File | Description |
|---|---|
src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.RunProcess.cs |
Houses the two shared process-launch overloads. |
src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs |
Removes the extracted overloads. |
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/ValidateJavaVersionTests.cs |
Retains end-to-end tooling validation while moving unit tests. |
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/ResolveSdksTaskTests.cs |
Removes redundant Java validation setup and assertions. |
src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Tooling.targets |
Registers the migrated task with Full/Core hosting conditions. |
src/Microsoft.Android.Build.Tasks/Tests/Microsoft.Android.Build.Tasks.Tests/Tasks/ValidateJavaVersionTests.cs |
Adds migrated tests, local faux-JDK helpers, and task-name resolution coverage. |
src/Microsoft.Android.Build.Tasks/Tasks/ValidateJavaVersion.cs |
Moves the task, adopts generated regexes, and adjusts nullable handling. |
src/Microsoft.Android.Build.Tasks/Microsoft.Android.Build.Tasks.csproj |
Compile-links the shared process helper. |
Review effort: Balanced
|
/review |
|
✅ Android PR Reviewer completed successfully!
|
There was a problem hiding this comment.
⚠️ Needs Changes
Findings: 1 ❌ error, 0
The target split preserves the design-time and nested-build reuse gates, keeps diagnostics localized through the existing tasks, and retains substantial range/parsing coverage. However, the release-file fast path no longer follows an explicit JavaToolExe override, so it can accept an unsupported JDK while reporting the version of a different installation. The inline finding includes the reproduction and requested regression coverage.
Validation: Reviewed all six changed files, the removed task, and relevant callers/project configuration. An isolated MSBuild invocation importing the new targets reproduced successful validation reporting JDK 21.0.8 despite the selected executable's adjacent release declaring 17.0.1. No repository files were changed.
CI: All 40 returned GitHub checks report success, including the aggregate dotnet-android check for Azure build 1674905. Direct Azure build/timeline verification was blocked because az devops invoke requires authentication in this environment; I could not independently confirm the Azure result. The full SDK/test suites were not run locally.
Generated by Android PR Reviewer for #13472 · copilot · gpt61 · 38.1 AIC · ⌖ 7.96 AIC · ⊞ 19.5K
Comment /review to run again
|
/review |
|
✅ Android PR Reviewer completed successfully!
|
There was a problem hiding this comment.
The selected-executable release-file lookup now preserves the previous override behavior, and the added absolute/relative override cases provide useful regression coverage. One remaining path-handling regression is flagged inline: the process fallback now interprets the executable path through a shell rather than invoking it literally.
CI: All 40 reported GitHub checks succeeded, including the aggregate dotnet-android check for Azure build 1677635. Independent Azure build/timeline verification was blocked because the Azure CLI required authentication. Local dotnet msbuild smoke checks were blocked before target execution by a NuGet-Migrations mutex initialization failure; the finding is based on source inspection, not a successful local reproduction.
Generated by Android PR Reviewer for #13472 · copilot · gpt61 · 56.1 AIC · ⌖ 4.75 AIC · ⊞ 20.2K
Comment /review to run again
…ndroid.Build.Tasks Context: #13460 `<ValidateJavaVersion/>` parses `java -version` and `javac -version` output with two fixed regular expressions, which are constructed and interpreted at runtime because Xamarin.Android.Build.Tasks targets netstandard2.0. Microsoft.Android.Build.Tasks targets net12.0, where `[GeneratedRegex]` is available. Move the task into Microsoft.Android.Build.Tasks, following the BuildAppBundle move in #13460: * The task now lives in the `Microsoft.Android.Tasks` namespace and keeps its `MultithreadedAndroidTask` base. `Microsoft.Android.Sdk.Tooling.targets` registers it with a single `<UsingTask/>` against `$(_MicrosoftAndroidBuildTasksAssembly)`; the unqualified `<ValidateJavaVersion/>` invocation is unchanged. * `JavaVersionRegex` and `JavacVersionRegex` become `[GeneratedRegex]` partial properties with the same patterns and default options. Neither uses `RegexOptions.IgnoreCase`, so there is no culture-sensitivity change. * `MonoAndroidHelper.RunProcess(TaskEnvironment, …)` and its `TaskEnvironment.Fallback` overload move verbatim from `MonoAndroidHelper.cs` into `MonoAndroidHelper.RunProcess.cs`, which is compile-linked into Microsoft.Android.Build.Tasks. `ResolveAndroidTooling`, `Lint`, and `MonoAndroidHelper.GetOSBinDirName()` still use it. * Two nullable warnings surfaced by net12.0 annotations are fixed without changing behavior for real JDK paths. The unit tests move to Microsoft.Android.Build.Tasks.Tests with a private copy of the faux-JDK helpers, plus a new test that loads the shipped `<UsingTask/>` and runs both `<ValidateJavaVersion/>` and `<Microsoft.Android.Tasks.ValidateJavaVersion/>` through `dotnet msbuild`. `ToolingTargetRejectsOldJavaForModernAndroidApi` stays in Xamarin.Android.Build.Tests to cover the task through `_ResolveAndroidTooling`. `ResolveSdksTaskTests` no longer instantiates `ValidateJavaVersion`; its JDK assertions were already covered by `ValidateJavaVersionTests`. The task now requires .NET 12 MSBuild, like the other Microsoft.Android.Build.Tasks tasks. Co-authored-by: Copilot App <[email protected]>
Visual Studio (Full MSBuild) must load the net12.0 task through TaskHostFactory, matching BuildAppBundle, GetAndroidPackageName, and BuildArchive. Co-authored-by: Copilot App <[email protected]>
GoogleSdkRequirementsTests imports Microsoft.Android.Sdk.Tooling.targets directly. Now that ValidateJavaVersion is registered from Microsoft.Android.Build.Tasks, evaluation fails with MSB4022 unless the assembly property is set, as AndroidComputeResPathsTests already does. Co-authored-by: Copilot App <[email protected]>
Instead of moving `<ValidateJavaVersion/>` into Microsoft.Android.Build.Tasks, express it directly in `Microsoft.Android.Sdk.Tooling.targets`. This reverts the move, including the `MonoAndroidHelper.RunProcess.cs` split, and deletes the C# task. `_ResolveAndroidTooling` now depends on `_ComputeAndroidToolingInputs;_ValidateJavaVersion`, so validation still runs before `<ResolveAndroidTooling/>`, with the same `_AndroidUseResolvedTooling`/`DesignTimeBuild` condition, and still sets `$(_JdkVersion)` and `$(_DefaultJdkVersion)`. The new target reads `JAVA_VERSION` from the JDK's `release` file and falls back to `<Exec/>`ing `java -version` (and, with `$(_AndroidUseJavaExeVersion)`, `javac -version`). Errors use the existing `<AndroidError/>`/`<AndroidWarning/>` tasks, so XA0030, XA0031, XA0033, and XA5300 stay localized. Co-authored-by: Copilot App <[email protected]>
Read $(_JavaSdkDirectory)release and build the java/javac paths inline instead of mirroring the C# task's intermediate properties. Check the java and javac versions with one batched block, and clear the temporary item afterwards. $(_AndroidAllowMissingSdkTooling) is already defaulted by _ResolveSdks, so the target no longer sets it. Co-authored-by: Copilot App <[email protected]>
_ValidateJavaVersion now reads $(_JavaSdkDirectory)release and only falls back to `java -version` when that file is missing or invalid. The private $(_AndroidUseJavaExeVersion) switch (added in #8663 as an undocumented way back to the old behavior) and the `javac -version` check it enabled are removed, along with the JavacToolExe/UseJavaExeVersion tooling inputs. Co-authored-by: Copilot App <[email protected]>
Resolve the release file relative to the selected Java executable before falling back to the SDK release file. Reuse that executable for the process fallback so both paths validate the same JDK. Add standalone production-target tests covering absolute and relative overrides, supported and unsupported versions, missing adjacent releases, and default tool selection. SDK-only lookup mutation fails six cases; the restored fix passes all ten plus sixteen design-time cases. Co-authored-by: Copilot App <[email protected]>
Pass the executable through the environment when invoking java -version so shell metacharacters remain literal. Cover paths containing dollar signs and other shell metacharacters. Co-authored-by: Copilot App <[email protected]>
5f8593c to
1d457af
Compare
|
/review |
|
✅ Android PR Reviewer completed successfully!
|
There was a problem hiding this comment.
⚠️ Needs Changes
Findings: 0 errors, 1 warning, 0 suggestions. One inline cleanup remains: the deleted task was the last producer of XA0034, but its English resource and generated accessor remain. No additional confirmed correctness regressions were found in the eight changed files and their relevant callers.
The selected-executable release-file lookup and literal-path process invocation address the earlier review findings. The updated tests cover executable overrides, release-file fallback, version ranges, special-character paths, design-time skipping, and nested-build reuse.
CI: All 40 GitHub checks report success, including the aggregate dotnet-android check for Azure build 1679335. Direct Azure build/timeline verification was blocked by CLI/authentication errors, so that independent verification remains incomplete. Local MSBuild execution was blocked during .NET first-run initialization; no local test results are claimed.
Generated by Android PR Reviewer for #13472 · copilot · gpt61 · 53.4 AIC · ⌖ 4.19 AIC · ⊞ 20.2K
Comment /review to run again
Remove the orphaned English resource and accessor, and retain the diagnostic documentation for earlier SDK versions. Co-authored-by: Copilot App <[email protected]>
|
/review |
|
✅ Android PR Reviewer completed successfully! Warning Firewall blocked 1 domainThe following domain was blocked by the firewall during workflow execution:
To allow these domains, add them to the network:
allowed:
- defaults
- "169.254.169.254"See Network Configuration for more information.
|
There was a problem hiding this comment.
⚠️ Needs Changes
Findings: 1 ❌ error · 0
The target-based replacement preserves design-time skipping, selected-executable release-file lookup, localized diagnostics, and outer/inner tooling reuse, with useful coverage for overrides and literal paths. One output-contract issue remains: major-only Java versions pass validation but are published unchanged, which can make downstream Javac version parsing throw. The inline comment includes the reproduction and requested regression coverage.
Validation: A focused MSBuild probe importing the production targets confirmed that 21 passes validation but fails the downstream System.Version.Parse contract; the 21.0.8 control succeeds. No full Android build/test suite was run for this review.
CI: All 40 GitHub checks currently report success, including the aggregate dotnet-android check for Azure build 1679901. Independent Azure build/timeline verification could not be completed: the build query requires CLI authentication, and the timeline query failed during CLI initialization. This review therefore does not independently certify the Azure pipeline result.
Warning
Firewall blocked 1 domain
The following domain was blocked by the firewall during workflow execution:
169.254.169.254
To allow these domains, add them to the network.allowed list in your workflow frontmatter:
network:
allowed:
- defaults
- "169.254.169.254"See Network Configuration for more information.
Generated by Android PR Reviewer for #13472 · copilot · gpt61 · 79.1 AIC · ⌖ 5.31 AIC · ⊞ 21K
Comment /review to run again
Append the missing minor component after either version lookup so downstream System.Version consumers receive a parseable value. Cover release files, process fallback, range validation, and downstream parsing. Co-authored-by: Copilot App <[email protected]>
|
/review |
|
✅ Android PR Reviewer completed successfully! Warning Firewall blocked 1 domainThe following domain was blocked by the firewall during workflow execution:
To allow these domains, add them to the network:
allowed:
- defaults
- "169.254.169.254"See Network Configuration for more information.
|
There was a problem hiding this comment.
⚠️ Needs Changes
Findings: 0 errors · 1 warning · 0 suggestions. One inline finding covers an XA0033 formatting regression when the Java process produces no output.
The target dependency split preserves the design-time and nested-build guards. Selected-tool release lookup, literal-path handling, and shared major-only version normalization are covered by targeted regression cases; the earlier review findings are addressed.
CI: All 40 GitHub checks report success, including the aggregate dotnet-android check for Azure build 1681129. Direct Azure build/timeline inspection was unavailable because the CLI requires authentication.
Local verification: Imported the production targets into a scratch MSBuild project and reproduced the empty-output argument loss using the same warning logging call and resource text; a nonempty-output control formatted correctly. This was an isolated reproduction, not a run of the full SDK test suites. No repository files were changed.
Warning
Firewall blocked 1 domain
The following domain was blocked by the firewall during workflow execution:
169.254.169.254
To allow these domains, add them to the network.allowed list in your workflow frontmatter:
network:
allowed:
- defaults
- "169.254.169.254"See Network Configuration for more information.
Generated by Android PR Reviewer for #13472 · copilot · gpt61 · 78.1 AIC · ⌖ 7.96 AIC · ⊞ 20.3K
Comment /review to run again
Use a nonempty description for empty or whitespace Java version output so MSBuild preserves both XA0033 format arguments. Cover silent and whitespace-only processes and assert the rendered warning. Co-authored-by: Copilot App <[email protected]>
|
/review |
|
✅ Android PR Reviewer completed successfully!
|
There was a problem hiding this comment.
✅ LGTM on code — 0 errors, 0 warnings, 1 optional testing suggestion posted inline.
Reviewed all 12 changed files and traced the version outputs into downstream Java compilation and nested-build tooling reuse. The selected-executable release lookup, shared major-only normalization, localized diagnostics, design-time guard, and silent-output handling address the earlier findings. No additional blocking correctness issue found.
CI verification: GitHub reports all 40 checks successful, including the aggregate dotnet-android check for build 1681261. Independent Azure build/timeline confirmation was unavailable: az devops invoke required authentication, and direct access was denied. This is a code-review verdict, not independent confirmation that the Azure pipeline is green.
Generated by Android PR Reviewer for #13472 · copilot · gpt61 · 52.8 AIC · ⌖ 8.34 AIC · ⊞ 20.3K
Comment /review to run again
Preserve absolute and relative JavaToolExe overrides in process-fallback tests, remove both release files, and verify selected versions and range diagnostics. Co-authored-by: Copilot App <[email protected]>
Remove JavaVersionTargetsTests: it ran 27 \dotnet msbuild\ processes
to cover the same _ValidateJavaVersion cases as ValidateJavaVersionTests,
and needed AndroidError.cs/AndroidWarning.cs compiled into a second
test assembly.
In ValidateJavaVersionTests:
* Test the version range once, from the release file. Add a
major-only \21\ case for the \.0\ normalization.
* Add JavaToolExeSelectsJdk for the JavaToolExe override.
* Use one directory name for the special-character path test.
* Build the full-project tooling test once.
* Remove ConfiguredMinimumJavaVersionIsRequired, which the range
test already covers.
In XASdkTests, count the _ValidateJavaVersion target under its own key.
Co-authored-by: Copilot App <[email protected]>
|
🤖 I pushed b77a9ff to make the tests smaller. The targets change is good. I did not change it. Test changes:
Local results (Windows, local SDK):
I also updated the PR description to match the current code and tests. |
|
@dalexsoto review |
dalexsoto
left a comment
There was a problem hiding this comment.
Reviewed b77a9ff4 against main at b0843532 (merge-base 380fbcbb). This was a static, read-only review. I didn't build or run anything.
LGTM. The new _ValidateJavaVersion target keeps the old task's behaviour. It reads JAVA_VERSION from the JDK release file first and falls back to java -version. A bare major version such as 21 becomes 21.0, so _JdkVersion is always empty or a dotted version that Version.Parse accepts. That holds for every consumer: Javac, JavaToolTask.GetJavaOptions and Lint, the StartsWith checks in AdjustJavacVersionArguments, and the LibBinding targets. The java path goes to Exec through an escaped environment variable, quoted on both Windows (with delayed expansion disabled) and Unix, so special characters in paths stay literal. "Picked up JAVA_TOOL_OPTIONS" lines on stderr don't confuse the regex. XA0030, XA0031 and XA0033 take the same arguments as before and still honour _AndroidAllowMissingSdkTooling. The empty-version guards short-circuit before VersionLessThan/VersionGreaterThan. _ResolveAndroidTooling runs _ComputeAndroidToolingInputs before _ValidateJavaVersion, so the outer/inner resolved-tooling reuse and the design-time skip behave as before. Nothing left references the deleted task or the XA0034 resource. xa0034.md is still there, so the TOC link still works.
Non-blocking: Documentation/docs-mobile/messages/index.md still lists the VJV → ValidateJavaVersion task prefix, and the localized Resources.*.resx files keep stale XA0034 entries until the next localization sync.
|
🤖 The latest CI failure is not related to this PR. It is a known flaky timing test.
Use "Retry failed jobs" in Azure DevOps to run the job again. |
Context: #13460
Context: #8663
<ValidateJavaVersion/>reads the JDK version and compares it with$(MinimumSupportedJavaVersion)and$(LatestSupportedJavaVersion). #13460 moved tasks with fixed regular expressions into Microsoft.Android.Build.Tasks for[GeneratedRegex]. For this task, that move needs a Full/Core<UsingTask/>pair, a .NET task host in Visual Studio, MSBuild on .NET 12 or later (MSB4061 on .NET 11), and a split ofMonoAndroidHelper.RunProcess(). The task only reads a file, runs one process, and compares two versions. MSBuild can do this, so delete the task and do the validation in the new_ValidateJavaVersiontarget inMicrosoft.Android.Sdk.Tooling.targets._ValidateJavaVersion_ResolveAndroidToolingdepends on_ComputeAndroidToolingInputs;_ValidateJavaVersion._ComputeAndroidToolingInputsis the old first half of_ResolveAndroidTooling._ValidateJavaVersionuses the old task condition,'$(_AndroidUseResolvedTooling)' != 'true' and '$(DesignTimeBuild)' != 'true', and sets$(_JdkVersion)and$(_DefaultJdkVersion).JAVA_VERSION="…"in thereleasefile. The target reads../releasenext to the selectedjava($(JavaToolExe)resolved against$(_JavaSdkDirectory)/bin) first, then$(_JavaSdkDirectory)/release. This is the same order as the old task.releasefile gives a version,<Exec ConsoleToMSBuild="true"/>runsjava -version. The path goes to the command through an environment variable, so quotes,;,$, and%in it stay literal.21, becomes21.0.System.Versioncannot parse21, and<Javac/>gets$(_JdkVersion).[MSBuild]::VersionLessThan()/VersionGreaterThan()compare the versions.<AndroidError/>/<AndroidWarning/>with the sameResources.resxnames, so they stay localized.ContinueOnError="$(_AndroidAllowMissingSdkTooling)"applies as before.Removed
$(_AndroidUseJavaExeVersion)switch from [Xamarin.Android.Build.Tasks] parse JDKreleasefile directly #8663. Only tests set it, and it was the only reasonjavac -versionran. TheJavacToolExeandUseJavaExeVersiontooling inputs only fed it.Behavior differences
javac -versionis not checked.RegisterTaskObject()cache ofjava -versionresults is gone. Normally no process starts, and validation runs once in the outer build.25.0and25.0.0as equal;System.Versiondoes not. This only matters exactly at a limit, where the target is slightly more lenient.$(MinimumSupportedJavaVersion)fails[System.Version]::Parse()with an MSBuild error, not XA0034.javagives XA0033 ((no output)when it prints nothing), not an exception wrapped in XA0034.Tests
ValidateJavaVersionTestsbuilds_ValidateJavaVersionin an in-memoryProjectInstance. It covers the version range (including21and five-part versions),$(JavaToolExe)selecting a different JDK,java -versionoutput withPicked up …lines, unparsable output (XA0033), an invalidreleasefile, special characters in the JDK path, relative paths, XA5300, and$(_AndroidAllowMissingSdkTooling). One test builds_ResolveAndroidToolingin a full project.XASdkTestscounts executed_ValidateJavaVersiontargets.DesignTimeResourceTargetsTestsreads the condition from_ValidateJavaVersion.ResolveSdksTaskTestsno longer creates the deleted task.Validation
On Windows, with a local SDK (
dotnet-local.cmd build Microsoft.Android.slnx,-t:BuildExtraApiLevels, and-t:ConfigureLocalWorkload) andTEST_ANDROID_JDK_PATHset to JDK 25:dotnet-local.cmd test bin\TestDebug\net11.0\Xamarin.Android.Build.Tests.dll --filter FullyQualifiedName~ValidateJavaVersionTests: 18 passed.--filter Name~<test>forXASdkTestsNestedBuildReusesResolvedAndroidTooling,NestedBuildToolingFallsBackForChangedInputs,NestedBuildPreservesEscapedNdkPath, andPublishReusesResolvedToolingInSpecializedInnerBuilds: 11 passed.dotnet-local.cmd test src\Microsoft.Android.Build.Tasks\Tests\Microsoft.Android.Build.Tasks.Tests\Microsoft.Android.Build.Tasks.Tests.csproj --filter FullyQualifiedName~DesignTimeResourceTargetsTests: 16 passed.