| """Android device related methods which extend from UI Automator. |
| |
| Android device class is defined in `mobly.controller`. |
| """ |
| |
| from mobly import utils |
| from mobly.controllers import android_device |
| |
| |
| def is_apk_installed( |
| device: android_device.AndroidDevice, package_name: str |
| ) -> bool: |
| """Checks if given package is already installed on the Android device. |
| |
| Args: |
| device: An AndroidDevice object. |
| package_name: The Android app package name. |
| |
| Raises: |
| adb.AdbError: When executing adb command fails. |
| |
| Returns: |
| True if package is installed. False otherwise. |
| """ |
| out = device.adb.shell(['pm', 'list', 'package']) |
| return bool(utils.grep('^package:%s$' % package_name, out)) |
| |
| |
| def install_apk(device: android_device.AndroidDevice, apk_path: str) -> None: |
| """Installs required apk snippet to the given Android device. |
| |
| Args: |
| device: An AndroidDevice object. |
| apk_path: The absolute file path where the apk is located. |
| """ |
| device.adb.install(['-r', '-g', apk_path]) |
| |
| |
| def uninstall_apk( |
| device: android_device.AndroidDevice, package_name: str |
| ) -> None: |
| """Uninstalls required apk snippet on given Android device. |
| |
| Args: |
| device: An AndroidDevice object. |
| package_name: The Android app package name. |
| """ |
| device.adb.uninstall(package_name) |