def adb_reboot(unlock=True): command.execute("adb", device_serial + ["reboot"]) time.sleep(20) wait_for_device() if unlock: unlock_device() device_status = get_state().strip() return device_status == "device"
def monkey(package_name=None, duration_seconds=30, throttle=1000, log_file=None): # https://developer.android.com/studio/test/monkey seed = 42 ratio_bug_fix = 4 event_count = int(duration_seconds * (1000 / throttle) * ratio_bug_fix) args = device_serial + [ "shell", "monkey", "-v", "-s", seed, # Seed value for pseudo-random number generator ] if package_name is not None: args = args + ["-p", package_name] args = args + [ "--throttle", throttle, # Inserts a fixed delay between events "--ignore-crashes", "--ignore-timeouts", # Application Not Responding "--ignore-security-exceptions", # Permissions error event_count, # Event count ">", log_file ] result = command.execute("adb", args, no_wait=True, display_cmd=True) _display_adb_error(result)
def get_apk_info(root_path, apk_path): aapt_path = _get_aapt_path(root_path) result = command.execute(aapt_path, ["dump", "badging", apk_path]) _display_adb_error(result) info = _get_default_package_info() info['name'] = strings.extract_regex_value( result, r"(?:package:\s*name='([a-zA-Z0-9\.\_\-]*)')") info['path'] = apk_path users = android_users.get_android_users_list(root_path) info['user'] = resolve_user(users, info['user_id']) info['version_code'] = utils.str_to_int( strings.extract_regex_value(result, r"(?:versionCode='([0-9]*)')")) info['version_name'] = strings.extract_regex_value( result, r"(?:versionName='([\s0-9a-zA-Z\-\(\)\.\_\[\]]{1,})')") info['min_sdk'] = utils.str_to_int( strings.extract_regex_value(result, r"(?:sdkVersion:'([0-9]{1,})')")) info['target_sdk'] = utils.str_to_int( strings.extract_regex_value(result, r"(?:targetSdkVersion:'([0-9]{1,})')")) info['multidex'] = _apk_is_multidex(root_path, apk_path) # info['signature'] = get_signature_info(root_path, apk_path) return info
def uninstall(package_name, keep_user_data=False): args = device_serial + ["uninstall"] if keep_user_data: args.append("-k") args.append(package_name) result = command.execute("adb", args, False) _display_adb_error(result) return result.strip()
def pull(source, destination, ignore_error_no_file=False): result = command.execute("adb", device_serial + ["pull", source, destination]) if not ignore_error_no_file or \ (not strings.str_contains(result, "No such file or directory") and not strings.str_contains(result, "does not exist")): _display_adb_error(result) return result
def shell(cmd): data = cmd.split() if data[0] != "shell": data.insert(0, "shell") result = command.execute("adb", device_serial + data) _display_adb_error(result) return result
def start_activity(package_name, activity_name=None): # adb shell am start com.package.name/.MainActivity args = device_serial + ["shell", "am", "start"] if activity_name is not None: args.append(package_name + "/" + activity_name) else: args.append(package_name) result = command.execute("adb", args, False) _display_adb_error(result) return result.strip()
def install(apk_file_path, option_replace=True, option_test=False, option_grant=False): args = device_serial + ["install"] if option_replace: args.append("-r") if option_test: args.append("-t") if option_grant: args.append("-g") args.append(apk_file_path) result = command.execute("adb", args, False) _display_adb_error(result) return result.strip()
def _apk_is_multidex(root_path, apk_path): aapt_path = _get_aapt_path(root_path) result = command.execute(aapt_path, ["list", apk_path]) _display_adb_error(result) if result is None: return False lines = result.split() counter = 0 for line in lines: if line.endswith('.dex'): counter += 1 return counter > 1
def list_devices(details=False): args = ["devices"] if details: args.append("-l") result = command.execute("adb", args) _display_adb_error(result) lines = result.split("\n") devices = [] for line in lines: if "List" not in line and ("device" in line or "offline" in line): data = line.split("\t") identifier = data[0].strip() identifier = identifier.rstrip('\r\n') device = {'identifier': identifier} devices.append(device) return devices
def list_files_pattern(pattern, path='.', output_file=None): # adb shell "find . -name '*.sh'" > output-scripts.txt find_cmd = "find " + path + " -name '" + pattern + "'" args = device_serial + ["shell", find_cmd] if output_file is not None: args.append(">") args.append(output_file) result = command.execute("adb", args, False) _display_adb_error(result) lines = result.split() filtered_lines = [] for line in lines: if not line.endswith("Permission denied"): filtered_lines.append(line) return filtered_lines
def list_files_extension(file_extension, path='.', output_file=None): # adb shell "find . -name '*.sh'" > output-scripts.txt find_cmd = "find " + path + " -name '*." + file_extension + "'" args = device_serial + ["shell", find_cmd] if output_file is not None: args.append(">") args.append(output_file) result = command.execute("adb", args, False, False, True) _display_adb_error(result) lines = result.split("\n") filtered_lines = [] for line in lines: clean_line = line.strip() if not clean_line.endswith( "Permission denied") and clean_line.endswith(file_extension): filtered_lines.append(clean_line) return filtered_lines
def delete_file(path): result = command.execute("adb", ["shell", "rm", path]).strip() # Logs.instance().debug("Result delete = " + result) if result.find("rm: " + path + ":") > -1: Logs.instance().info("Couldn't remove file from device: " + result)
def push(source, destination): result = command.execute("adb", device_serial + ["push", source, destination]) _display_adb_error(result) return result
def get_state(): return command.execute("adb", args=device_serial + ["get-state"])
def wait_for_device(): command.execute("adb", device_serial + ["wait-for-device"])