配置别名,简化 Flutter OpenHarmony 日常编译命令
欢迎大家加入开源鸿蒙跨平台开发者社区
通过 shell 别名把冗长的 flutter build hap / flutter run 参数收拢成短命令,减少重复输入与出错。
适用前提
- 已安装支持 OpenHarmony 的 Flutter 工具链,且
flutter、hdc已在 PATH 中。 --local-engine为可选项:指向你本地编译的 Engine 产物;若使用官方/镜像预编译引擎,可去掉--local-engine=...及相关ENGINE_*变量,只保留alias中的其余参数。
各系统把别名写在哪里
| 系统 | 建议文件 | 说明 |
|---|---|---|
| Windows | Git Bash 用户目录下的 ~/.bashrc 或 ~/.bash_profile |
在资源管理器空白处右键选择 Git Bash Here,用编辑器打开上述文件之一追加配置即可。 |
| Linux | ~/.bashrc 或 ~/.bash_profile |
二选一常用即可;改完后执行 source ~/.bashrc(或对应文件)生效。 |
| macOS | ~/.zshrc(默认 zsh) |
若习惯把通用配置放在 ~/.bash_profile,可在 ~/.zshrc 末尾加一行:source ~/.bash_profile。 |
配置内容
将下面整段追加到上述配置文件中(按需修改 ENGINE_DIR 为你的 Engine 源码/产物根路径)。
sh
# --local-engine 可选:指向自行编译的 engine 产物,Linux/mac 上较常用
export ENGINE_DIR=~/ohos/engine
export ENGINE_DEBUG=$ENGINE_DIR/src/out/ohos_debug_unopt_arm64
export ENGINE_PROFILE=$ENGINE_DIR/src/out/ohos_profile_arm64
export ENGINE_RELEASE=$ENGINE_DIR/src/out/ohos_release_arm64
# fbuild* 用双引号即可(仅展开 ENGINE_*);frun* 用单引号,避免 $(hdc ...) 在「定义别名」时被提前展开
alias fbuildD="flutter build hap --local-engine=$ENGINE_DEBUG --debug"
alias fbuildP="flutter build hap --local-engine=$ENGINE_PROFILE --profile"
alias fbuildR="flutter build hap --local-engine=$ENGINE_RELEASE --release"
alias frunD='flutter run -d "$(hdc list targets)" --local-engine=$ENGINE_DEBUG --debug'
alias frunP='flutter run -d "$(hdc list targets)" --local-engine=$ENGINE_PROFILE --profile'
alias frunR='flutter run -d "$(hdc list targets)" --local-engine=$ENGINE_RELEASE --release'
说明:
fbuild*:仅构建 HAP,不安装运行。frun*:对hdc list targets列出的目标执行flutter run。若连接多台设备,请先hdc tconn <设备>或改用明确设备 ID,避免-d解析异常。- 不使用本地 Engine 时,可把别名改成例如:
alias fbuildD="flutter build hap --debug",并删除不需要的ENGINE_*行。
生效与使用
- 保存文件后,新开终端,或执行
source ~/.zshrc(或你实际编辑的文件)。 - 在 Flutter 工程目录下使用别名,例如:
sh
flutter create hello --platforms ohos
cd hello
fbuildD # Debug 构建
frunD # Debug 运行到设备
若某条命令报错,先在同一终端执行 hdc list targets 与 flutter doctor,确认设备与工具链正常。