Skip to content

Commit 604940c

Browse files
[Xamarin.Android.Tools.AndroidSdk] Prevent fallback to major API level for minor (#267)
Context: dotnet/android#10720 When requesting a minor API level (e.g., 36.1), do not fall back to the major version (e.g., 36) if the specific minor version platform is not installed. Adds a test to verify this behavior and references issue dotnet/android#10720.
1 parent fb95edd commit 604940c

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ public string GetPlatformDirectoryFromId (string id)
107107
if (Directory.Exists (dir))
108108
return dir;
109109

110+
// Only fall back to major API level if we weren't explicitly requesting a minor version.
111+
// For example, if "36.1" was requested but android-36.1 doesn't exist, don't fall back to android-36
112+
// because the minor version may have APIs that the major version doesn't have.
113+
// See: https://github.com/dotnet/android/issues/10720
114+
if (Version.TryParse (id, out var version) && version.Minor != 0) {
115+
return null;
116+
}
117+
110118
var level = versions.GetApiLevelFromId (id);
111119
dir = level.HasValue ? GetPlatformDirectory (level.Value) : null;
112120
if (dir != null && Directory.Exists (dir))

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,46 @@ string UnixConfigPath {
593593
}
594594
}
595595

596+
[Test]
597+
public void TryGetPlatformDirectoryFromApiLevel_MinorVersionDoesNotFallback ()
598+
{
599+
// Verifies that when requesting a minor API level (like 36.1), we don't fall back
600+
// to the major version (36) if the minor version platform is not installed.
601+
// See: https://github.com/dotnet/android/issues/10720
602+
CreateSdks (out string root, out string jdk, out string ndk, out string sdk);
603+
604+
// Only create android-36, not android-36.1
605+
var platformsPath = Path.Combine (sdk, "platforms");
606+
var platform36Path = Path.Combine (platformsPath, "android-36");
607+
Directory.CreateDirectory (platform36Path);
608+
File.WriteAllText (Path.Combine (platform36Path, "android.jar"), "");
609+
610+
var logs = new StringWriter ();
611+
Action<TraceLevel, string> logger = (level, message) => {
612+
logs.WriteLine ($"[{level}] {message}");
613+
};
614+
615+
try {
616+
var info = new AndroidSdkInfo (logger, androidSdkPath: sdk, androidNdkPath: ndk, javaSdkPath: jdk);
617+
var versions = new AndroidVersions (new [] {
618+
new AndroidVersion (36, "16.0"),
619+
new AndroidVersion (new Version (36, 1), "16.0"),
620+
});
621+
622+
// Requesting "36" should find android-36
623+
var dir36 = info.TryGetPlatformDirectoryFromApiLevel ("36", versions);
624+
Assert.IsNotNull (dir36, "Should find android-36");
625+
Assert.AreEqual (platform36Path, dir36);
626+
627+
// Requesting "36.1" should NOT fall back to android-36
628+
var dir361 = info.TryGetPlatformDirectoryFromApiLevel ("36.1", versions);
629+
Assert.IsNull (dir361, "Should NOT fall back to android-36 when android-36.1 is requested but not installed");
630+
}
631+
finally {
632+
Directory.Delete (root, recursive: true);
633+
}
634+
}
635+
596636
[Test]
597637
public void GetBuildToolsPaths_StableVersionsFirst ()
598638
{

0 commit comments

Comments
 (0)