@@ -30,10 +30,8 @@ public class UnityCompat {
30
30
private const string Namespace = "GooglePlayServices." ;
31
31
private const string ANDROID_MIN_SDK_FALLBACK_KEY = Namespace + "MinSDKVersionFallback" ;
32
32
private const string ANDROID_PLATFORM_FALLBACK_KEY = Namespace + "PlatformVersionFallback" ;
33
- private const string ANDROID_BUILD_TOOLS_FALLBACK_KEY = Namespace + "BuildToolsVersionFallback" ;
34
33
private const int DEFAULT_ANDROID_MIN_SDK = 14 ;
35
34
private const int DEFAULT_PLATFORM_VERSION = 25 ;
36
- private const string DEFAULT_BUILD_TOOLS_VERSION = "25.0.2" ;
37
35
38
36
private const string UNITY_ANDROID_VERSION_ENUM_PREFIX = "AndroidApiLevel" ;
39
37
private const string UNITY_ANDROID_MIN_SDK_VERSION_PROPERTY = "minSdkVersion" ;
@@ -53,14 +51,6 @@ private static int MinSDKVersionFallback {
53
51
private static int AndroidPlatformVersionFallback {
54
52
get { return EditorPrefs . GetInt ( ANDROID_PLATFORM_FALLBACK_KEY , DEFAULT_PLATFORM_VERSION ) ; }
55
53
}
56
- private static string AndroidBuildToolsVersionFallback {
57
- get {
58
- return EditorPrefs . GetString ( ANDROID_BUILD_TOOLS_FALLBACK_KEY ,
59
- DEFAULT_BUILD_TOOLS_VERSION ) ;
60
- }
61
- }
62
-
63
- private const float EPSILON = 1e-7f ;
64
54
65
55
// Parses a UnityEditor.AndroidSDKVersion enum for a value.
66
56
private static int VersionFromAndroidSDKVersionsEnum ( string enumName , string fallbackPrefKey ,
@@ -181,26 +171,26 @@ private static object AndroidJavaToolsInstance {
181
171
182
172
private static Type AndroidSDKToolsClass {
183
173
get {
184
- Type sdkTools = Type . GetType (
185
- UNITY_ANDROID_SDKTOOLS_CLASS + ", " + UNITY_ANDROID_EXTENSION_ASSEMBLY ) ;
186
- if ( sdkTools == null ) {
187
- Debug . LogError ( "Could not find the " +
188
- UNITY_ANDROID_SDKTOOLS_CLASS + " class via reflection. " + WRITE_A_BUG ) ;
189
- return null ;
190
- }
191
- return sdkTools ;
174
+ return Type . GetType ( UNITY_ANDROID_SDKTOOLS_CLASS + ", " +
175
+ UNITY_ANDROID_EXTENSION_ASSEMBLY ) ;
192
176
}
193
177
}
194
178
195
179
private static object AndroidSDKToolsInstance {
196
180
get {
181
+ var androidSdkToolsClass = AndroidSDKToolsClass ;
182
+ if ( androidSdkToolsClass == null ) {
183
+ Debug . LogError ( String . Format ( "{0} class does not exist. {1}" ,
184
+ UNITY_ANDROID_SDKTOOLS_CLASS , WRITE_A_BUG ) ) ;
185
+ return null ;
186
+ }
197
187
try {
198
- return VersionHandler . InvokeStaticMethod ( AndroidSDKToolsClass , "GetInstance" , null ) ;
188
+ return VersionHandler . InvokeStaticMethod ( androidSdkToolsClass , "GetInstance" , null ) ;
199
189
} catch ( Exception ) {
200
190
}
201
191
// Unity 2018+ requires the AndroidJavaTools instance to get the SDK tools instance.
202
192
try {
203
- return VersionHandler . InvokeStaticMethod ( AndroidSDKToolsClass , "GetInstance" ,
193
+ return VersionHandler . InvokeStaticMethod ( androidSdkToolsClass , "GetInstance" ,
204
194
new object [ ] { AndroidJavaToolsInstance } ) ;
205
195
} catch ( Exception e ) {
206
196
Debug . LogError ( String . Format ( "Failed while calling {0}.GetInstance() ({1}). {2}" ,
@@ -243,11 +233,23 @@ private static object PostProcessAndroidPlayerInstance {
243
233
}
244
234
}
245
235
236
+ // Display a warning and get the fallback Android SDK version.
237
+ private static int WarnOnAndroidSdkFallbackVersion ( ) {
238
+ int version = AndroidPlatformVersionFallback ;
239
+ Debug . LogWarning ( String . Format (
240
+ "Failed to determine the most recently installed Android SDK version. {0} " +
241
+ "Resorting to reading a fallback value from the editor preferences {1}: {2}" ,
242
+ WRITE_A_BUG , ANDROID_PLATFORM_FALLBACK_KEY , version ) ) ;
243
+ return version ;
244
+ }
245
+
246
246
// Gets the latest SDK version that's currently installed.
247
247
// This is required for generating gradle builds.
248
248
public static int FindNewestInstalledAndroidSDKVersion ( ) {
249
249
var androidSdkToolsClass = AndroidSDKToolsClass ;
250
250
if ( androidSdkToolsClass != null ) {
251
+ var androidSdkToolsInstance = AndroidSDKToolsInstance ;
252
+ if ( androidSdkToolsInstance == null ) return WarnOnAndroidSdkFallbackVersion ( ) ;
251
253
// Unity 2019+ only has a method to list the installed targets, so we need to parse
252
254
// the returned list to determine the newest Android SDK version.
253
255
var listTargetPlatforms = androidSdkToolsClass . GetMethod (
@@ -259,14 +261,14 @@ public static int FindNewestInstalledAndroidSDKVersion() {
259
261
try {
260
262
// Unity 2019+
261
263
platformStrings = ( IEnumerable < string > ) listTargetPlatforms . Invoke (
262
- AndroidSDKToolsInstance , null ) ;
264
+ androidSdkToolsInstance , null ) ;
263
265
} catch ( Exception ) {
264
266
}
265
267
if ( platformStrings != null ) {
266
268
try {
267
269
// Unity 2018+
268
270
platformStrings = ( IEnumerable < string > ) listTargetPlatforms . Invoke (
269
- AndroidSDKToolsInstance , new object [ ] { AndroidJavaToolsInstance } ) ;
271
+ androidSdkToolsInstance , new object [ ] { AndroidJavaToolsInstance } ) ;
270
272
} catch ( Exception ) {
271
273
}
272
274
}
@@ -292,7 +294,7 @@ public static int FindNewestInstalledAndroidSDKVersion() {
292
294
androidSdkToolsClass . GetMethod ( "GetTopAndroidPlatformAvailable" ) ;
293
295
if ( getTopAndroidPlatformAvailable != null ) {
294
296
return ( int ) getTopAndroidPlatformAvailable . Invoke (
295
- AndroidSDKToolsInstance , BindingFlags . NonPublic , null ,
297
+ androidSdkToolsInstance , BindingFlags . NonPublic , null ,
296
298
new object [ ] { null } , null ) ;
297
299
}
298
300
}
@@ -309,36 +311,7 @@ public static int FindNewestInstalledAndroidSDKVersion() {
309
311
PostProcessAndroidPlayerInstance , BindingFlags . NonPublic , null ,
310
312
new object [ ] { } , null ) ;
311
313
}
312
-
313
- Debug . LogError ( String . Format (
314
- "Failed to determine the most recently installed Android SDK version. {0} " +
315
- "Resorting to reading a fallback value from the editor preferences {1}: {2}" ,
316
- WRITE_A_BUG , ANDROID_PLATFORM_FALLBACK_KEY , AndroidPlatformVersionFallback ) ) ;
317
-
318
- return AndroidPlatformVersionFallback ;
319
- }
320
-
321
- // Finds the latest build tools version that's currently installed.
322
- // This is required for generating gradle builds.
323
- [ Obsolete ]
324
- public static string GetAndroidBuildToolsVersion ( ) {
325
- // TODO: We're using reflection to access Unity's SDK helpers to get at this info
326
- // but since this is likely fragile, and may not even be consistent across versions
327
- // of Unity, we'll need to replace it with our own.
328
- MethodInfo buildToolsVersionMethod = null ;
329
- Type sdkClass = AndroidSDKToolsClass ;
330
- if ( sdkClass != null )
331
- buildToolsVersionMethod = sdkClass . GetMethod ( "BuildToolsVersion" ) ;
332
- if ( buildToolsVersionMethod != null ) {
333
- return ( string ) buildToolsVersionMethod . Invoke ( AndroidSDKToolsInstance ,
334
- BindingFlags . NonPublic , null , new object [ ] { null } , null ) ;
335
- }
336
-
337
- Debug . LogError ( "Could not find the " + UNITY_ANDROID_SDKTOOLS_CLASS +
338
- ".BuildToolsVersion method via reflection. " + WRITE_A_BUG +
339
- " Resorting to reading a fallback value from the editor preferences " +
340
- ANDROID_BUILD_TOOLS_FALLBACK_KEY + ": " + AndroidBuildToolsVersionFallback ) ;
341
- return AndroidBuildToolsVersionFallback ;
314
+ return WarnOnAndroidSdkFallbackVersion ( ) ;
342
315
}
343
316
344
317
/// <summary>
0 commit comments