Skip to content

Commit eaec4e3

Browse files
authored
[Xamarin.Android.Tools.AndroidSdk] More Microsoft Dist JDK Support (#130)
Fixes: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1314263 Remember `AndroidSdkInfo.DetectAndSetPreferredJavaSdkPathToLatest()` (commit a4aad18)? It's supposed to "detect" the latest installed "preferred" JDK installation location, and set configuration values so that subsequent `new AndroidSdkInfo()` expressions will use that JDK location, instead of some other JDK location. The "preferred" JDK was a JDK that was "known good" for Xamarin.Android use, i.e. one that Visual Studio installed. In a4aad18, this was the `microsoft_dist_openjdk_*` version. When we (prematurely) removed support for the `microsoft_dist_openjdk_` build in commits 237642c and dca30d9, we updated `AndroidSdkInfo.DetectAndSetPreferredJavaSdkPathToLatest()` to instead prefer the [Microsoft OpenJDK][0] build, but this migration needed to be delayed until Visual Studio 17.0. Which means that the "current world order" is *wrong*: `AndroidSdkInfo.DetectAndSetPreferredJavaSdkPathToLatest()` tries to detect a JDK that is not currently installed by anything. Futhermore, Visual Studio for Mac will call `AndroidSdkInfo.DetectAndSetPreferredJavaSdkPathToLatest()` if no JDK is currently configured. The result is that on "clean" systems, Visual Studio for Mac can crash with a `NotSupportedException` because it's looking for a Microsoft OpenJDK installation, but only a `microsoft_dist_openjdk_` installation is available. Thus, no preferred JDK is found, and the IDE crashes. Fix this by adding a new (`internal`) `JdkInfo.GetPreferredJdkInfos()` method, which appropriately checks *both* Microsoft OpenJDK *and* `microsoft_dist_openjdk_` installation locations, and update `AndroidSdkInfo.DetectAndSetPreferredJavaSdkPathToLatest()` to now use `JdkInfo.GetPreferredJdkInfos()`. (We add the method to `JdkInfo` so that it is easier to keep `GetPreferredJdkInfos()` and `GetKnownSystemJdkInfos()` consistent with each other.) [0]: https://www.microsoft.com/openjdk
1 parent f9c1b0d commit eaec4e3

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public static void DetectAndSetPreferredJavaSdkPathToLatest (Action<TraceLevel,
185185

186186
logger = logger ?? DefaultConsoleLogger;
187187

188-
var latestJdk = MicrosoftOpenJdkLocations.GetMicrosoftOpenJdks (logger).FirstOrDefault ();
188+
var latestJdk = JdkInfo.GetPreferredJdkInfos (logger).FirstOrDefault ();
189189
if (latestJdk == null)
190190
throw new NotSupportedException ("No Microsoft OpenJDK could be found. Please re-run the Visual Studio installer or manually specify the JDK path in settings.");
191191

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ static Dictionary<string, List<string>> GetJavaProperties (string java)
280280
return props;
281281
}
282282

283+
// Keep ordering in sync w/ GetPreferredJdkInfos
283284
public static IEnumerable<JdkInfo> GetKnownSystemJdkInfos (Action<TraceLevel, string>? logger = null)
284285
{
285286
logger = logger ?? AndroidSdkInfo.DefaultConsoleLogger;
@@ -300,6 +301,14 @@ public static IEnumerable<JdkInfo> GetKnownSystemJdkInfos (Action<TraceLevel, st
300301
;
301302
}
302303

304+
// Keep ordering in sync w/ GetKnownSystemJdkInfos
305+
internal static IEnumerable<JdkInfo> GetPreferredJdkInfos (Action<TraceLevel, string> logger)
306+
{
307+
return MicrosoftOpenJdkLocations.GetMicrosoftOpenJdks (logger)
308+
.Concat (MicrosoftDistJdkLocations.GetMicrosoftDistJdks (logger))
309+
;
310+
}
311+
303312
internal static JdkInfo? TryGetJdkInfo (string path, Action<TraceLevel, string> logger, string locator)
304313
{
305314
JdkInfo? jdk = null;

0 commit comments

Comments
 (0)