harmonyos-build-deploy
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseHarmonyOS Build & Deploy
HarmonyOS 构建与部署
Complete workflow for building, cleaning, packaging, and installing HarmonyOS applications.
HarmonyOS应用的完整构建、清理、打包及安装工作流。
First Step: Confirm Operation with User
第一步:与用户确认操作
IMPORTANT: Before executing any build or deploy operation, confirm which specific operation(s) the user wants to perform. Ask the user to choose from:
| Operation | Description |
|---|---|
| Clean build artifacts | Remove previous build cache and outputs |
| Install dependencies | Use ohpm to install project dependencies |
| Build project | Use hvigorw to build HAP/APP packages |
| Install to device | Use hdc to install the app on a device |
| Full pipeline | Clean → install deps → build → deploy to device |
Why confirm first:
- Different scenarios require different operations (e.g., incremental build vs clean build)
- Avoid unnecessary time-consuming operations
- Give user control over the workflow
- Prevent accidental device installation
After user responds:
- Execute only the selected operations
- Report progress and results clearly
重要提示: 在执行任何构建或部署操作前,请确认用户需要执行的具体操作。请让用户从以下选项中选择:
| 操作 | 描述 |
|---|---|
| 清理构建产物 | 删除之前的构建缓存和输出文件 |
| 安装依赖 | 使用ohpm安装项目依赖 |
| 构建项目 | 使用hvigorw构建HAP/APP安装包 |
| 安装到设备 | 使用hdc将应用安装到设备上 |
| 完整流水线 | 清理 → 安装依赖 → 构建 → 部署到设备 |
确认原因:
- 不同场景需要不同操作(例如增量构建 vs 清理构建)
- 避免不必要的耗时操作
- 让用户掌控工作流
- 防止意外的设备安装
用户回复后:
- 仅执行用户选择的操作
- 清晰地报告进度和结果
Quick Reference
快速参考
bash
undefinedbash
undefinedBuild complete app (incremental)
构建完整应用(增量构建)
hvigorw assembleApp --mode project -p product=default -p buildMode=release --no-daemon
hvigorw assembleApp --mode project -p product=default -p buildMode=release --no-daemon
Install to device (Git Bash compatible)
安装到设备(兼容Git Bash)
If you use Git Bash on Windows, disable MSYS path conversion for hdc commands.
如果你在Windows上使用Git Bash,请为hdc命令禁用MSYS路径转换。
export MSYS_NO_PATHCONV=1
INSTALL_DIR="//data/local/tmp/install_$(date +%s)"
hdc -t <UDID> shell "mkdir -p $INSTALL_DIR"
export MSYS_NO_PATHCONV=1
INSTALL_DIR="//data/local/tmp/install_$(date +%s)"
hdc -t <UDID> shell "mkdir -p $INSTALL_DIR"
Push files one-by-one to explicit remote file paths (most reliable on Git Bash)
逐个推送文件到明确的远程文件路径(在Git Bash上最可靠)
for f in outputs/.hap outputs/.hsp; do
[ -f "$f" ] && hdc -t <UDID> file send "$f" "$INSTALL_DIR/$(basename "$f")"
done
for f in outputs/.hap outputs/.hsp; do
[ -f "$f" ] && hdc -t <UDID> file send "$f" "$INSTALL_DIR/$(basename "$f")"
done
Install (reinstall) HSPs first, then the HAP
先安装(重新安装)HSP,再安装HAP
for f in outputs/.hsp outputs/.hap; do
[ -f "$f" ] && hdc -t <UDID> shell "bm install -p $INSTALL_DIR/$(basename "$f") -r"
done
hdc -t <UDID> shell "rm -rf $INSTALL_DIR"
**Note:** Build output path is `outputs/`.for f in outputs/.hsp outputs/.hap; do
[ -f "$f" ] && hdc -t <UDID> shell "bm install -p $INSTALL_DIR/$(basename "$f") -r"
done
hdc -t <UDID> shell "rm -rf $INSTALL_DIR"
**注意:** 构建输出路径为 `outputs/`。Workflows
工作流
IMPORTANT: Build, clean, and deploy operations are long-running (build ~30s, file transfer ~20s). Always delegate these workflows to a subagent to avoid timeout. This also provides better error handling and clearer progress reporting to the user.
重要提示: 构建、清理和部署操作耗时较长(构建约30秒,文件传输约20秒)。请始终将这些工作流委托给子Agent,以避免超时。这也能提供更好的错误处理,并向用户清晰地报告进度。
Clean Build & Deploy
清理构建与部署
Delegate to subagent with the following steps:
- Clean:
hvigorw clean --no-daemon - Install dependencies:
ohpm install --all - Build:
hvigorw assembleApp --mode project -p product=default -p buildMode=release --no-daemon - Deploy to device (see Push and Install below)
- Launch:
hdc -t <UDID> shell "aa start -a EntryAbility -b <bundleName>" - Report success/failure with details
委托给子Agent执行以下步骤:
- 清理:
hvigorw clean --no-daemon - 安装依赖:
ohpm install --all - 构建:
hvigorw assembleApp --mode project -p product=default -p buildMode=release --no-daemon - 部署到设备(见下文的【推送与安装】)
- 启动:
hdc -t <UDID> shell "aa start -a EntryAbility -b <bundleName>" - 详细报告成功/失败结果
Deploy Only (No Rebuild)
仅部署(不重新构建)
Delegate to subagent with the following steps:
- Read to get bundleName
AppScope/app.json5 - Check for existing build outputs. If empty or missing, collect signed HAP/HSP from each module's build directory (
outputs/) into{srcPath}/build/default/outputs/default/*-signed.*. See module-discovery.md for details.outputs/ - Deploy to device (see Push and Install below)
- Launch:
hdc -t <UDID> shell "aa start -a EntryAbility -b <bundleName>" - Report success/failure with details
委托给子Agent执行以下步骤:
- 读取 获取bundleName
AppScope/app.json5 - 检查 目录是否存在构建输出。如果为空或缺失,从每个模块的构建目录(
outputs/)收集已签名的HAP/HSP到{srcPath}/build/default/outputs/default/*-signed.*目录。详情请参考 module-discovery.md。outputs/ - 部署到设备(见下文的【推送与安装】)
- 启动:
hdc -t <UDID> shell "aa start -a EntryAbility -b <bundleName>" - 详细报告成功/失败结果
Clean App Cache/Data
清理应用缓存/数据
Delegate to subagent:
- Clean cache:
hdc -t <UDID> shell "bm clean -n <bundleName> -c" - Clean data:
hdc -t <UDID> shell "bm clean -n <bundleName> -d" - Report success/failure
委托给子Agent执行:
- 清理缓存:
hdc -t <UDID> shell "bm clean -n <bundleName> -c" - 清理数据:
hdc -t <UDID> shell "bm clean -n <bundleName> -d" - 报告成功/失败结果
Build Commands (hvigorw)
构建命令(hvigorw)
Incremental Build (Default)
增量构建(默认)
Use incremental build for normal development - only changed modules are rebuilt:
bash
undefined在日常开发中使用增量构建 - 仅重新构建修改过的模块:
bash
undefinedBuild complete app (incremental)
构建完整应用(增量构建)
hvigorw assembleApp --mode project -p product=default -p buildMode=release --no-daemon
undefinedhvigorw assembleApp --mode project -p product=default -p buildMode=release --no-daemon
undefinedSingle Module Build
单模块构建
Build only a specific module for faster iteration:
bash
undefined仅构建特定模块以加快迭代速度:
bash
undefinedBuild single HAP module
构建单个HAP模块
hvigorw assembleHap -p module=entry@default --mode module -p buildMode=release --no-daemon
hvigorw assembleHap -p module=entry@default --mode module -p buildMode=release --no-daemon
Build single HSP module
构建单个HSP模块
hvigorw assembleHsp -p module=my_feature@default --mode module -p buildMode=release --no-daemon
hvigorw assembleHsp -p module=my_feature@default --mode module -p buildMode=release --no-daemon
Build single HAR module
构建单个HAR模块
hvigorw assembleHar -p module=library@default --mode module -p buildMode=release --no-daemon
hvigorw assembleHar -p module=library@default --mode module -p buildMode=release --no-daemon
Build multiple modules at once
同时构建多个模块
hvigorw assembleHsp -p module=module1@default,module2@default --mode module -p buildMode=release --no-daemon
**Module name format:** `{moduleName}@{targetName}`
- `moduleName`: Directory name of the module (e.g., `entry`, `my_feature`)
- `targetName`: Target defined in module's `build-profile.json5` (usually `default`)
**When to use single module build:**
- Developing/debugging a specific module
- Faster build times during iteration
- Testing changes in isolated module
**Note:** After single module build, you still need to run `assembleApp` to package the complete application for installation.hvigorw assembleHsp -p module=module1@default,module2@default --mode module -p buildMode=release --no-daemon
**模块名称格式:** `{moduleName}@{targetName}`
- `moduleName`:模块的目录名称(例如 `entry`、`my_feature`)
- `targetName`:模块的`build-profile.json5`中定义的目标(通常为`default`)
**何时使用单模块构建:**
- 开发/调试特定模块
- 迭代过程中加快构建速度
- 在独立模块中测试变更
**注意:** 单模块构建完成后,仍需运行`assembleApp`来打包完整的可安装应用。Clean Build (When Needed)
清理构建(必要时使用)
Only perform clean build when:
- First time building the project
- Encountering unexplained build errors
- After modifying or SDK version
build-profile.json5 - Dependency resolution issues
bash
undefined仅在以下场景执行清理构建:
- 首次构建项目
- 遇到无法解释的构建错误
- 修改或SDK版本后
build-profile.json5 - 依赖解析出现问题
bash
undefinedClean build artifacts
清理构建产物
hvigorw clean --no-daemon
hvigorw clean --no-daemon
Deep clean (for dependency issues)
深度清理(解决依赖问题)
ohpm clean && ohpm cache clean
ohpm install --all
hvigorw --sync -p product=default -p buildMode=release --no-daemon
hvigorw assembleApp --mode project -p product=default -p buildMode=release --no-daemon
undefinedohpm clean && ohpm cache clean
ohpm install --all
hvigorw --sync -p product=default -p buildMode=release --no-daemon
hvigorw assembleApp --mode project -p product=default -p buildMode=release --no-daemon
undefinedInstall Dependencies (ohpm)
安装依赖(ohpm)
bash
undefinedbash
undefinedInstall all dependencies (after clean or first clone)
安装所有依赖(清理后或首次克隆项目时)
ohpm install --all
ohpm install --all
With custom registry
使用自定义镜像源
ohpm install --all --registry "https://repo.harmonyos.com/ohpm/"
undefinedohpm install --all --registry "https://repo.harmonyos.com/ohpm/"
undefinedSync Project
同步项目
Only needed after modifying or :
build-profile.json5oh-package.json5bash
hvigorw --sync -p product=default -p buildMode=release --no-daemon仅在修改或后需要执行:
build-profile.json5oh-package.json5bash
hvigorw --sync -p product=default -p buildMode=release --no-daemonBuild Parameters
构建参数
| Parameter | Description |
|---|---|
| Target product defined in build-profile.json5 |
| Build mode |
| Target module with |
| Build all modules in project |
| Build specific module only |
| Disable daemon (recommended for CI) |
| Enable build analysis |
| 参数 | 描述 |
|---|---|
| |
| 构建模式 |
| 配合 |
| 构建项目中的所有模块 |
| 仅构建特定模块 |
| 禁用守护进程(CI环境推荐使用) |
| 启用构建分析 |
Build Outputs
构建输出
Build output path:
outputs/outputs/
├── entry-default-signed.hap
└── *.hsp构建输出路径:
outputs/outputs/
├── entry-default-signed.hap
└── *.hspModule Types
模块类型
| Type | Extension | Description |
|---|---|---|
| HAP | | Harmony Ability Package - Application entry module |
| HSP | | Harmony Shared Package - Dynamic shared library |
| HAR | | Harmony Archive - Static library (compiled into HAP) |
| APP | | Complete application bundle (all HAP + HSP) |
| 类型 | 扩展名 | 描述 |
|---|---|---|
| HAP | | Harmony Ability Package - 应用入口模块 |
| HSP | | Harmony Shared Package - 动态共享库 |
| HAR | | Harmony Archive - 静态库(编译到HAP中) |
| APP | | 完整应用包(包含所有HAP + HSP) |
Finding Modules
查找模块
Modules are defined in at the project root. The module type is determined by the field in :
build-profile.json5type{module}/src/main/module.json5 | Module Type | Build Command |
|---|---|---|
| HAP | |
| HSP | |
| HAR | |
Module build outputs:
{srcPath}/build/default/outputs/default/For detailed module discovery, build output structure, and handling unwanted modules, see references/module-discovery.md.
模块在项目根目录的中定义。模块类型由中的字段决定:
build-profile.json5{module}/src/main/module.json5type | 模块类型 | 构建命令 |
|---|---|---|
| HAP | |
| HSP | |
| HAR | |
模块构建输出路径:
{srcPath}/build/default/outputs/default/关于模块发现、构建输出结构及无用模块处理的详细信息,请参考 references/module-discovery.md。
Device Installation (hdc)
设备安装(hdc)
hdc (HarmonyOS Device Connector) is the CLI tool for device communication, similar to Android's adb.
hdc(HarmonyOS Device Connector)是用于设备通信的CLI工具,类似Android的adb。
Check Device
检查设备
bash
hdc list targets # List connected devices (returns UDID)
hdc -t <UDID> shell "whoami" # Test connectionbash
hdc list targets # 列出已连接设备(返回UDID)
hdc -t <UDID> shell "whoami" # 测试连接Push and Install
推送与安装
bash
undefinedbash
undefinedCreate temp directory on device (Git Bash compatible)
在设备上创建临时目录(兼容Git Bash)
If you use Git Bash on Windows, disable MSYS path conversion for hdc commands.
如果你在Windows上使用Git Bash,请为hdc命令禁用MSYS路径转换。
export MSYS_NO_PATHCONV=1
INSTALL_DIR="//data/local/tmp/install_$(date +%s)"
hdc -t <UDID> shell "mkdir -p $INSTALL_DIR"
export MSYS_NO_PATHCONV=1
INSTALL_DIR="//data/local/tmp/install_$(date +%s)"
hdc -t <UDID> shell "mkdir -p $INSTALL_DIR"
Push files one-by-one to explicit remote file paths (most reliable on Git Bash)
逐个推送文件到明确的远程文件路径(在Git Bash上最可靠)
for f in outputs/.hap outputs/.hsp; do
[ -f "$f" ] && hdc -t <UDID> file send "$f" "$INSTALL_DIR/$(basename "$f")"
done
for f in outputs/.hap outputs/.hsp; do
[ -f "$f" ] && hdc -t <UDID> file send "$f" "$INSTALL_DIR/$(basename "$f")"
done
Install (reinstall) HSPs first, then the HAP
先安装(重新安装)HSP,再安装HAP
for f in outputs/.hsp outputs/.hap; do
[ -f "$f" ] && hdc -t <UDID> shell "bm install -p $INSTALL_DIR/$(basename "$f") -r"
done
for f in outputs/.hsp outputs/.hap; do
[ -f "$f" ] && hdc -t <UDID> shell "bm install -p $INSTALL_DIR/$(basename "$f") -r"
done
Clean up temp directory
清理临时目录
hdc -t <UDID> shell "rm -rf $INSTALL_DIR"
undefinedhdc -t <UDID> shell "rm -rf $INSTALL_DIR"
undefinedVerify and Launch
验证与启动
bash
undefinedbash
undefinedCheck installation
检查安装状态
hdc -t <UDID> shell "bm dump -n <bundleName>"
hdc -t <UDID> shell "bm dump -n <bundleName>"
Launch app (default ability)
启动应用(默认Ability)
hdc -t <UDID> shell "aa start -a EntryAbility -b <bundleName>"
undefinedhdc -t <UDID> shell "aa start -a EntryAbility -b <bundleName>"
undefinedUninstall
卸载
bash
hdc -t <UDID> shell "bm uninstall -n <bundleName>"bash
hdc -t <UDID> shell "bm uninstall -n <bundleName>"hdc Command Reference
hdc命令参考
| Command | Description |
|---|---|
| List connected devices |
| Execute shell command on device |
| Push file/directory to device |
| Pull file/directory from device |
| Kill hdc server |
| Start hdc server |
| Show hdc version |
| 命令 | 描述 |
|---|---|
| 列出已连接设备 |
| 在设备上执行shell命令 |
| 推送文件/目录到设备 |
| 从设备拉取文件/目录 |
| 终止hdc服务器 |
| 启动hdc服务器 |
| 显示hdc版本 |
Bundle Manager (bm)
包管理器(bm)
Run via :
hdc -t <UDID> shell "bm ..."| Command | Description |
|---|---|
| Install from directory (all HAP/HSP) |
| Install single HAP file |
| Reinstall (replace existing, keep data) |
| Uninstall application |
| Show package info |
| List all installed packages |
| Clean application cache |
| Clean application data |
通过执行:
hdc -t <UDID> shell "bm ..."| 命令 | 描述 |
|---|---|
| 从目录安装(所有HAP/HSP) |
| 安装单个HAP文件 |
| 重新安装(替换现有应用,保留数据) |
| 卸载应用 |
| 显示包信息 |
| 列出所有已安装包 |
| 清理应用缓存 |
| 清理应用数据 |
Ability Assistant (aa)
Ability助手(aa)
Run via :
hdc -t <UDID> shell "aa ..."| Command | Description |
|---|---|
| Start specific ability |
| Force stop application |
通过执行:
hdc -t <UDID> shell "aa ..."| 命令 | 描述 |
|---|---|
| 启动指定Ability |
| 强制停止应用 |
Troubleshooting
故障排查
| Error | Cause | Solution |
|---|---|---|
| HSP in output not in build-profile.json5 | Remove unwanted HSP files before install (see module-discovery.md) |
| Signature/profile mismatch | Check signing config in build-profile.json5; verify bundleName matches app.json5; check certificate not expired |
| Device SDK < app's compatibleSdkVersion | Update device or lower compatibleSdkVersion |
| Device not found | Connection issue | Check USB; enable Developer Options (tap build number 7x) and USB debugging; |
| Invalid or expired certificate | Regenerate certificate in DevEco Studio; check validity period; ensure correct signing config for build type |
| 错误 | 原因 | 解决方案 |
|---|---|---|
| 输出目录中的HSP未在build-profile.json5中定义 | 安装前删除无用的HSP文件(参考 module-discovery.md) |
| 签名/配置文件不匹配 | 检查build-profile.json5中的签名配置;验证bundleName与app.json5一致;检查证书是否过期 |
| 设备SDK版本 < 应用的compatibleSdkVersion | 更新设备系统或降低应用的compatibleSdkVersion |
| 设备未找到 | 连接问题 | 检查USB连接;启用开发者选项(连续点击版本号7次)和USB调试;执行 |
| 证书无效或过期 | 在DevEco Studio中重新生成证书;检查证书有效期;确保构建类型使用正确的签名配置 |
Key Configuration Files
关键配置文件
| File | Description |
|---|---|
| App metadata (bundleName, versionCode, versionName) |
| Modules list, products, signing configs |
| Module config (abilities, permissions) |
| Module dependencies |
| 文件 | 描述 |
|---|---|
| 应用元数据(bundleName、versionCode、versionName) |
| 模块列表、产品配置、签名配置 |
| 模块配置(Ability、权限) |
| 模块依赖 |
Reference Files
参考文档
- Module Discovery & Build Outputs: references/module-discovery.md - Module definitions, type identification, build output paths, unwanted modules
- Complete Installation Guide: references/device-installation.md - Version verification scripts and installation script
- 模块发现与构建输出:references/module-discovery.md - 模块定义、类型识别、构建输出路径、无用模块处理
- 完整安装指南:references/device-installation.md - 版本验证脚本和安装脚本
Related Skills
相关技能
- arkts-development: ArkTS/ArkUI development patterns, state management, component lifecycle, and API usage. Use alongside this skill when developing HarmonyOS application code.
- arkts-development:ArkTS/ArkUI开发模式、状态管理、组件生命周期及API使用。开发HarmonyOS应用代码时可搭配本技能使用。