写点什么

一文掌握 HarmonyOS5 模拟器与真机调试技巧

作者:颜颜yan_
  • 2025-06-11
    湖北
  • 本文字数:2283 字

    阅读完需:约 7 分钟

一文掌握 HarmonyOS5 模拟器与真机调试技巧

⭐本期内容:一文掌握 HarmonyOS5 模拟器与真机调试技巧

🏆系列专栏:鸿蒙 HarmonyOS:探索未来智能生态新纪元



前言

DevEco Studio 的预览器可以提供高效的 UI 实时反馈,但要进行全面的功能测试、性能分析及硬件相关功能调试,仍需在模拟器或真机上运行应用。


使用本地真机运行应用/服务

设备连接

  • USB 连接

  • 通过 USB 线将 Phone 或 Tablet 连接至 PC,在设备的 设置 > 系统 > 开发者选项 中开启 USB 调试。设备上弹出授权对话框时,点击“允许”。



  • 无线连接

  • 开启“无线调试”,通过 IP 地址连接设备:


  hdc tconn 设备IP地址:端口号
复制代码


设备管理

在 DevEco Studio 底部的 Device Manager 中查看已连接设备,确认设备状态、系统版本和连接方式。

应用部署

点击菜单栏 Run > Run '模块名',或使用快捷键:


  • Windows/Linux:Shift + F10

  • macOS:Control + R


调试技巧

  • 断点调试:点击代码行号旁边设置断点

  • 日志查看:使用 Logcat 窗口实时查看日志

  • 性能监控:内置工具可监控 CPU、内存等性能指标

  • 热更新:支持代码变更后无需重新安装即可快速同步至设备



使用模拟器运行应用/服务

系统要求

  • 操作系统:Windows 10/11 64 位、macOS 10.15+

  • CPU:支持 VT-x / AMD-V 虚拟化

  • 内存:8GB 起(推荐 16GB)

  • 硬盘:至少 10GB 可用空间

前置条件

  • 在 BIOS 中启用虚拟化功能

  • 安装最新版 DevEco Studio

  • 下载并安装所需模拟器系统镜像

模拟器 vs 真机


操控屏幕


使用工具栏


移动与缩放模拟器窗口

  • 拖动标题栏移动窗口

  • 支持缩放比例(50%、100%、150%)

  • 支持全屏模式(F11 切换)

  • 支持多窗口并排显示

应用安装与文件传输

应用安装

  • 方法一:拖拽 .hap 文件到模拟器窗口

  • 方法二:使用如下命令行。


hdc install path/to/your/app.hap
复制代码


文件传输

# 发送文件hdc file send local_file remote_path
# 接收文件hdc file recv remote_path local_folder
复制代码


支持配置共享文件夹,实现主机与模拟器之间文件共享。

扩展能力

  • 快照:保存/恢复测试状态

  • 硬件模拟:模拟加速度计、陀螺仪、通话、电量等

  • 调试工具:性能分析、UI 渲染分析

  • 自动化测试:录制操作、批量运行、脚本回放

常见问题与解决方案



HDC 常用命令

设备管理

# 列出所有已连接的设备hdc list targets
# 连接到指定IP和端口的设备(无线调试)hdc target connect 192.168.1.100:8710
# 断开与指定设备的连接hdc target disconnect 192.168.1.100:8710
# 重启设备hdc target reboot
# 检查设备连接状态hdc check-status
复制代码

应用管理

# 安装应用hdc install path/to/your/app.hap
# 安装多个HAP包hdc install -m path/to/entry.hap path/to/feature.hap
# 卸载应用hdc uninstall com.example.app
# 启动应用hdc shell aa start -a com.example.app.MainAbility -b com.example.app
# 停止应用hdc shell aa stop -a com.example.app.MainAbility -b com.example.app
# 清除应用数据hdc shell aa clear -b com.example.app
复制代码

文件操作

# 从设备拉取文件到本地hdc file recv /data/storage/el2/base/files/example.db ./local_folder/
# 发送本地文件到设备hdc file send ./local_file.txt /data/storage/el2/base/files/
# 列出设备目录内容hdc shell ls -la /data/storage/el2/base/files/
# 删除设备上的文件hdc shell rm /data/storage/el2/base/files/temp.txt
# 创建设备上的目录hdc shell mkdir /data/storage/el2/base/files/new_folder
复制代码

日志与调试

# 查看实时日志hdc shell hilog
# 按标签过滤日志hdc shell hilog | grep "MyApp"
# 保存日志到文件hdc shell hilog > app_log.txt
# 清除日志缓冲区hdc shell hilog -c
# 设置日志级别hdc shell hilog -L DEBUG
复制代码

性能分析

# 显示进程列表hdc shell ps -ef
# 显示特定应用的进程hdc shell ps -ef | grep com.example.app
# 查看内存使用情况hdc shell free -m
# 查看CPU使用情况hdc shell top -n 1
# 获取应用内存使用详情hdc shell dumpsys meminfo com.example.app
复制代码

网络相关

# 端口转发(将设备8080端口映射到本地8888端口)hdc forward tcp:8888 tcp:8080
# 移除端口转发hdc forward --remove tcp:8888
# 列出所有端口转发规则hdc forward --list
# 查看设备网络配置hdc shell ifconfig
# 测试网络连接hdc shell ping www.example.com
复制代码

自动化测试

# 执行UI自动化测试hdc shell aa test -p com.example.test -s class com.example.test.TestClass
# 模拟输入事件(点击屏幕坐标x=500, y=600)hdc shell input tap 500 600
# 模拟滑动事件(从(300,500)滑动到(300,100))hdc shell input swipe 300 500 300 100
# 模拟按键事件(按下返回键)hdc shell input keyevent 4
# 模拟文本输入hdc shell input text "Hello%sWorld" # %s表示空格
复制代码

模拟器专用

# 列出所有可用模拟器hdc emulator list
# 创建新模拟器hdc emulator create -n MyEmulator -t phone
# 启动模拟器hdc emulator start -n MyEmulator
# 停止模拟器hdc emulator stop -n MyEmulator
# 设置模拟器属性hdc emulator set -n MyEmulator -k resolution -v 1080x2340
复制代码

批处理示例

# 执行设备上的Shell脚本hdc shell sh /data/local/tmp/test_script.sh
# 执行本地Shell脚本并将结果传回hdc shell < ./local_script.sh > result.txt
# 批量安装多个应用for hap in ./apps/*.hap; do hdc install "$hap"; done
复制代码



总结

无论是使用真机还是模拟器进行开发调试,合理选择测试环境与工具,能显著提升开发效率与应用质量。配合强大的 HDC 命令行工具,开发者可以实现高效部署、调试与自动化测试,是日常 HarmonyOS 应用开发的核心能力之一。


如有疑问,欢迎随时私信交流!

发布于: 15 小时前阅读数: 3
用户头像

颜颜yan_

关注

还未添加个人签名 2022-10-01 加入

还未添加个人简介

评论

发布
暂无评论
一文掌握 HarmonyOS5 模拟器与真机调试技巧_架构_颜颜yan__InfoQ写作社区