Skip to content

Commit cb41333

Browse files
kdubaujonpryor
authored andcommitted
Get OpenJDK from well-known paths, not just registry (#72)
Context: http://work.azdo.io/735565 Turns Out™ that certain anti-virus programs block or otherwise mitigate access to various Windows Registry keys. (No idea which anti-virus, or what the symptoms are; this is just What I've Heard.) Reduce reliance on the registry by looking for the JDK within the `%ProgramW6432%\Android\jdk\microsoft_dist_openjdk_*` directories, in which the `*` suffix is a `System.Version`-parsable value, and prefer the highest JDK found there. The Registry value is still preferred, if set.
1 parent 53ffdae commit cb41333

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

nuget.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0
1+
1.1

src/Xamarin.Android.Tools.AndroidSdk/Sdks/AndroidSdkWindows.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ IEnumerable<JdkInfo> ToJdkInfos (IEnumerable<string> paths, string locator)
134134

135135
return ToJdkInfos (GetPreferredJdkPaths (), "Preferred Registry")
136136
.Concat (ToJdkInfos (GetOpenJdkPaths (), "OpenJDK"))
137+
.Concat (ToJdkInfos (GetKnownOpenJdkPaths (), "Well-known OpenJDK paths"))
137138
.Concat (ToJdkInfos (GetOracleJdkPaths (), "Oracle JDK"));
138139
}
139140

@@ -163,6 +164,36 @@ private static IEnumerable<string> GetOpenJdkPaths ()
163164
}
164165
}
165166

167+
/// <summary>
168+
/// Locate OpenJDK installations by well known path.
169+
/// </summary>
170+
/// <returns>List of valid OpenJDK paths in version descending order.</returns>
171+
private static IEnumerable<string> GetKnownOpenJdkPaths ()
172+
{
173+
string JdkFolderNamePattern = "microsoft_dist_openjdk_";
174+
175+
var paths = new List<Tuple<string, Version>> ();
176+
var rootPaths = new List<string> {
177+
Path.Combine (Environment.ExpandEnvironmentVariables ("%ProgramW6432%"), "Android", "jdk"),
178+
Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.ProgramFilesX86), "Android", "jdk"),
179+
};
180+
181+
foreach (var rootPath in rootPaths) {
182+
if (Directory.Exists (rootPath)) {
183+
foreach (var directoryName in Directory.EnumerateDirectories (rootPath, $"{JdkFolderNamePattern}*").ToList ()) {
184+
var versionString = directoryName.Replace ($"{rootPath}\\{JdkFolderNamePattern}", string.Empty);
185+
if (Version.TryParse (versionString, out Version ver)) {
186+
paths.Add (new Tuple<string, Version>(directoryName, ver));
187+
}
188+
}
189+
}
190+
}
191+
192+
return paths.OrderByDescending (v => v.Item2)
193+
.Where (openJdk => ProcessUtils.FindExecutablesInDirectory (Path.Combine (openJdk.Item1, "bin"), _JarSigner).Any ())
194+
.Select (openJdk => openJdk.Item1);
195+
}
196+
166197
private static IEnumerable<string> GetOracleJdkPaths ()
167198
{
168199
string subkey = @"SOFTWARE\JavaSoft\Java Development Kit";

0 commit comments

Comments
 (0)