|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import subprocess |
| 4 | + |
| 5 | + |
| 6 | +def get_sdk_path(): |
| 7 | + """ |
| 8 | + Dynamically retrieve the Android SDK path from environment variables or default locations. |
| 9 | + """ |
| 10 | + # Check if ANDROID_HOME or ANDROID_SDK_ROOT is set |
| 11 | + sdk_path = os.getenv("ANDROID_HOME") or os.getenv("ANDROID_SDK_ROOT") |
| 12 | + |
| 13 | + if not sdk_path: |
| 14 | + # Fallback to default installation paths |
| 15 | + default_paths = [ |
| 16 | + os.path.expanduser(r"~\AppData\Local\Android\Sdk"), # Windows |
| 17 | + os.path.expanduser("~/Library/Android/sdk"), # macOS |
| 18 | + os.path.expanduser("~/.android/sdk"), # Linux |
| 19 | + ] |
| 20 | + for path in default_paths: |
| 21 | + if os.path.exists(path): |
| 22 | + sdk_path = path |
| 23 | + break |
| 24 | + |
| 25 | + if not sdk_path or not os.path.exists(sdk_path): |
| 26 | + sys.exit( |
| 27 | + "Android SDK path not found. Please set ANDROID_HOME or ANDROID_SDK_ROOT." |
| 28 | + ) |
| 29 | + |
| 30 | + return sdk_path |
| 31 | + |
| 32 | + |
| 33 | +def get_emulator_path(): |
| 34 | + """ |
| 35 | + Construct the path to the emulator executable based on the SDK path. |
| 36 | + """ |
| 37 | + sdk_path = get_sdk_path() |
| 38 | + emulator_path = os.path.join( |
| 39 | + sdk_path, "emulator", "emulator.exe" if os.name == "nt" else "emulator" |
| 40 | + ) |
| 41 | + |
| 42 | + if not os.path.exists(emulator_path): |
| 43 | + sys.exit(f"Emulator executable not found at: {emulator_path}") |
| 44 | + |
| 45 | + return emulator_path |
| 46 | + |
| 47 | + |
| 48 | +def get_avds(emulator_path): |
| 49 | + """ |
| 50 | + Retrieve the list of available Android Virtual Devices (AVDs). |
| 51 | + """ |
| 52 | + try: |
| 53 | + result = subprocess.run( |
| 54 | + [emulator_path, "-list-avds"], capture_output=True, text=True, check=True |
| 55 | + ) |
| 56 | + avds = result.stdout.strip().splitlines() |
| 57 | + return avds if avds else [] |
| 58 | + except subprocess.CalledProcessError as e: |
| 59 | + print(f"Error retrieving AVDs: {e}") |
| 60 | + return [] |
| 61 | + |
| 62 | + |
| 63 | +def display_menu(options): |
| 64 | + """ |
| 65 | + Display a selection menu for the user without external modules. |
| 66 | + """ |
| 67 | + print("\nSelect an emulator to launch:") |
| 68 | + for i, option in enumerate(options, 1): |
| 69 | + print(f"{i}. {option}") |
| 70 | + |
| 71 | + while True: |
| 72 | + try: |
| 73 | + choice = int(input("\nEnter your choice (number): ")) - 1 |
| 74 | + if 0 <= choice < len(options): |
| 75 | + return options[choice] |
| 76 | + else: |
| 77 | + print("Invalid choice. Please enter a valid number.") |
| 78 | + except ValueError: |
| 79 | + print("Invalid input. Please enter a number.") |
| 80 | + |
| 81 | + |
| 82 | +def launch_avd(emulator_path, avd_name): |
| 83 | + """ |
| 84 | + Launch the selected Android Virtual Device (AVD). |
| 85 | + """ |
| 86 | + print(f"\nLaunching emulator: {avd_name}...") |
| 87 | + try: |
| 88 | + subprocess.run([emulator_path, "-avd", avd_name], check=True) |
| 89 | + except subprocess.CalledProcessError as e: |
| 90 | + sys.exit(f"Failed to start emulator: {avd_name}. Error: {e}") |
| 91 | + |
| 92 | + |
| 93 | +def main(): |
| 94 | + """ |
| 95 | + Main function to list and launch AVDs. |
| 96 | + """ |
| 97 | + # Get the path to the emulator executable |
| 98 | + emulator_path = get_emulator_path() |
| 99 | + |
| 100 | + # Retrieve the list of available AVDs |
| 101 | + avds = get_avds(emulator_path) |
| 102 | + |
| 103 | + if not avds: |
| 104 | + sys.exit( |
| 105 | + "No Android Virtual Devices found. Please create one using AVD Manager." |
| 106 | + ) |
| 107 | + |
| 108 | + # Display the menu and let the user select an AVD |
| 109 | + avd_name = display_menu(avds) |
| 110 | + |
| 111 | + # Launch the selected AVD |
| 112 | + launch_avd(emulator_path, avd_name) |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + main() |
0 commit comments