Skip to content

[AndroidSdk] Replace uname and rename P/Invokes - #13457

Merged
jonathanpeppers merged 12 commits into
mainfrom
simonrozsival-sdk-native-interop
Nov 16, 2026
Merged

jonathanpeppers merged 12 commits into
mainfrom
simonrozsival-sdk-native-interop

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Context: #10763
Context: #12988
Context: #13375

Xamarin.Android.Tools.AndroidSdk.OS calls uname() and allocates an unmanaged buffer to determine whether the host is Darwin. Android SDK tooling runs on desktop development hosts, so use RuntimeInformation.IsOSPlatform (OSPlatform.OSX) directly instead. The Windows check and the fallback for non-Windows, non-macOS hosts remain unchanged; separate checks for iOS, tvOS, watchOS, and Mac Catalyst are not needed.

FileUtil.SystemRename() is used in production only by AndroidAppManifest.WriteToFile(). Its Unix rename() and Windows backup/restore paths have different semantics from a managed file move. Keep the public WriteToFile(string) API, but write directly to the destination with XmlWriter.Create (fileName, ...) and remove the rename helpers. Also remove the unused duplicate build-tasks FileUtil.

Compatibility: The public API does not change. WriteToFile() no longer writes to a temporary file first, so a serialization failure can leave the destination file incomplete.

The chmod fallback and Windows path/registry interop are unchanged. #10763 migrated different runtime P/Invokes. #12988 changes the SDK chmod fallback and targeting, and #13375 adds safety annotations to the removed declarations. Neither is a prerequisite, but both touch these files and may need rebasing.

Validation

After restoring WriteToFile() (a2839ac), on Windows x64:

  • dotnet build src/Xamarin.Android.Tools.AndroidSdk/Xamarin.Android.Tools.AndroidSdk.csproj — passed, 0 warnings.
  • dotnet test tests/Xamarin.Android.Tools.AndroidSdk-Tests/Xamarin.Android.Tools.AndroidSdk-Tests.csproj --filter 'FullyQualifiedName~AndroidAppManifest|FullyQualifiedName~FileUtil|FullyQualifiedName~OS' — 28 passed, 4 skipped.

Before that change, on macOS arm64, using a session-owned toolchain and home, with main 6b8fb8c3742b8ea4aa5f9f92e70a601a28f83360 as the baseline:

  • make leeroy MSBUILD_ARGS=-p:IncludeAndroidJavadoc=true — passed; two source-control/SourceLink warnings.
  • ./dotnet-local.sh test bin/TestDebug/net11.0/Xamarin.Android.Build.Tests.dll --filter 'FullyQualifiedName~RemoveDirTests|FullyQualifiedName~BuildTest2.NonAndroidNativeLibrariesDoNotProduceWarnings' -v minimal — 7 passed, 4 skipped.
  • dotnet build src/Xamarin.Android.Tools.AndroidSdk/Xamarin.Android.Tools.AndroidSdk.csproj -c Release -p:AndroidToolsDisableMultiTargeting=false -p:DotNetTargetFrameworkVersion=11.0 -v minimal — passed for net11.0 and netstandard2.0.
  • dotnet build src/Xamarin.Android.Tools.AndroidSdk/Xamarin.Android.Tools.AndroidSdk.csproj -c Release -p:AndroidToolsDisableMultiTargeting=false -p:DotNetTargetFrameworkVersion=10.0 -v minimal — passed for net10.0 and netstandard2.0.
  • dotnet test tests/Xamarin.Android.Tools.AndroidSdk-Tests/Xamarin.Android.Tools.AndroidSdk-Tests.csproj -p:AndroidToolsDisableMultiTargeting=false -p:DotNetTargetFrameworkVersion=11.0 -v minimal — 536 passed, 8 skipped.
  • dotnet test tests/Xamarin.Android.Tools.AndroidSdk-Tests/Xamarin.Android.Tools.AndroidSdk-Tests.csproj -p:AndroidToolsDisableMultiTargeting=false -p:DotNetTargetFrameworkVersion=10.0 -v minimal — 536 passed, 8 skipped.
  • dotnet test tests/Xamarin.Android.Tools.AndroidSdk-Tests/Xamarin.Android.Tools.AndroidSdk-Tests.csproj -p:AndroidToolsDisableMultiTargeting=true -p:DotNetTargetFrameworkVersion=11.0 --filter 'FullyQualifiedName!~CreateProcessStartInfo_SingleArg&FullyQualifiedName!~CreateProcessStartInfo_MultipleArgs&FullyQualifiedName!~CreateProcessStartInfo_ArgWithSpaces' -v minimal — 529 passed, 8 skipped. The three excluded ProcessUtilsTests argument-list failures also reproduce against the saved main-baseline assembly.

A session-only probe verified macOS classification and native-library format on arm64 and x64/Rosetta for all three target frameworks.

Performance

The paired OS-constructor benchmark measured intermediate commit 9e977cbdf7, not the current PR head. Median times were 841.9 → 163.1 µs for net11.0 and 834.5 → 179.4 µs for netstandard2.0. These are class-constructor timings, not application startup or build times. Final-head timing was deferred due to host contention; no speedup is claimed for the current head.

simonrozsival and others added 6 commits November 10, 2026 04:35
* OS.IsRunningOnMac: use OperatingSystem (net) / RuntimeInformation
  (netstandard2.0) platform checks instead of P/Invoking uname(3).
  All Apple platforms are matched, as uname reports "Darwin" on each.
* FileUtil.SystemRename: use a LibraryImport'd rename(2) on .NET,
  keeping exact rename(2) and errno semantics; netstandard2.0 keeps
  the DllImport. File.Move(overwrite) was rejected: it falls back to
  non-atomic copy+delete on EXDEV, rejects directory sources, and
  throws UnauthorizedAccessException instead of errno IOExceptions.
* Remove unused Xamarin.Android.Build.Tasks Utilities/FileUtil.cs and
  its rename P/Invoke.

Co-authored-by: Copilot App <[email protected]>
uname() reports the "Darwin" sysname on watchOS too, so the managed
replacement for the uname P/Invoke must treat watchOS as Mac on both the
.NET (OperatingSystem.IsWatchOS) and netstandard2.0
(OSPlatform.Create ("WATCHOS")) paths.

Add a host test comparing OS.IsMac/IsLinux/IsWindows with the original
uname()-based classification.

Co-authored-by: Copilot App <[email protected]>
The runtime platform checks do not establish uname equivalence on
unsupported platforms such as watchOS. Correct the comment without
changing the detection logic.

Co-authored-by: Copilot App <[email protected]>
The platform detection implementation now relies on runtime platform checks instead of calling uname(3). Remove the host-only comparison, which required a separate uname P/Invoke in the test suite.

Co-authored-by: Copilot App <[email protected]>
AndroidAppManifest.WriteToFile remains public and uses the native rename helper. Cover non-ASCII paths and both new and existing destinations so the source-generated and netstandard marshalling paths preserve this behavior.

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

AndroidAppManifest.WriteToFile() is the only production caller of FileUtil.SystemRename(), and no in-tree callers use the file-writing method. The manifest type is still used by build tasks, so retain it while removing the file writer and its native rename implementation.

This removes a shipped public API. Consumers can use Write(XmlWriter) and manage file creation and replacement themselves. Record the removal in the public API baselines and remove tests for the deleted operations.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 10, 2026 15:01

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

Removing the shipped public AndroidAppManifest.WriteToFile(string) API is binary-breaking and requires maintainer confirmation of downstream compatibility and release-policy acceptance.

No open issues 🎉

What changed in this PR

This PR simplifies Android SDK tooling by replacing native uname detection with managed platform checks and removing unused manifest-writing and rename code.

Changes:

  • Uses runtime platform checks for Darwin classification, with watchOS equivalence explicitly unverified.
  • Intentionally removes the shipped AndroidAppManifest.WriteToFile(string) API; Write(XmlWriter) remains, with file creation and replacement left to callers.
  • Removes unused rename helpers, the duplicate build-task utility, and their tests. The chmod fallback and Windows interop remain unchanged.
File Description
tests/​Xamarin.Android.Tools.AndroidSdk-Tests/​SystemRenameTests.cs Deletes tests for the removed rename helper.
tests/​Xamarin.Android.Tools.AndroidSdk-Tests/​FileUtilTests.cs Removes rename-specific cases and an unused import.
src/​Xamarin.Android.Tools.AndroidSdk/​PublicAPI/​netstandard2.0/​PublicAPI.Unshipped.txt Records the manifest writer API removal for netstandard2.0.
src/​Xamarin.Android.Tools.AndroidSdk/​PublicAPI/​net11.0/​PublicAPI.Unshipped.txt Records the manifest writer API removal for net11.0.
src/​Xamarin.Android.Tools.AndroidSdk/​PublicAPI/​net10.0/​PublicAPI.Unshipped.txt Records the manifest writer API removal for net10.0.
src/​Xamarin.Android.Tools.AndroidSdk/​OS.cs Replaces uname interop with managed Apple-platform checks.
src/​Xamarin.Android.Tools.AndroidSdk/​FileUtil.cs Removes temporary-name and rename helpers, including native interop.
src/​Xamarin.Android.Tools.AndroidSdk/​AndroidAppManifest.cs Removes WriteToFile, retaining Write(XmlWriter).
src/​Xamarin.Android.Build.Tasks/​Utilities/​FileUtil.cs Deletes the unused duplicate utility.

Review effort: Balanced

@simonrozsival

simonrozsival commented Nov 11, 2026 •

Copy link
Copy Markdown
Member Author

OS classification microbenchmark — earlier candidate

On macOS arm64 with the .NET 11 runtime, 40 counterbalanced cold-process pairs compared main 6b8fb8c3742b8ea4aa5f9f92e70a601a28f83360 with 9e977cbdf7e699a8ceb0d70a8f3cc00f94b2ccd7:

AndroidSdk assembly Baseline median Candidate median Ratio of medians
net11.0 841.9 µs 163.1 µs 5.16×
netstandard2.0 834.5 µs 179.4 µs 4.65×

These measure OS class-constructor time in fresh processes, not complete process startup, application startup, or build time. The run used heavy-slot.sh --toolchain; one-minute load ranged from 3.36 to 3.57 across six snapshots, four during measurement. No competing build/test processes or heavy-slot holders were detected in those samples, but the host was shared and short activity between samples cannot be ruled out.

This measurement predates the later macOS-only simplification and does not measure the current PR head.

@simonrozsival

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #13457

@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.

⚠️ Needs Changes — compatibility decision outstanding

Reviewed all nine changed files, their surrounding source, callers, and AndroidSdk target-framework configuration. 0 errors, 1 warning, 1 suggestion, posted inline. The warning asks for explicit downstream-compatibility/release acceptance of the intentional shipped WriteToFile(string) removal; absent that acceptance, preserve a compatibility entry point. The suggestion adds independent platform-classification coverage for both target-framework paths.

The managed Darwin checks remove the unmanaged allocation and uname call while retaining the existing Windows/non-Darwin classification on supported desktop hosts. No in-repository callers of the removed writer or rename helpers remain. The API-removal records are consistent across all three existing framework baselines, and the chmod fallback and Windows interop are unchanged. The description clearly distinguishes the API break and watchOS limitation; no additional correctness defect was identified in the remaining implementation.

CI: All 41 GitHub check runs report success, including the aggregate dotnet-android check for Azure build 1673052. Direct Azure build/timeline verification was blocked by CLI startup and authentication errors, so Azure status was not independently confirmed. No local builds or tests were run for this read-only review.

Generated by Android PR Reviewer for #13457 · copilot · gpt61 · 31.3 AIC · ⌖ 7.63 AIC · ⊞ 19.5K
Comment /review to run again

@@ -1,4 +1,5 @@
#nullable enable
*REMOVED*Xamarin.Android.Tools.AndroidAppManifest.WriteToFile(string! fileName) -> void

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.

🤖 ⚠️ API design — Could we confirm downstream-consumer compatibility and maintainer acceptance of this shipped API break before removing it? The absence of in-repository callers does not establish that consumers of the published AndroidSdk package no longer use it: an existing binary calling AndroidAppManifest.WriteToFile(string) will fail with MissingMethodException after upgrading. The description correctly acknowledges this, but Write(XmlWriter) is neither a binary-compatible replacement nor an equivalent file-replacement API. Unless the break is approved for this release, retain an obsolete compatibility entry point and preserve its replacement behavior. This applies to the net10.0 and net11.0 removals as well.

{Rule: Shared tooling API-shape changes require downstream binary-compatibility review.}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The API removal remains intentional and was explicitly requested by the PR author after confirming there are no in-repository callers. A compatibility entry point would retain the file-writing/rename behavior this change is removing. I cannot independently confirm downstream compatibility or release-policy acceptance, so I am leaving this thread open for maintainer review of that release decision.

Auto-replied by the GitHub Copilot app

Add independent runtime-platform assertions for the OS classification and native library format. Run the test with both the netstandard2.0 and modern .NET AndroidSdk assemblies.

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

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #13457

@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.

⚠️ Needs Changes — outstanding compatibility decision

Reviewed all 10 changed files, the removed implementations, callers, target-framework configuration, and the Android tools test pipeline at 877d1f2f1c2ff65d34cf682a9516ec88359bfa37. New findings: 0 errors, 0 warnings, 1 documentation suggestion, posted inline. No additional correctness defects found.

The existing unresolved API-design thread still needs a maintainer decision: removing the shipped AndroidAppManifest.WriteToFile(string) breaks downstream source/binary compatibility, even though no in-repository callers remain. The PR accurately acknowledges both that intentional break and the unsupported-platform/watchOS limitation; I have not duplicated the existing compatibility finding.

The managed platform checks remove the unmanaged allocation and uname call without changing the constructor's Windows/non-Apple fallback logic. The new platform-classification test addresses the previous testing feedback, and the removed rename tests correspond to functionality that is actually deleted. The chmod fallback and Windows interop are unchanged.

CI: All 40 head-commit GitHub checks report success, including the aggregate dotnet-android check and Windows/macOS Android Tools Tests. A direct query of Azure build 1675489 failed because az devops invoke requires authentication in this environment, so independent Azure build/timeline verification is incomplete. No local builds or tests were run for this review.

Generated by Android PR Reviewer for #13457 · copilot · gpt61 · 37.4 AIC · ⌖ 4.14 AIC · ⊞ 20.2K
Comment /review to run again

simonrozsival and others added 3 commits November 13, 2026 09:34
Context: #13457

Android SDK tooling runs on desktop development hosts and does not need to classify iOS, tvOS, watchOS, or Mac Catalyst as macOS.

Replace the private helper with a direct RuntimeInformation.IsOSPlatform (OSPlatform.OSX) check for both target frameworks. Keep Windows detection and the existing fallback for other platforms.

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

Removing AndroidAppManifest.WriteToFile breaks existing callers. Document how consumers can migrate to Write(XmlWriter), and explain why writing directly to the destination does not preserve the old temporary-file replacement behavior.

Co-authored-by: Copilot App <[email protected]>
@simonrozsival simonrozsival added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Nov 13, 2026
@jonathanpeppers

Copy link
Copy Markdown
Member

Conflicts

image

@simonrozsival

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #13457

@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.

⚠️ Needs Changes — existing compatibility decision remains open

Reviewed all 10 changed files and searched the repository for callers of the removed APIs. No new correctness defects found. New findings: 0 errors, 0 warnings, 1 documentation suggestion (inline).

The RuntimeInformation.IsOSPlatform(OSPlatform.OSX) replacement fits the desktop-host scope and the netstandard2.0 target. Removing the writer together with both unused rename implementations avoids silently changing replacement semantics. The API removal is recorded for all three target frameworks, and the new README clearly explains the binary break and direct-write truncation risk.

The existing API-compatibility thread remains unresolved: intentional removal and absence of in-tree callers do not establish release-policy acceptance for downstream binaries. I have not duplicated that finding or the platform-test suggestion already considered in the earlier review.

CI: All 40 head-commit GitHub checks report success, including the overall dotnet-android check for Azure build 1679285. Direct Azure build/timeline verification was blocked because az devops invoke requires authentication in this environment, so that part of CI verification is incomplete. GitHub also currently reports merge conflicts (mergeable_state: dirty).

Generated by Android PR Reviewer for #13457 · copilot · gpt61 · 34.2 AIC · ⌖ 5.19 AIC · ⊞ 21K
Comment /review to run again

@simonrozsival simonrozsival removed the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Nov 14, 2026

@jonathanpeppers jonathanpeppers left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm good with removing the p/invoke, but we can keep the public API still. 👍

Restore the public WriteToFile API and write directly to the
destination file with XmlWriter, so the rename p/invoke is not needed.
Revert the README and PublicAPI changes.

Co-authored-by: Copilot App <[email protected]>
@jonathanpeppers jonathanpeppers changed the title [AndroidSdk] Replace uname and remove the unused manifest file writer [AndroidSdk] Replace uname and rename P/Invokes Nov 16, 2026
@jonathanpeppers jonathanpeppers added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Nov 16, 2026
…ative-interop-review

# Conflicts:
#	src/Xamarin.Android.Tools.AndroidSdk/FileUtil.cs
#	src/Xamarin.Android.Tools.AndroidSdk/OS.cs
@jonathanpeppers

Copy link
Copy Markdown
Member

@dalexsoto review

@jonathanpeppers
jonathanpeppers enabled auto-merge (squash) November 16, 2026 20:38

@dalexsoto dalexsoto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed 03912cd3 against main at f9c38dcb, which is also the merge-base. This was a static, read-only review. I didn't build or run anything.

LGTM. Switching OS to RuntimeInformation.IsOSPlatform(OSPlatform.OSX) keeps the same IsWindows/IsMac/IsLinux results. Windows is still checked first, and the static constructor gains no new way to fail. AndroidAppManifest.WriteToFile(string) keeps its public signature and its XmlWriterSettings (UTF-8 BOM, tab indent, \n), and XmlWriter.Create(path) truncates the existing file. Writing directly to the destination is the tradeoff you agreed on in this thread. The deleted Build.Tasks FileUtil was a separate internal copy. Build.Tasks only takes a ProjectReference on AndroidSdk and doesn't link its sources, and nothing left in the tree calls SystemRename or GetTempFilenameForWrite. Every removed member was internal or private, and the removed tests only covered SystemRename.

@jonathanpeppers
jonathanpeppers merged commit 428307d into main Nov 16, 2026
39 checks passed
@jonathanpeppers
jonathanpeppers deleted the simonrozsival-sdk-native-interop branch November 16, 2026 23:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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.

4 participants