From 4ea031507a0b237bc2aca5bc648b716b1b51fb35 Mon Sep 17 00:00:00 2001 From: Frida Schlaug Date: Mon, 31 Aug 2020 21:03:57 +0200 Subject: [PATCH 1/2] Fixes #322, Use the raw property value when the enum does not contain this value --- source/AndroidResolver/src/UnityCompat.cs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/source/AndroidResolver/src/UnityCompat.cs b/source/AndroidResolver/src/UnityCompat.cs index 0027af7c..70b31ca1 100644 --- a/source/AndroidResolver/src/UnityCompat.cs +++ b/source/AndroidResolver/src/UnityCompat.cs @@ -55,11 +55,22 @@ private static int AndroidPlatformVersionFallback { } // Parses a UnityEditor.AndroidSDKVersion enum for a value. - private static int VersionFromAndroidSDKVersionsEnum(string enumName, string fallbackPrefKey, + private static int VersionFromAndroidSDKVersionsEnum(object enumValue, string fallbackPrefKey, int fallbackValue) { - // If the enum property has no name it's not possible to parse the version so fallback to - // auto-selection. - if (String.IsNullOrEmpty(enumName)) return -1; + string enumName = null; + try { + enumName = Enum.GetName(typeof(AndroidSdkVersions), enumValue); + } + catch (ArgumentException) + { + //Fall back on auto if the enum value is not parsable + return -1; + } + + // If the enumName is empty then enumValue was not represented in the enum, + // most likely because Unity has not yet added the new version, + // fall back on the raw enumValue + if (String.IsNullOrEmpty(enumName)) return (int)enumValue; if (enumName.StartsWith(UNITY_ANDROID_VERSION_ENUM_PREFIX)) { enumName = enumName.Substring(UNITY_ANDROID_VERSION_ENUM_PREFIX.Length); @@ -91,7 +102,7 @@ private static int VersionFromAndroidSDKVersionsEnum(string enumName, string fal /// the sdk value (ie. 24 for Android 7.0 Nouget) public static int GetAndroidMinSDKVersion() { int minSdkVersion = VersionFromAndroidSDKVersionsEnum( - PlayerSettings.Android.minSdkVersion.ToString(), + (object)PlayerSettings.Android.minSdkVersion, ANDROID_MIN_SDK_FALLBACK_KEY, MinSDKVersionFallback); if (minSdkVersion == -1) return MinSDKVersionFallback; @@ -121,7 +132,7 @@ public static int GetAndroidTargetSDKVersion() { var property = typeof(UnityEditor.PlayerSettings.Android).GetProperty("targetSdkVersion"); int apiLevel = property == null ? -1 : VersionFromAndroidSDKVersionsEnum( - Enum.GetName(property.PropertyType, property.GetValue(null, null)), + property.GetValue(null, null), ANDROID_PLATFORM_FALLBACK_KEY, AndroidPlatformVersionFallback); if (apiLevel >= 0) return apiLevel; return FindNewestInstalledAndroidSDKVersion(); From 2c64e520fb36c5fa5496ece8316fe933d490b8d3 Mon Sep 17 00:00:00 2001 From: Frida Schlaug Date: Wed, 9 Sep 2020 10:39:57 +0200 Subject: [PATCH 2/2] Update source/AndroidResolver/src/UnityCompat.cs Co-authored-by: chkuang-g <31869252+chkuang-g@users.noreply.github.com> --- source/AndroidResolver/src/UnityCompat.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/source/AndroidResolver/src/UnityCompat.cs b/source/AndroidResolver/src/UnityCompat.cs index 70b31ca1..f8549bde 100644 --- a/source/AndroidResolver/src/UnityCompat.cs +++ b/source/AndroidResolver/src/UnityCompat.cs @@ -60,9 +60,7 @@ private static int VersionFromAndroidSDKVersionsEnum(object enumValue, string fa string enumName = null; try { enumName = Enum.GetName(typeof(AndroidSdkVersions), enumValue); - } - catch (ArgumentException) - { + } catch (ArgumentException) { //Fall back on auto if the enum value is not parsable return -1; }