discovery.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. # Utility functions to discover system settings like Android SDK location, etc.
  8. import os
  9. import platform
  10. def could_be_android_sdk_directory(sdk_path: str) -> bool:
  11. """
  12. @returns True if the provided @sdk_path could be an Android SDK directory.
  13. Will check for existence of a few directories that are typically located
  14. inside an Android SDK directory.
  15. """
  16. if not os.path.isdir(sdk_path):
  17. return False
  18. test_dir = os.path.join(sdk_path, "ndk")
  19. if os.path.isdir(test_dir):
  20. return True
  21. test_dir = os.path.join(sdk_path, "platform-tools")
  22. if os.path.isdir(test_dir):
  23. return True
  24. return False
  25. def discover_android_sdk_path() -> str:
  26. """
  27. Follows common sense heuristic to find the location of the Android SDK.
  28. @returns absolute path as a string to the Android SDK directory.
  29. """
  30. var = os.getenv('ANDROID_HOME')
  31. if var and os.path.isdir(var):
  32. return var
  33. var = os.getenv('ANDROID_SDK_ROOT')
  34. if var and os.path.isdir(var):
  35. return var
  36. user_home_dir = os.path.expanduser("~")
  37. if platform.system() == "Windows":
  38. sdk_path = f"{user_home_dir}\\AppData\\Local\\Android\\Sdk"
  39. if os.path.isdir(sdk_path):
  40. return sdk_path
  41. sdk_system_path = "C:\\Program Files (x86)\\Android\\android-sdk"
  42. if os.path.isdir(sdk_system_path):
  43. return sdk_system_path
  44. else:
  45. # Linux or MacOS
  46. sdk_path = f"{user_home_dir}/Library/Android/sdk"
  47. if os.path.isdir(sdk_path):
  48. return sdk_path
  49. return ""