Skip to content

Commit

Permalink
Get OpenJDK from well-known paths, not just registry (#72)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kdubau authored and jonpryor committed Jun 10, 2019
1 parent 53ffdae commit cb41333
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion nuget.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0
1.1
31 changes: 31 additions & 0 deletions src/Xamarin.Android.Tools.AndroidSdk/Sdks/AndroidSdkWindows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ IEnumerable<JdkInfo> ToJdkInfos (IEnumerable<string> paths, string locator)

return ToJdkInfos (GetPreferredJdkPaths (), "Preferred Registry")
.Concat (ToJdkInfos (GetOpenJdkPaths (), "OpenJDK"))
.Concat (ToJdkInfos (GetKnownOpenJdkPaths (), "Well-known OpenJDK paths"))
.Concat (ToJdkInfos (GetOracleJdkPaths (), "Oracle JDK"));
}

Expand Down Expand Up @@ -163,6 +164,36 @@ private static IEnumerable<string> GetOpenJdkPaths ()
}
}

/// <summary>
/// Locate OpenJDK installations by well known path.
/// </summary>
/// <returns>List of valid OpenJDK paths in version descending order.</returns>
private static IEnumerable<string> GetKnownOpenJdkPaths ()
{
string JdkFolderNamePattern = "microsoft_dist_openjdk_";

var paths = new List<Tuple<string, Version>> ();
var rootPaths = new List<string> {
Path.Combine (Environment.ExpandEnvironmentVariables ("%ProgramW6432%"), "Android", "jdk"),
Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.ProgramFilesX86), "Android", "jdk"),
};

foreach (var rootPath in rootPaths) {
if (Directory.Exists (rootPath)) {
foreach (var directoryName in Directory.EnumerateDirectories (rootPath, $"{JdkFolderNamePattern}*").ToList ()) {
var versionString = directoryName.Replace ($"{rootPath}\\{JdkFolderNamePattern}", string.Empty);
if (Version.TryParse (versionString, out Version ver)) {
paths.Add (new Tuple<string, Version>(directoryName, ver));
}
}
}
}

return paths.OrderByDescending (v => v.Item2)
.Where (openJdk => ProcessUtils.FindExecutablesInDirectory (Path.Combine (openJdk.Item1, "bin"), _JarSigner).Any ())
.Select (openJdk => openJdk.Item1);
}

private static IEnumerable<string> GetOracleJdkPaths ()
{
string subkey = @"SOFTWARE\JavaSoft\Java Development Kit";
Expand Down

0 comments on commit cb41333

Please sign in to comment.