Commit c6fc83d
Add JDK installation support (Microsoft OpenJDK) (#274)
Fixes: #270
Adds JDK installation support to `Xamarin.Android.Tools.AndroidSdk`
using the **Microsoft Build of OpenJDK**
(https://www.microsoft.com/openjdk).
## New Files
- **`JdkInstaller.cs`** — Discovers, downloads, installs, validates,
and removes Microsoft OpenJDK
- **`DownloadUtils.cs`** — Shared helpers: download with progress,
SHA-256 checksum verification, Zip Slip-safe ZIP extraction, tar.gz
extraction, checksum file fetching/parsing
- **`Models/JdkVersionInfo.cs`** — Version metadata (major, minor,
patch, build, platform, architecture, URL, checksum URL)
- **`Models/JdkInstallPhase.cs`** — `enum` for progress reporting
phases (Downloading, Verifying, Extracting, etc.)
- **`Models/JdkInstallProgress.cs`** — `record` combining phase,
percentage, and message for `IProgress<T>` callbacks
- **`IsExternalInit.cs`** — Enables `init` accessors on netstandard2.0
- **`.github/copilot-instructions.md`** — Repository-specific Copilot
context
## Modified Files
- **`FileUtil.cs`** — Added `TryDeleteFile`, `TryDeleteDirectory`,
`MoveWithRollback`, `CommitMove`, `IsTargetPathWritable`,
`IsUnderDirectory`, `GetInstallerExtension`, `GetArchiveExtension`
- **`ProcessUtils.cs`** — Added `CreateProcessStartInfo` (uses
`ArgumentList` on net5+, `JoinArguments` fallback), `IsElevated`
(uses `Environment.IsPrivilegedProcess` on net7+, P/Invoke fallback)
- **`.csproj`** — Added `System.Buffers` 4.5.1 (netstandard2.0),
`IsExternalInit.cs` exclusion on modern TFMs
## API Surface
```csharp
public class JdkInstaller : IDisposable
{
public JdkInstaller(Action<string>? logger = null);
public static int RecommendedMajorVersion { get; } // 21
public static IReadOnlyList<int> SupportedVersions { get; } // [21]
public Task<IReadOnlyList<JdkVersionInfo>> DiscoverAsync(CancellationToken ct = default);
public Task InstallAsync(int majorVersion, string targetPath,
IProgress<JdkInstallProgress>? progress = null, CancellationToken ct = default);
public bool IsValid(string jdkPath);
public bool Remove(string jdkPath);
public void Dispose();
}
// Helpers used by JdkInstaller (not on JdkInstaller itself):
// FileUtil.IsTargetPathWritable(string, Action<TraceLevel, string>)
// ProcessUtils.IsElevated()
```
## Key Design Decisions
- **Microsoft OpenJDK only** — downloads from
`https://aka.ms/download-jdk` with SHA-256 checksum verification
- **JDK 21 only** — `RecommendedMajorVersion` = 21,
`SupportedVersions` = `[21]`. Array allows future additions
- **Owns its `HttpClient`** — no injection; `Dispose()` cleans it up
- **Elevated install mode** — when `ProcessUtils.IsElevated()` is
true, uses platform-native silent installers (`.msi` via `msiexec
/qn`, `.pkg` via `/usr/sbin/installer`). When not elevated, extracts
archive to `targetPath`
- **Safe directory replacement** — `MoveWithRollback`: rename existing
→ temp, move new in place, delete temp via `CommitMove`. Avoids
Delete/Move race on Windows
- **Zip Slip protection** — ZIP extraction validates that all entry
paths resolve under the destination directory
- **`ArrayPool<byte>` download buffer** —
`ArrayPool<byte>.Shared.Rent` for the 80 KB download buffer,
returned in `finally`
- **Cross-platform P/Invoke fallbacks** — `IsUserAnAdmin()` (shell32)
on Windows, `geteuid()` (libc) on Unix, behind `#if
!NET5_0_OR_GREATER`
- **`NullProgress` pattern** — avoids null checks throughout install
flow
- **`netstandard2.0` + `net10.0`** — conditional compilation for
`ArgumentList`, `Environment.IsPrivilegedProcess`,
`ReadAsStringAsync`/`ReadAsStreamAsync` overloads. Private helpers
encapsulate `#if` blocks
## Tests
66 unit tests across 5 test files (all target `net10.0`):
| Test File | Count | Coverage |
|--------------------------|-------|------------------------------------------------------------------------------------------|
| `JdkInstallerTests.cs` | 19 | `IsValid`, `DiscoverAsync`, `InstallAsync`, `IsTargetPathWritable`, `Remove`, properties |
| `DownloadUtilsTests.cs` | 20 | `ParseChecksumFile` (12 cases), `VerifyChecksum` (4), `ExtractZipSafe` + Zip Slip (4) |
| `FileUtilTests.cs` | 15 | `MoveWithRollback` (4), `IsUnderDirectory` (11 including cross-platform path cases) |
| `ProcessUtilsTests.cs` | 7 | `CreateProcessStartInfo` (6), `IsElevated` smoke (1) |
| `JdkVersionInfoTests.cs` | 5 | Constructor, defaults, `ToString`, mutable properties |
## Review Feedback Addressed
JDK 21 only (removed 17), removed `HttpClient` injection,
`IDisposable`, `CancellationToken` pattern, path normalization,
validation cleanup, `ArgumentList` for tar, absolute `/usr/bin/tar`,
null comparison pattern, one type per file, `record` type, checksum
errors throw, test `TearDown` dispose, CI skip, elevated install mode,
Zip Slip protection, `ArrayPool` buffer, extracted constants
(`BufferSize`, `BytesPerMB`, `WhitespaceChars`), private
`ReadAsStreamAsync`/`ReadAsStringAsync` helpers, cross-platform test
paths, comprehensive unit test coverage
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Jonathan Peppers <jonathan.peppers@microsoft.com>1 parent 062cc29 commit c6fc83d
File tree
15 files changed
+1567
-2
lines changed- .github
- src/Xamarin.Android.Tools.AndroidSdk
- Models/Jdk
- tests/Xamarin.Android.Tools.AndroidSdk-Tests
15 files changed
+1567
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
27 | 39 | | |
28 | 40 | | |
| 41 | + | |
29 | 42 | | |
30 | 43 | | |
31 | 44 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
54 | 184 | | |
55 | 185 | | |
56 | 186 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
0 commit comments