Python代码实现,adb相关操作封装成的工具类,包含中文注释

Python代码实现,adb相关操作封装成的工具类,包含中文注释

方案1

import os

class ADBTool:
    def __init__(self, device_id=None):
        self.device_id = device_id

    def adb(self, *args):
        """
        执行adb命令
        """
        command = ["adb"]
        if self.device_id:
            command.extend(["-s", self.device_id])
        command.extend(args)
        return os.popen(" ".join(command)).read().strip()

    def get_devices(self):
        """
        获取已连接的设备列表
        """
        devices = self.adb("devices").split("\n")[1:]
        devices = [d.split("\t")[0] for d in devices if d.strip()]
        return devices

    def get_screen_size(self):
        """
        获取屏幕分辨率
        """
        size_str = self.adb("shell", "wm", "size")
        size = size_str.split("Physical size: ")[-1].strip()
        return tuple(map(int, size.split("x")))

    def get_top_activity(self):
        """
        获取当前顶层Activity
        """
        activity = self.adb("shell", "dumpsys", "window", "windows")
        activity = [a for a in activity.split("\n") if "mFocusedApp" in a]
        if activity:
            activity = activity[0].split()[-1].split("/")
            return activity[0], activity[1]
        return None, None

    def take_screenshot(self, filename):
        """
        截屏
        """
        self.adb("shell", "screencap", "-p", "/sdcard/screenshot.png")
        self.adb("pull", "/sdcard/screenshot.png", filename)

    def start_app(self, package, activity):
        """
        启动应用
        """
        self.adb("shell", "am", "start", "-n", f"{package}/{activity}")

    def stop_app(self, package):
        """
        停止应用
        """
        self.adb("shell", "am", "force-stop", package)

    def clear_app_data(self, package):
        """
        清除应用数据
        """
        self.adb("shell", "pm", "clear", package)

    def install_app(self, apk_path):
        """
        安装应用
        """
        self.adb("install", "-r", apk_path)

    def uninstall_app(self, package):
        """
        卸载应用
        """
        self.adb("uninstall", package)

以上是一个简单的ADB工具类,可以通过实例化该类来执行各种ADB命令。例如:

adb = ADBTool()
devices = adb.get_devices()
print(devices)

adb.device_id = devices[0]
size = adb.get_screen_size()
print(size)

package = "com.example.app"
activity = ".MainActivity"
adb.start_app(package, activity)

adb.take_screenshot("screenshot.png")

top_package, top_activity = adb.get_top_activity()
print(top_package, top_activity)

adb.stop_app(package)
adb.clear_app_data(package)

apk_path = "app.apk"
adb.install_app(apk_path)

adb.uninstall_app(package)

以上代码演示了如何使用ADB工具类执行一些常见的操作,例如获取设备列表、获取屏幕分辨率、启动应用程序、截屏、获取当前顶层Activity等等。有了这个工具类,我们可以更方便地进行Android设备测试和调试。

方案2



#但是,以下是一份adb相关操作封装成的工具类(Python)的框架,你可以根据需要对其进行修改和优化。

```
import os
import subprocess
import re
import time


class ADB:

    def __init__(self, device=None):
        self.device = device

    def runCmd(self, cmd):
        """
        执行adb命令并返回结果
        """
        if self.device is not None:
            cmd = 'adb -s %s %s' % (self.device, cmd)
        else:
            cmd = 'adb %s' % cmd

        p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        text = p.stdout.read()
        p.wait()
        return text

    def runCmdNoResult(self, cmd):
        """
        执行adb命令但不返回结果
        """
        if self.device is not None:
            cmd = 'adb -s %s %s' % (self.device, cmd)
        else:
            cmd = 'adb %s' % cmd

        os.system(cmd)

    def getDevices(self):
        """
        获取设备列表
        """
        result = []
        text = self.runCmd('devices')
        for line in text.split(b'\r\n'):
            line = line.strip()
            if line.endswith(b'device'):
                device = line[:-len(b'device')].strip()
                result.append(device)
        return result

    def getFocusedPackageAndActivity(self):
        """
        获取当前焦点应用的包名和Activity
        """
        pattern = re.compile(r"[a-zA-Z0-9\.]+/[a-zA-Z0-9\.]+")
        out = self.runCmd('shell dumpsys window windows | grep -E "mCurrentFocus"')
        match = pattern.search(out)
        if match is not None:
            return match.group()
        else:
            return ""

    def getCurrentPackageName(self):
        """
        获取当前应用的包名
        """
        return self.getFocusedPackageAndActivity().split('/')[0]

    def getCurrentActivity(self):
        """
        获取当前应用的Activity
        """
        return self.getFocusedPackageAndActivity().split('/')[1]

    def startActivity(self, component):
        """
        启动指定包名和Activity的组件
        """
        self.runCmd('shell am start -n %s' % component)

    def startActivityByPackageName(self, packageName):
        """
        启动指定包名的应用
        """
        self.runCmd('shell monkey -p %s -c android.intent.category.LAUNCHER 1' % packageName)

    def stopPackage(self, packageName):
        """
        停止指定包名的应用
        """
        self.runCmd('shell am force-stop %s' % packageName)

    def clearPackageData(self, packageName):
        """
        清除指定包名的应用数据
        """
        self.runCmd('shell pm clear %s' % packageName)

    def installApp(self, apkPath):
        """
        安装应用
        """
        self.runCmdNoResult('install -r %s' % apkPath)

    def uninstallApp(self, packageName):
        """
        卸载应用
        """
        self.runCmdNoResult('uninstall %s' % packageName)

    def isScreenOn(self):
        """
        判断屏幕是否亮着
        """
        out = self.runCmd('shell dumpsys power')
        if 'Display Power: state=ON' in str(out):
            return True
        else:
            return False

    def turnScreenOn(self):
        """
        唤醒屏幕
        """
        self.runCmdNoResult('shell input keyevent 26')

    def turnScreenOff(self):
        """
        关闭屏幕
        """
        self.runCmdNoResult('shell input keyevent 26')

    def press(self, keyCode):
        """
        模拟物理按键
        """
        self.runCmdNoResult('shell input keyevent %d' % keyCode)

    def longPress(self, keyCode):
        """
        模拟物理按键长按
        """
        self.runCmdNoResult('shell input keyevent --longpress %d' % keyCode)

    def swipe(self, startX, startY, endX, endY, duration):
        """
        模拟滑屏动作
        """
        self.runCmdNoResult('shell input swipe %d %d %d %d %d' % (startX, startY, endX, endY, duration))

    def screenshot(self, filename=None):
        """
        截屏并保存图片
        """
        if filename is None:
            filename = '%d.png' % int(time.time())

        if self.device is not None:
            self.runCmdNoResult('shell screencap -p /sdcard/tmp.png')
            self.runCmdNoResult('pull /sdcard/tmp.png %s' % filename)
            self.runCmdNoResult('shell rm /sdcard/tmp.png')
        else:
            self.runCmdNoResult('screencap -p %s' % filename)

    def pullFile(self, remote, local):
        """
        从设备中拉取文件
        """
        if self.device is not None:
            self.runCmd('pull %s %s' % (remote, local))

    def pushFile(self, local, remote):
        """
        将文件推送至设备
        """
        if self.device is not None:
            self.runCmd('push %s %s' % (local, remote))


if __name__ == '__main__':
    # 以下为使用示例
    adb = ADB()
    devices = adb.getDevices()
    if len(devices) > 0:
        adb.device = devices[0]

    packageName = adb.getCurrentPackageName()
    print('当前应用:', packageName)

    apkPath = '/path/to/apk'
    adb.installApp(apkPath)
    adb.startActivityByPackageName('com.example.myapplication')
    time.sleep(5)
    adb.stopPackage('com.example.myapplication')
    adb.uninstallApp('com.example.myapplication')

    adb.screenshot()
    adb.pullFile('/sdcard/tmp.png', './tmp.png')
```

方案3

class AdbShell:
"""
该类封装了安卓adb shell命令,并提供了200个实例方法供调用。
"""

def __init__(self, device_id=None):
    """
    初始化方法,可传入设备ID,若不传入则默认使用第一个连接的设备。
    """
    self.device_id = device_id

def adb_shell(self, command):
    """
    执行adb shell命令的方法。
    """
    if self.device_id:
        cmd = f"adb -s {self.device_id} shell {command}"
    else:
        cmd = f"adb shell {command}"
    return os.popen(cmd).read().strip()

def get_device_id(self):
    """
    获取设备ID的方法。
    """
    return self.adb_shell("getprop ro.serialno")

def get_android_version(self):
    """
    获取安卓系统版本号的方法。
    """
    return self.adb_shell("getprop ro.build.version.release")

def get_sdk_version(self):
    """
    获取安卓SDK版本号的方法。
    """
    return self.adb_shell("getprop ro.build.version.sdk")

def get_device_model(self):
    """
    获取设备型号的方法。
    """
    return self.adb_shell("getprop ro.product.model")

def get_device_brand(self):
    """
    获取设备品牌的方法。
    """
    return self.adb_shell("getprop ro.product.brand")

def get_device_name(self):
    """
    获取设备名称的方法。
    """
    return self.adb_shell("getprop ro.product.name")

def get_device_manufacturer(self):
    """
    获取设备制造商的方法。
    """
    return self.adb_shell("getprop ro.product.manufacturer")

def get_cpu_abi(self):
    """
    获取CPU架构的方法。
    """
    return self.adb_shell("getprop ro.product.cpu.abi")

def get_cpu_abi2(self):
    """
    获取CPU架构2的方法。
    """
    return self.adb_shell("getprop ro.product.cpu.abi2")

def get_screen_density(self):
    """
    获取屏幕密度的方法。
    """
    return self.adb_shell("wm density")

def get_screen_resolution(self):
    """
    获取屏幕分辨率的方法。
    """
    return self.adb_shell("wm size")

def get_display_density(self):
    """
    获取显示密度的方法。
    """
    return self.adb_shell("getprop ro.sf.lcd_density")

def get_wifi_mac_address(self):
    """
    获取WiFi MAC地址的方法。
    """
    return self.adb_shell("cat /sys/class/net/wlan0/address")

def get_ip_address(self):
    """
    获取IP地址的方法。
    """
    return self.adb_shell("ifconfig | grep inet | grep -v inet6 | awk '{print $2}'")

def get_battery_level(self):
    """
    获取电池电量的方法。
    """
    return self.adb_shell("dumpsys battery | grep level | awk '{print $2}'")

def get_battery_status(self):
    """
    获取电池状态的方法。
    """
    return self.adb_shell("dumpsys battery | grep status | awk '{print $2}'")

def get_battery_health(self):
    """
    获取电池健康状况的方法。
    """
    return self.adb_shell("dumpsys battery | grep health | awk '{print $2}'")

def get_battery_temperature(self):
    """
    获取电池温度的方法。
    """
    return self.adb_shell("dumpsys battery | grep temperature | awk '{print $2}'")

def get_battery_technology(self):
    """
    获取电池技术的方法。
    """
    return self.adb_shell("dumpsys battery | grep technology | awk '{print $2}'")

def get_installed_packages(self):
    """
    获取已安装应用包名的方法。
    """
    return self.adb_shell("pm list packages")

def get_running_packages(self):
    """
    获取正在运行的应用包名的方法。
    """
    return self.adb_shell("dumpsys activity | grep -i run | awk '{print $5}'")

def get_device_orientation(self):
    """
    获取设备方向的方法。
    """
    return self.adb_shell("dumpsys input | grep 'SurfaceOrientation' | awk '{print $2}'")

def get_current_activity(self):
    """
    获取当前Activity的方法。
    """
    return self.adb_shell("dumpsys activity | grep 'mFocusedActivity' | awk '{print $4}' | cut -d '/' -f2")

def get_current_package(self):
    """
    获取当前应用包名的方法。
    """
    return self.adb_shell("dumpsys activity | grep 'mFocusedActivity' | awk '{print $4}' | cut -d '/' -f1")

def get_current_window(self):
    """
    获取当前窗口的方法。
    """
    return self.adb_shell("dumpsys window windows | grep 'mCurrentFocus' | awk '{print $3}' | cut -d '}' -f1")

def get_system_property(self, key):
    """
    获取系统属性的方法。
    """
    return self.adb_shell(f"getprop {key}")

def set_system_property(self, key, value):
    """
    设置系统属性的方法。
    """
    return self.adb_shell(f"setprop {key} {value}")

def get_system_properties(self):
    """
    获取所有系统属性的方法。
    """
    return self.adb_shell("getprop")

def get_running_services(self):
    """
    获取正在运行的服务的方法。
    """
    return self.adb_shell("dumpsys activity services")

def get_running_processes(self):
    """
    获取正在运行的进程的方法。
    """
    return self.adb_shell("ps")

def get_running_threads(self, pid):
    """
    获取指定进程ID的所有线程的方法。
    """
    return self.adb_shell(f"ps -T -p {pid}")

def get_cpu_usage(self):
    """
    获取CPU使用率的方法。
    """
    return self.adb_shell("top -n 1 -d 1 | grep 'CPU'")

def get_memory_usage(self):
    """
    获取内存使用情况的方法。
    """
    return self.adb_shell("dumpsys meminfo")

def get_network_usage(self):
    """
    获取网络使用情况的方法。
    """
    return self.adb_shell("cat /proc/net/xt_qtaguid/stats")

def get_battery_usage(self):
    """
    获取电池使用情况的方法。
    """
    return self.adb_shell("dumpsys batterystats")

def get_fingerprint(self):
    """
    获取设备指纹信息的方法。
    """
    return self.adb_shell("getprop ro.build.fingerprint")

def get_build_date(self):
    """
    获取系统构建日期的方法。
    """
    return self.adb_shell("getprop ro.build.date")

def get_build_id(self):
    """
    获取系统构建ID的方法。
    """
    return self.adb_shell("getprop ro.build.id")

def get_build_version(self):
    """
    获取系统构建版本号的方法。
    """
    return self.adb_shell("getprop ro.build.version.incremental")

def get_build_type(self):
    """
    获取系统构建类型的方法。
    """
    return self.adb_shell("getprop ro.build.type")

def get_build_tags(self):
    """
    获取系统构建标签的方法。
    """
    return self.adb_shell("getprop ro.build.tags")

def get_build_user(self):
    """
    获取系统构建用户的方法。
    """
    return self.adb_shell("getprop ro.build.user")

def get_build_host(self):
    """
    获取系统构建主机名的方法。
    """
    return self.adb_shell("getprop ro.build.host")

def get_build_flavor(self):
    """
    获取系统构建Flavor的方法。
    """
    return self.adb_shell("getprop ro.build.flavor")

def get_build_board(self):
    """
    获取系统构建主板名称的方法。
    """
    return self.adb_shell("getprop ro.product.board")

def get_build_bootloader(self):
    """
    获取系统构建引导程序版本的方法。
    """
    return self.adb_shell("getprop ro.bootloader")

def get_build_radio(self):
    """
    获取系统构建无线电固件版本的方法。
    """
    return self.adb_shell("getprop ro.build.expect.baseband")

def get_build_codename(self):
    """
    获取系统构建代号的方法。
    """
    return self.adb_shell("getprop ro.build.version.codename")

def get_build_sdk(self):
    """
    获取系统构建SDK版本号的方法。
    """
    return self.adb_shell("getprop ro.build.version.sdk")

def get_build_release(self):
    """
    获取系统构建发行版的方法。
    """
    return self.adb_shell("getprop ro.build.version.release")

def get_build_incremental(self):
    """
    获取系统构建增量版本号的方法。
    """
    return self.adb_shell("getprop ro.build.version.incremental")

def get_build_description(self):
    """
    获取系统构建描述信息的方法。
    """
    return self.adb_shell("getprop ro.build.description")

def get_build_display_id(self):
    """
    获取系统构建显示ID的方法。
    """
    return self.adb_shell("getprop ro.build.display.id")

def get_build_security_patch(self):
    """
    获取系统构建安全补丁程序级别的方法。
    """
    return self.adb_shell("getprop ro.build.version.security_patch")

def get_build_abi(self):
    """
    获取系统构建ABI的方法。
    """
    return self.adb_shell("getprop ro.product.cpu.abi")

def get_build_abi2(self):
    """
    获取系统构建ABI2的方法。
    """
    return self.adb_shell("getprop ro.product.cpu.abi2")

def get_build_features(self):
    """
    获取系统构建特性的方法。
    """
    return self.adb_shell("getprop ro.build.features")

def get_build_characteristics(self):
    """
    获取系统构建特点的方法。
    """
    return self.adb_shell("getprop ro.build.characteristics")

def get_build_app_id(self):
    """
    获取系统构建应用ID的方法。
    """
    return self.adb_shell("getprop ro.build.app.id")

def get_build_app_name(self):
    """
    获取系统构建应用名称的方法。
    """
    return self.adb_shell("getprop ro.build.app.name")

def get_build_app_version(self):
    """
    获取系统构建应用版本号的方法。
    """
    return self.adb_shell("getprop ro.build.app.version")

def get_build_app_description(self):
    """
    获取系统构建应用描述信息的方法。
    """
    return self.adb_shell("getprop ro.build.app.description")

def get_build_app_author(self):
    """
    获取系统构建应用作者信息的方法。
    """
    return self.adb_shell("getprop ro.build.app.author")

def get_build_app_company(self):
    """
    获取系统构建应用公司信息的方法。
    """
    return self.adb_shell("getprop ro.build.app.company")

def get_build_app_email(self):
    """
    获取系统构建应用电子邮件地址的方法。
    """
    return self.adb_shell("getprop ro.build.app.email")

def get_build_app_website(self):
    """
    获取系统构建应用网站地址的方法。
    """
    return self.adb_shell("getprop ro.build.app.website")

def get_build_app_license(self):
    """
    获取系统构建应用许可证信息的方法。
    """
    return self.adb_shell("getprop ro.build.app.license")

def get_build_app_icon(self):
    """
    获取系统构建应用图标的方法。
    """
    return self.adb_shell("getprop ro.build.app.icon")

def get_build_app_min_sdk(self):
    """
    获取系统构建应用最小SDK版本号的方法。
    """
    return self.adb_shell("getprop ro.build.app.min_sdk")

def get_build_app_target_sdk(self):
    """
    获取系统构建应用目标SDK版本号的方法。
    """
    return self.adb_shell("getprop ro.build.app.target_sdk")

def get_build_app_permissions(self):
    """
    获取系统构建应用权限列表的方法。
    """
    return self.adb_shell("getprop ro.build.app.permissions")

def get_build_app_features(self):
    """
    获取系统构建应用特性列表的方法。
    """
    return self.adb_shell("getprop ro.build.app.features")

def get_build_app_signatures(self):
    """
    获取系统构建应用签名信息的方法。
    """
    return self.adb_shell("getprop ro.build.app.signatures")

def get_build_app_capabilities(self):
    """
    获取系统构建应用功能列表的方法。
    """
    return self.adb_shell("getprop ro.build.app.capabilities")

def get_build_app_activities(self):
    """
    获取系统构建应用Activity列表的方法。
    """
    return self.adb_shell("getprop ro.build.app.activities")

def get_build_app_services(self):
    """
    获取系统构建应用服务列表的方法。
    """
    return self.adb_shell("getprop ro.build.app.services")

def get_build_app_receivers(self):
    """
    获取系统构建应用广播接收器列表的方法。
    """
    return self.adb_shell("getprop ro.build.app.receivers")

def get_build_app_providers(self):
    """
    获取系统构建应用内容提供者列表的方法。
    """
    return self.adb_shell("getprop ro.build.app.providers")

def get_build_app_permissions_group(self):
    """
    获取系统构建应用权限组列表的方法。
    """
    return self.adb_shell("getprop ro.build.app.permissions_group")

def get_build_app_instrumentation(self):
    """
    获取系统构建应用测试信息的方法。
    """
    return self.adb_shell("getprop ro.build.app.instrumentation")

def get_build_app_libraries(self):
    """
    获取系统构建应用库列表的方法。
    """
    return self.adb_shell("getprop ro.build.app.libraries")

def get_build_app_native_libraries(self):
    """
    获取系统构建应用本地库列表的方法。
    """
    return self.adb_shell("getprop ro.build.app.native_libraries")

def get_build_app_features_required(self):
    """
    获取系统构建应用所需特性列表的方法。
    """
    return self.adb_shell("getprop ro.build.app.features_required")

def get_build_app_features_available(self):
    """
    获取系统构建应用可用特性列表的方法。
    """
    return self.adb_shell("getprop ro.build.app.features_available")

def get_build_app_features_unavailable(self):
    """
    获取系统构建应用不可用特性列表的方法。
    """
    return self.adb_shell("getprop ro.build.app.features_unavailable")

def get_build_app_configurations(self):
    """
    获取系统构建应用配置列表的方法。
    """
    return self.adb_shell("getprop ro.build.app.configurations")

def get_build_app_resources(self):
    """
    获取系统构建应用资源列表的方法。
    """
    return self.adb_shell("getprop ro.build.app.resources")

def get_build_app_assets(self):
    """
    获取系统构建应用资源列表的方法。
    """
    return self.adb_shell("getprop ro.build.app.assets")

def get_build_app_manifest(self):
    """
    获取系统构建应用清单文件的方法。
    """
    return self.adb_shell("getprop ro.build.app.manifest")

def get_build_app_directory(self):
    """
    获取系统构建应用目录的方法。
    """
    return self.adb_shell("getprop ro.build.app.dir")

def get_build_app_files_directory(self):
    """
    获取系统构建应用文件目录的方法。
    """
    return self.adb_shell("getprop ro.build.app.files_dir")

def get_build_app_cache_directory(self):
    """
    获取系统构建应用缓存目录的方法。
    """
    return self.adb_shell("getprop ro.build.app.cache_dir")

def get_build_app_code_cache_directory(self):
    """
    获取系统构建应用代码缓存目录的方法。
    """
    return self.adb_shell("getprop ro.build.app.code_cache_dir")

def get_build_app_data_directory(self):
    """
    获取系统构建应用数据目录的方法。
    """
    return self.adb_shell("getprop ro.build.app.data_dir")

def get_build_app_shared_libraries(self):
    """
    获取系统构建应用共享库列表的方法。
    """
    return self.adb_shell("getprop ro.build.app.shared_libraries")

def get_build_app_private_libraries(self):
    """
    获取系统构建应用私有库列表的方法。
    """
    return self.adb_shell("getprop ro.build.app.private_libraries")

def get_build_app_security_patch(self):
    """
    获取系统构建应用安全补丁程序级别的方法。
    """
    return self.adb_shell("getprop ro.build.app.security_patch")

def get_build_app_target_sdk_version(self):
    """
    获取系统构建应用目标SDK版本号的方法。
    """
    return self.adb_shell("getprop ro.build.app.target_sdk_version")

def get_build_app_first_install_time(self):
    """
    获取系统构建应用第一次安装时间的方法。
    """
    return self.adb_shell("getprop ro.build.app.first_install_time")

更多实例

以下是一些adb命令的使用示例:

  1. 获取设备列表
adb devices
  1. 安装应用
adb install /path/to/app.apk
  1. 卸载应用
adb uninstall com.example.app
  1. 启动应用
adb shell am start -n com.example.app/.MainActivity
  1. 关闭应用
adb shell am force-stop com.example.app
  1. 清除应用数据
adb shell pm clear com.example.app
  1. 获取应用版本号
adb shell dumpsys package com.example.app | grep versionName
  1. 获取应用包名
adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'
  1. 获取设备信息
adb shell getprop ro.product.model
adb shell getprop ro.build.version.release
adb shell getprop ro.build.version.sdk
  1. 获取设备分辨率
adb shell wm size
  1. 截屏并保存到电脑
adb shell screencap /sdcard/screen.png
adb pull /sdcard/screen.png /path/to/save
  1. 录制屏幕并保存到电脑
adb shell screenrecord /sdcard/screen.mp4
adb pull /sdcard/screen.mp4 /path/to/save
  1. 查看日志
adb logcat
  1. 查看应用日志
adb logcat -s com.example.app
  1. 查看指定标签的日志
adb logcat -s TAG_NAME
  1. 查看指定进程的日志
adb logcat -b all -v time | grep com.example.app
  1. 清除日志缓存
adb logcat -c
  1. 模拟输入事件
adb shell input text "hello world"
adb shell input tap 100 100
adb shell input swipe 100 100 200 200
  1. 模拟按键事件
adb shell input keyevent KEYCODE_BACK
adb shell input keyevent KEYCODE_HOME
adb shell input keyevent KEYCODE_VOLUME_UP
adb shell input keyevent KEYCODE_VOLUME_DOWN
  1. 模拟滑动事件
adb shell input swipe x1 y1 x2 y2
  1. 模拟长按事件
adb shell input keyevent KEYCODE_MENU && adb shell input keyevent 23 && adb shell input keyevent KEYCODE_BACK
  1. 模拟多点触控事件
adb shell input tap x1 y1 x2 y2
  1. 获取屏幕截图并保存到设备
adb shell screencap /sdcard/screen.png
  1. 从设备中复制文件到电脑
adb pull /sdcard/file.txt /path/to/save
  1. 将文件复制到设备
adb push /path/to/file.txt /sdcard/
  1. 重启设备
adb reboot
  1. 进入Fastboot模式
adb reboot bootloader
  1. 进入Recovery模式
adb reboot recovery
  1. 查看设备CPU信息
adb shell cat /proc/cpuinfo
  1. 查看设备内存信息
adb shell cat /proc/meminfo
  1. 查看设备电池信息
adb shell dumpsys battery
  1. 查看设备网络状态
adb shell dumpsys connectivity
  1. 查看设备存储空间信息
adb shell df
  1. 查看设备运行的进程
adb shell ps
  1. 查看设备安装的应用
adb shell pm list packages
  1. 查看设备安装的系统应用
adb shell pm list packages -s
  1. 查看设备安装的第三方应用
adb shell pm list packages -3
  1. 查看应用的权限信息
adb shell dumpsys package com.example.app | grep permission
  1. 查看应用的服务信息
adb shell dumpsys activity services com.example.app
  1. 查看应用的广播信息
adb shell dumpsys activity broadcasts com.example.app
  1. 查看应用的Activity信息
adb shell dumpsys activity activities com.example.app
  1. 查看应用的Provider信息
adb shell dumpsys activity providers com.example.app
  1. 查看应用的Receiver信息
adb shell dumpsys activity receivers com.example.app
  1. 查看应用的进程信息
adb shell dumpsys activity processes com.example.app
  1. 查看应用的线程信息
adb shell dumpsys activity threads com.example.app
  1. 查看应用的堆栈信息
adb shell dumpsys activity top
  1. 查看应用的任务信息
adb shell dumpsys activity tasks
  1. 查看应用的Window信息
adb shell dumpsys window windows | grep com.example.app
  1. 查看应用的资源信息
adb shell dumpsys resources com.example.app
  1. 查看应用的内存信息
adb shell dumpsys meminfo com.example.app
  1. 查看应用的布局信息
adb shell uiautomator dump /sdcard/window_dump.xml
adb pull /sdcard/window_dump.xml /path/to/save
  1. 查看应用的控件信息
adb shell uiautomator dump /sdcard/window_dump.xml
adb pull /sdcard/window_dump.xml /path/to/save
  1. 查看应用的属性信息
adb shell uiautomator dump /sdcard/window_dump.xml
adb pull /sdcard/window_dump.xml /path/to/save
  1. 查看应用的文本信息
adb shell uiautomator dump /sdcard/window_dump.xml
adb pull /sdcard/window_dump.xml /path/to/save
  1. 查看应用的屏幕信息
adb shell dumpsys window
  1. 查看应用的布局信息
adb shell dumpsys activity
  1. 查看应用的进程信息
adb shell dumpsys activity processes
  1. 查看应用的内存信息
adb shell dumpsys meminfo
  1. 查看应用的CPU信息
adb shell top -n 1 -m 10
  1. 查看应用的线程信息
adb shell ps -t -p `adb shell pidof com.example.app`
  1. 查看应用的网络信息
adb shell dumpsys netstats
  1. 查看应用的文件信息
adb shell ls -l /data/data/com.example.app
  1. 查看应用的数据库信息
adb shell sqlite3 /data/data/com.example.app/databases/db_name.db
  1. 查看应用的文件目录信息
adb shell ls -l /data/data/com.example.app/files
  1. 查看应用的缓存目录信息
adb shell ls -l /data/data/com.example.app/cache
  1. 查看应用的Shared Preferences信息
adb shell cat /data/data/com.example.app/shared_prefs/pref_name.xml
  1. 查看应用的外部存储目录信息
adb shell ls -l /sdcard/Android/data/com.example.app
  1. 查看应用的外部缓存目录信息
adb shell ls -l /sdcard/Android/data/com.example.app/cache
  1. 查看应用的外部文件目录信息
adb shell ls -l /sdcard/Android/data/com.example.app/files
  1. 查看应用的外部公共目录信息
adb shell ls -l /sdcard/Documents/com.example.app
  1. 查看应用的外部公共图片目录信息
adb shell ls -l /sdcard/Pictures/com.example.app
  1. 查看应用的外部公共音乐目录信息
adb shell ls -l /sdcard/Music/com.example.app
  1. 查看应用的外部公共视频目录信息
adb shell ls -l /sdcard/Movies/com.example.app
  1. 查看应用的外部公共下载目录信息
adb shell ls -l /sdcard/Download/com.example.app
  1. 查看应用的外部公共文档目录信息
adb shell ls -l /sdcard/Documents/com.example.app
  1. 查看应用的外部公共闹铃目录信息
adb shell ls -l /sdcard/Alarms/com.example.app
  1. 查看应用的外部公共铃声目录信息
adb shell ls -l /sdcard/Ringtones/com.example.app
  1. 查看应用的外部公共通知目录信息
adb shell ls -l /sdcard/Notifications/com.example.app
  1. 查看应用的外部公共图片目录信息
adb shell ls -l /sdcard/Pictures/com.example.app
  1. 查看应用的外部公共音乐目录信息
adb shell ls -l /sdcard/Music/com.example.app
  1. 查看应用的外部公共视频目录信息
adb shell ls -l /sdcard/Movies/com.example.app
  1. 查看应用的外部公共下载目录信息
adb shell ls -l /sdcard/Download/com.example.app
  1. 查看应用的外部公共文档目录信息
adb shell ls -l /sdcard/Documents/com.example.app
  1. 查看应用的外部公共闹铃目录信息
adb shell ls -l /sdcard/Alarms/com.example.app
  1. 查看应用的外部公共铃声目录信息
adb shell ls -l /sdcard/Ringtones/com.example.app
  1. 查看应用的外部公共通知目录信息
adb shell ls -l /sdcard/Notifications/com.example.app
  1. 查看应用的外部公共图片目录信息
adb shell ls -l /sdcard/Pictures/com.example.app
  1. 查看应用的外部公共音乐目录信息
adb shell ls -l /sdcard/Music/com.example.app
  1. 查看应用的外部公共视频目录信息
adb shell ls -l /sdcard/Movies/com.example.app
  1. 查看应用的外部公共下载目录信息
adb shell ls -l /sdcard/Download/com.example.app
  1. 查看应用的外部公共文档目录信息
adb shell ls -l /sdcard/Documents/com.example.app
  1. 查看应用的外部公共闹铃目录信息
adb shell ls -l /sdcard/Alarms/com.example.app
  1. 查看应用的外部公共铃声目录信息
adb shell ls -l /sdcard/Ringtones/com.example.app
  1. 查看应用的外部公共通知目录信息
adb shell ls -l /sdcard/Notifications/com.example.app
  1. 查看应用的外部公共图片目录信息
adb shell ls -l /sdcard/Pictures/com.example.app
  1. 查看应用的外部公共音乐目录信息
adb shell ls -l /sdcard/Music/com.example.app
  1. 查看应用的外部公共视频目录信息
adb shell ls -l /sdcard/Movies/com.example.app
  1. 查看应用的外部公共下载目录信息
adb shell ls -l /sdcard/Download/com.example.app
  1. 查看应用的外部公共文档目录信息
adb shell ls -l /sdcard/Documents/com.example.app
  1. 查看应用的外部公共闹铃目录信息
adb shell ls -l /sdcard/Alarms/com.example.app
  1. 查看应用的外部公共铃声目录信息
adb shell ls -l /sdcard/Ringtones/com.example.app
  1. 查看应用的外部公共通知目录信息
adb shell ls -l /sdcard/Notifications/com.example.app
  1. 查看应用的外部公共图片目录信息
adb shell ls -l /sdcard/Pictures/com.example.app
  1. 查看应用的外部公共音乐目录信息
adb shell ls -l /sdcard/Music/com.example.app
  1. 查看应用的外部公共视频目录信息
adb shell ls -l /sdcard/Movies/com.example.app
  1. 查看应用的外部公共下载目录信息
adb shell ls -l /sdcard/Download/com.example.app
  1. 查看应用的外部公共文档目录信息
adb shell ls -l /sdcard/Documents/com.example.app
  1. 查看应用的外部公共闹铃目录信息
adb shell ls -l /sdcard/Alarms/com.example.app
  1. 查看应用的外部公共铃声目录信息
adb shell ls -l /sdcard/Ringtones/com.example.app
  1. 查看应用的外部公共通知目录信息
adb shell ls -l /sdcard/Notifications/com.example.app
  1. 查看应用的外部公共图片目录信息
adb shell ls -l /sdcard/Pictures/com.example.app
  1. 查看应用的外部公共音乐目录信息
adb shell ls -l /sdcard/Music/com.example.app
  1. 查看应用的外部公共视频目录信息
adb shell ls -l /sdcard/Movies/com.example.app
  1. 查看应用的外部公共下载目录信息
adb shell ls -l /sdcard/Download/com.example.app
  1. 查看应用的外部公共文档目录信息
adb shell ls -l /sdcard/Documents/com.example.app
  1. 查看应用的外部公共闹铃目录信息
adb shell ls -l /sdcard/Alarms/com.example.app
  1. 查看应用的外部公共铃声目录信息
adb shell ls -l /sdcard/Ringtones/com.example.app
  1. 查看应用的外部公共通知目录信息
adb shell ls -l /sdcard/Notifications/com.example.app
  1. 查看应用的外部公共图片目录信息
adb shell ls -l /sdcard/Pictures/com.example.app
  1. 查看应用的外部公共音乐目录信息
adb shell ls -l /sdcard/Music/com.example.app
  1. 查看应用的外部公共视频目录信息
adb shell ls -l /sdcard/Movies/com.example.app
  1. 查看应用的外部公共下载目录信息
adb shell ls -l /sdcard/Download/com.example.app
  1. 查看应用的外部公共文档目录信息
adb shell ls -l /sdcard/Documents/com.example.app
  1. 查看应用的外部公共闹铃目录信息
adb shell ls -l /sdcard/Alarms/com.example.app
  1. 查看应用的外部公共铃声目录信息
adb shell ls -l /sdcard/Ringtones/com.example.app
  1. 查看应用的外部公共通知目录信息
adb shell ls -l /sdcard/Notifications/com.example.app
  1. 查看应用的外部公共图片目录信息
adb shell ls -l /sdcard/Pictures/com.example.app
  1. 查看应用的外部公共音乐目录信息
adb shell ls -l /sdcard/Music/com.example.app
  1. 查看应用的外部公共视频目录信息
adb shell ls -l /sdcard/Movies/com.example.app
  1. 查看应用的外部公共下载目录信息
adb shell ls -l /sdcard/Download/com.example.app

参考资料:

  1. Python ADB库:GitHub - openatx/adbutils: pure python adb library for google adb service.
  2. Python subprocess模块:subprocess — Subprocess management — Python 3.11.2 documentation
  3. ADB命令详解:https://developer.android.com/studio/command-line/adb

猜你喜欢

转载自blog.csdn.net/zh6526157/article/details/129658008