Skip to content

Commit a428178

Browse files
[Xamarin.Android.Tools.AndroidSdk] fall back to android-{major}.0 (#320)
Fixes: #319 Starting with API 37, Google's SDK Manager installs platform directories as `android-37.0` instead of `android-37`. Both `TryGetPlatformDirectoryFromApiLevel` and `IsPlatformInstalled` only tried `android-{major}`, so projects targeting these newer API levels would fail with **XA5207**. This adds a `.0` fallback to both methods: when the integer-only directory doesn't exist, also try `android-{id}.0`. This complements the data-side fix in dotnet/android#11072 (which sets the `Id` to `"37.0"` directly) by providing a safety net when the `Id` is just `"37"`. ### Changes - **`AndroidSdkInfo.TryGetPlatformDirectoryFromApiLevel`** — After existing lookups fail, try `GetPlatformDirectoryFromId(id + ".0")` when `id` is an integer. - **`AndroidSdkInfo.IsPlatformInstalled`** — Apply the same `.0` fallback so it stays consistent with `TryGetPlatformDirectoryFromApiLevel`. - **`AndroidSdkInfoTests`** — New test `TryGetPlatformDirectoryFromApiLevel_MajorFallsBackToMajorDotZero` creates only `android-37.0` and verifies both APIs find it. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent ec5040a commit a428178

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/Xamarin.Android.Tools.AndroidSdk/AndroidSdkInfo.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,23 @@ public string GetPlatformDirectoryFromId (string id)
120120
if (dir != null && Directory.Exists (dir))
121121
return dir;
122122

123+
// Starting with API 37, Google's SDK Manager installs platforms to
124+
// "android-37.0" instead of "android-37". Try the "{major}.0" fallback.
125+
// See: https://github.com/dotnet/android-tools/issues/319
126+
if (int.TryParse (id, out _)) {
127+
dir = GetPlatformDirectoryFromId (id + ".0");
128+
if (Directory.Exists (dir))
129+
return dir;
130+
}
131+
123132
return null;
124133
}
125134

126135
public bool IsPlatformInstalled (int apiLevel)
127136
{
128-
return apiLevel != 0 && Directory.Exists (GetPlatformDirectory (apiLevel));
137+
return apiLevel != 0 &&
138+
(Directory.Exists (GetPlatformDirectory (apiLevel)) ||
139+
Directory.Exists (GetPlatformDirectoryFromId (apiLevel + ".0")));
129140
}
130141

131142
public string? AndroidNdkPath {

tests/Xamarin.Android.Tools.AndroidSdk-Tests/AndroidSdkInfoTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,44 @@ public void TryGetPlatformDirectoryFromApiLevel_MinorVersionDoesNotFallback ()
633633
}
634634
}
635635

636+
[Test]
637+
public void TryGetPlatformDirectoryFromApiLevel_MajorFallsBackToMajorDotZero ()
638+
{
639+
// Starting with API 37, Google installs platforms to "android-37.0"
640+
// instead of "android-37". Verify the fallback works.
641+
// See: https://github.com/dotnet/android-tools/issues/319
642+
CreateSdks (out string root, out string jdk, out string ndk, out string sdk);
643+
644+
// Only create android-37.0, not android-37
645+
var platformsPath = Path.Combine (sdk, "platforms");
646+
var platform370Path = Path.Combine (platformsPath, "android-37.0");
647+
Directory.CreateDirectory (platform370Path);
648+
File.WriteAllText (Path.Combine (platform370Path, "android.jar"), "");
649+
650+
var logs = new StringWriter ();
651+
Action<TraceLevel, string> logger = (level, message) => {
652+
logs.WriteLine ($"[{level}] {message}");
653+
};
654+
655+
try {
656+
var info = new AndroidSdkInfo (logger, androidSdkPath: sdk, androidNdkPath: ndk, javaSdkPath: jdk);
657+
var versions = new AndroidVersions (new [] {
658+
new AndroidVersion (37, "17.0"),
659+
});
660+
661+
// Requesting "37" should fall back to android-37.0
662+
var dir37 = info.TryGetPlatformDirectoryFromApiLevel ("37", versions);
663+
Assert.IsNotNull (dir37, "Should fall back from android-37 to android-37.0");
664+
Assert.AreEqual (platform370Path, dir37);
665+
666+
// IsPlatformInstalled should also find android-37.0
667+
Assert.IsTrue (info.IsPlatformInstalled (37), "IsPlatformInstalled should find android-37.0");
668+
}
669+
finally {
670+
Directory.Delete (root, recursive: true);
671+
}
672+
}
673+
636674
[Test]
637675
public void GetBuildToolsPaths_StableVersionsFirst ()
638676
{

0 commit comments

Comments
 (0)