Skip to content

Commit ea3e8d8

Browse files
author
Stewart Miles
committed
Support for Unity 2019 Android SDK and JDK install locations
Unity 2019 adds support for installation of the Android SDK and JDK in the AndroidPlayer directory making it easier for developers to configure the Android build environment. Unfortunately, Unity does not update the JdkPath and AndroidSdkRoot editor preferences when the Android SDK and JDK are installed with the AndroidPlayer. This change searches for the Android SDK and JDK if JdkPath and AndroidSdkRoot are not set. Fixes #188 Bug: 132072681 Change-Id: If01f642ff81570d9c28552fab6957bc88cf4dac1
1 parent 1805c2b commit ea3e8d8

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

source/PlayServicesResolver/src/JavaUtilities.cs

+14-8
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,26 @@ public ToolNotFoundException(string message) : base(message) {}
4848
/// </summary>
4949
private static Version MinimumJdkVersion = new Version("1.8");
5050

51-
/// <summary>
52-
/// Get the JDK path (JAVA_HOME) configured in the Unity editor.
53-
/// </summary>
54-
private static string EditorJavaHome {
55-
get { return UnityEditor.EditorPrefs.GetString("JdkPath"); }
56-
}
57-
5851
/// <summary>
5952
/// Find the JDK path (JAVA_HOME) either configured in the Unity editor or via the JAVA_HOME
6053
/// environment variable.
6154
/// </summary>
6255
private static string JavaHome {
6356
get {
64-
var javaHome = EditorJavaHome;
57+
var javaHome = UnityEditor.EditorPrefs.GetString("JdkPath");
58+
// Unity 2019.x added installation of the JDK in the AndroidPlayer directory
59+
// so fallback to searching for it there.
60+
if (String.IsNullOrEmpty(javaHome) || EditorPrefs.GetBool("JdkUseEmbedded")) {
61+
var androidPlayerDir = PlayServicesResolver.AndroidPlaybackEngineDirectory;
62+
if (!String.IsNullOrEmpty(androidPlayerDir)) {
63+
var platformDir = UnityEngine.Application.platform.ToString().Replace(
64+
"Editor", "").Replace("OSX", "MacOS");
65+
var openJdkDir = Path.Combine(Path.Combine(Path.Combine(
66+
androidPlayerDir, "Tools"), "OpenJDK"), platformDir);
67+
if (Directory.Exists(openJdkDir)) javaHome = openJdkDir;
68+
}
69+
}
70+
// If the JDK stil isn't found, check the environment.
6571
if (String.IsNullOrEmpty(javaHome)) {
6672
javaHome = Environment.GetEnvironmentVariable(JAVA_HOME);
6773
}

source/PlayServicesResolver/src/PlayServicesResolver.cs

+29-1
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,19 @@ public class AndroidAbisChangedArgs : EventArgs {
608608
/// Get the Android SDK directory.
609609
/// </summary>
610610
public static string AndroidSdkRoot {
611-
get { return EditorPrefs.GetString("AndroidSdkRoot"); }
611+
get {
612+
var sdkPath = EditorPrefs.GetString("AndroidSdkRoot");
613+
// Unity 2019.x added installation of the Android SDK in the AndroidPlayer directory
614+
// so fallback to searching for it there.
615+
if (String.IsNullOrEmpty(sdkPath) || EditorPrefs.GetBool("SdkUseEmbedded")) {
616+
var androidPlayerDir = AndroidPlaybackEngineDirectory;
617+
if (!String.IsNullOrEmpty(androidPlayerDir)) {
618+
var androidPlayerSdkDir = Path.Combine(androidPlayerDir, "SDK");
619+
if (Directory.Exists(androidPlayerSdkDir)) sdkPath = androidPlayerSdkDir;
620+
}
621+
}
622+
return sdkPath;
623+
}
612624
}
613625

614626
/// <summary>
@@ -637,6 +649,22 @@ public class AndroidSdkRootChangedArgs : EventArgs {
637649
/// </summary>
638650
public static event EventHandler<AndroidSdkRootChangedArgs> AndroidSdkRootChanged;
639651

652+
/// <summary>
653+
/// Get the Android playback engine directory.
654+
/// </summary>
655+
/// <returns>Get the playback engine directory.</returns>
656+
public static string AndroidPlaybackEngineDirectory {
657+
get {
658+
try {
659+
return (string)VersionHandler.InvokeStaticMethod(
660+
typeof(BuildPipeline), "GetPlaybackEngineDirectory",
661+
new object[] { BuildTarget.Android, BuildOptions.None });
662+
} catch (Exception) {
663+
return null;
664+
}
665+
}
666+
}
667+
640668
/// <summary>
641669
/// Initializes the <see cref="GooglePlayServices.PlayServicesResolver"/> class.
642670
/// </summary>

source/VersionHandler/src/VersionHandler.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,15 @@ public static object InvokeStaticMethod(
494494
public static object InvokeMethod(
495495
Type type, object objectInstance, string methodName,
496496
object[] args, Dictionary<string, object> namedArgs = null) {
497-
MethodInfo method = type.GetMethod(methodName);
497+
Type[] argTypes = null;
498+
if (args != null && args.Length > 0) {
499+
argTypes = new Type[args.Length];
500+
for (int i = 0; i < args.Length; ++i) {
501+
argTypes[i] = args[i].GetType();
502+
}
503+
}
504+
MethodInfo method = argTypes != null ?
505+
type.GetMethod(methodName, argTypes) : type.GetMethod(methodName);
498506
ParameterInfo[] parameters = method.GetParameters();
499507
int numParameters = parameters.Length;
500508
object[] parameterValues = new object[numParameters];

0 commit comments

Comments
 (0)