Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions src/Xamarin.Android.Tools.AndroidSdk/JdkInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
namespace Xamarin.Android.Tools
{
public class JdkInfo {
static readonly string[] JdkLibraryTopDirs = {
"jre",
"lib",
};

public string HomePath {get;}

Expand Down Expand Up @@ -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<string> ();
var jdkInclude = Path.Combine (HomePath, "include");
Expand Down Expand Up @@ -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<string> FindLibrariesInDirectory (string dir, string libraryName)
{
if (!Directory.Exists (dir))
Expand All @@ -140,7 +141,7 @@ static IEnumerable<string> 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");
Expand Down