Skip to content

Commit 3974fc3

Browse files
authored
[Xamarin.Android.Tools.AndroidSdk] JdkInfo + JDK11 + Windows (#88)
Context: dotnet/android#4567 Commit 36d7fee 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`.
1 parent 5552b07 commit 3974fc3

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

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

+23-22
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
namespace Xamarin.Android.Tools
1515
{
1616
public class JdkInfo {
17-
static readonly string[] JdkLibraryTopDirs = {
18-
"jre",
19-
"lib",
20-
};
2117

2218
public string HomePath {get;}
2319

@@ -58,27 +54,14 @@ public JdkInfo (string homePath)
5854
JavaPath = ProcessUtils.FindExecutablesInDirectory (binPath, "java").FirstOrDefault ();
5955
JavacPath = ProcessUtils.FindExecutablesInDirectory (binPath, "javac").FirstOrDefault ();
6056

61-
string? topDir = null;
62-
foreach (string dir in JdkLibraryTopDirs) {
63-
topDir = Path.Combine (HomePath, dir);
64-
if (!Directory.Exists (topDir)) {
65-
topDir = null;
66-
continue;
67-
}
68-
break;
69-
}
70-
71-
if (String.IsNullOrEmpty (topDir))
72-
topDir = Path.Combine (HomePath, JdkLibraryTopDirs [0]);
73-
74-
JdkJvmPath = OS.IsMac
75-
? FindLibrariesInDirectory (topDir!, "jli").FirstOrDefault ()
76-
: FindLibrariesInDirectory (topDir!, "jvm").FirstOrDefault ();
57+
string? jdkJvmPath = GetJdkJvmPath ();
7758

7859
ValidateFile ("jar", JarPath);
7960
ValidateFile ("java", JavaPath);
8061
ValidateFile ("javac", JavacPath);
81-
ValidateFile ("jvm", JdkJvmPath);
62+
ValidateFile ("jvm", jdkJvmPath);
63+
64+
JdkJvmPath = jdkJvmPath!;
8265

8366
var includes = new List<string> ();
8467
var jdkInclude = Path.Combine (HomePath, "include");
@@ -132,6 +115,24 @@ public bool GetJavaSettingsPropertyValue (string key, [NotNullWhen (true)] out s
132115
return false;
133116
}
134117

118+
string? GetJdkJvmPath ()
119+
{
120+
string jreDir = Path.Combine (HomePath, "jre");
121+
string libDir = Path.Combine (HomePath, "lib");
122+
123+
if (OS.IsMac) {
124+
return FindLibrariesInDirectory (jreDir, "jli").FirstOrDefault () ??
125+
FindLibrariesInDirectory (libDir, "jli").FirstOrDefault ();
126+
}
127+
if (OS.IsWindows) {
128+
string binServerDir = Path.Combine (HomePath, "bin", "server");
129+
return FindLibrariesInDirectory (jreDir, "jvm").FirstOrDefault () ??
130+
FindLibrariesInDirectory (binServerDir, "jvm").FirstOrDefault ();
131+
}
132+
return FindLibrariesInDirectory (jreDir, "jvm").FirstOrDefault () ??
133+
FindLibrariesInDirectory (libDir, "jvm").FirstOrDefault ();
134+
}
135+
135136
static IEnumerable<string> FindLibrariesInDirectory (string dir, string libraryName)
136137
{
137138
if (!Directory.Exists (dir))
@@ -140,7 +141,7 @@ static IEnumerable<string> FindLibrariesInDirectory (string dir, string libraryN
140141
return Directory.EnumerateFiles (dir, library, SearchOption.AllDirectories);
141142
}
142143

143-
void ValidateFile (string name, string path)
144+
void ValidateFile (string name, string? path)
144145
{
145146
if (path == null || !File.Exists (path))
146147
throw new ArgumentException ($"Could not find required file `{name}` within `{HomePath}`; is this a valid JDK?", "homePath");

0 commit comments

Comments
 (0)