From 3974fc38c0f25f943b5d3bf0a4e174532a2a60ee Mon Sep 17 00:00:00 2001 From: Jonathan Pryor Date: Thu, 4 Jun 2020 13:27:53 -0400 Subject: [PATCH] [Xamarin.Android.Tools.AndroidSdk] JdkInfo + JDK11 + Windows (#88) Context: https://github.com/xamarin/xamarin-android/pull/4567 Commit 36d7fee5 added support for JetBrains OpenJDK 11 detection on macOS and Linux. Lacking was *Windows* support for JetBrains OpenJDK 11, because of course it has to be different. In particular, OpenJDK 11 *moves the `jvm` library* for Windows. We checked for it in `{HomePath}\jre\**\jvm.dll` or `{HomePath}\lib\**\jvm.dll`, but neither of those exist. Instead, OpenJDK 11 has `jvm.dll` in `{HomePath}\bin\server\jvm.dll`, which is also the only `.dll` file in the `.tar.gz` which exports the symbol `JNI_CreateJavaVM`. Update `JdkInfo` so that it looks for `{HomePath}\bin\server\jvm.dll` to populate `JdkInfo.JdkJvmPath`. --- .../JdkInfo.cs | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/src/Xamarin.Android.Tools.AndroidSdk/JdkInfo.cs b/src/Xamarin.Android.Tools.AndroidSdk/JdkInfo.cs index 4ea56bb..8ff1c4a 100644 --- a/src/Xamarin.Android.Tools.AndroidSdk/JdkInfo.cs +++ b/src/Xamarin.Android.Tools.AndroidSdk/JdkInfo.cs @@ -14,10 +14,6 @@ namespace Xamarin.Android.Tools { public class JdkInfo { - static readonly string[] JdkLibraryTopDirs = { - "jre", - "lib", - }; public string HomePath {get;} @@ -58,27 +54,14 @@ public JdkInfo (string homePath) JavaPath = ProcessUtils.FindExecutablesInDirectory (binPath, "java").FirstOrDefault (); JavacPath = ProcessUtils.FindExecutablesInDirectory (binPath, "javac").FirstOrDefault (); - string? topDir = null; - foreach (string dir in JdkLibraryTopDirs) { - topDir = Path.Combine (HomePath, dir); - if (!Directory.Exists (topDir)) { - topDir = null; - continue; - } - break; - } - - if (String.IsNullOrEmpty (topDir)) - topDir = Path.Combine (HomePath, JdkLibraryTopDirs [0]); - - JdkJvmPath = OS.IsMac - ? FindLibrariesInDirectory (topDir!, "jli").FirstOrDefault () - : FindLibrariesInDirectory (topDir!, "jvm").FirstOrDefault (); + string? jdkJvmPath = GetJdkJvmPath (); ValidateFile ("jar", JarPath); ValidateFile ("java", JavaPath); ValidateFile ("javac", JavacPath); - ValidateFile ("jvm", JdkJvmPath); + ValidateFile ("jvm", jdkJvmPath); + + JdkJvmPath = jdkJvmPath!; var includes = new List (); var jdkInclude = Path.Combine (HomePath, "include"); @@ -132,6 +115,24 @@ public bool GetJavaSettingsPropertyValue (string key, [NotNullWhen (true)] out s return false; } + string? GetJdkJvmPath () + { + string jreDir = Path.Combine (HomePath, "jre"); + string libDir = Path.Combine (HomePath, "lib"); + + if (OS.IsMac) { + return FindLibrariesInDirectory (jreDir, "jli").FirstOrDefault () ?? + FindLibrariesInDirectory (libDir, "jli").FirstOrDefault (); + } + if (OS.IsWindows) { + string binServerDir = Path.Combine (HomePath, "bin", "server"); + return FindLibrariesInDirectory (jreDir, "jvm").FirstOrDefault () ?? + FindLibrariesInDirectory (binServerDir, "jvm").FirstOrDefault (); + } + return FindLibrariesInDirectory (jreDir, "jvm").FirstOrDefault () ?? + FindLibrariesInDirectory (libDir, "jvm").FirstOrDefault (); + } + static IEnumerable FindLibrariesInDirectory (string dir, string libraryName) { if (!Directory.Exists (dir)) @@ -140,7 +141,7 @@ static IEnumerable FindLibrariesInDirectory (string dir, string libraryN return Directory.EnumerateFiles (dir, library, SearchOption.AllDirectories); } - void ValidateFile (string name, string path) + void ValidateFile (string name, string? path) { if (path == null || !File.Exists (path)) throw new ArgumentException ($"Could not find required file `{name}` within `{HomePath}`; is this a valid JDK?", "homePath");