
OpenHarmony和Android源码编译完全是两个世界。Android的AOSP经过十几年迭代相当顺滑,OpenHarmony还在快速演进期,工具链、构建脚本、依赖仓库状态变化频繁,官方文档偏理想状态,按步骤走容易卡住。正因不成熟,源码编译特别有价值。本文记录从零在一台全新Ubuntu机器上完整编译OpenHarmony标准系统,包括环境搭建、源码拉取、编译参数、踩过的坑。
编译环境:Ubuntu 22.04 LTS、64GB内存、16核CPU、NVMe系统盘加机械数据盘。目标OpenHarmony 4.0 Release分支,编译产品RK3568开发板镜像。官方最低要求16GB内存200GB磁盘,但16GB编标准系统大概率中途OOM;内存低于32GB建议关掉并行任务。编译完out目录占68GB,主力编译盘务必用NVMe,机械盘会被I/O拖垮。
Ubuntu版本建议22.04,4.0编译脚本已在此大量验证;20.04部分依赖版本触发兼容告警;24.04的Python过高需切换环境。基础工具链用apt安装git、gnupg、flex、bison、gperf、build-essential、libssl-dev、python3-pip等,libssl-dev新版是3.x,旧工具链需1.x兼容层。文件句柄限制是必踩坑,源码树超20万文件,默认1024限制会报随机Too many open files,要在/etc/security/limits.conf加nofile 65536并重新登录生效。建议用bash而非dash执行编译脚本。
源码获取:安装repo并配清华pip源,repo init -u gitee.com/openharmony/manifest.git -b OpenHarmony-4.0-Release,repo sync -c -j8。全量源码10GB级,中断后先repo sync -l本地校验再repo sync -c -j8 --no-clone-bundle继续;.repo目录占43GB。目录结构中device/rockchip下有RK3568镜像脚本,build目录是编译系统核心。
构建系统:hb工具分三阶段,加载编译配置、生成ninja构建文件、执行ninja编译。GN(Generate Ninja)是元构建系统,描述目标依赖关系而非执行命令序列,比Makefile依赖追踪更准;ninja读取扁平.ninja文件调度极快。hb需在源码根目录用python3 -m pip install --user build/lite并source init_env.sh,每次新终端都要重新source。hb set选产品和编译类型,hb build -f全量编译。常用参数-f全量、-T指定模块、-c缓存目录、--ccache、--v详细日志,最实用的是-T只编某子系统加速调试。
实测首次全量编译约一个半小时:配置加载和GN解析约8分钟、ninja调度准备约3分钟、内核编译约15分钟、系统库和应用编译50至70分钟、镜像打包约2分钟。并行度合理算法是内存GB除以2.5,32GB用-j12、64GB用-j20,16核默认-j16内存峰值近44GB会OOM。ccache二次全量编译从1.5小时降到30分钟。增量编译改头文件会退化到接近全量;改代码后需hb build --pkg重新打包镜像否则烧录没变化。
报错处理:OOM三层次降并行度、加16GB swap、top定位内存大户;网络超时用--no-clone-bundle加-c;Python 3.12移除distutils要用pyenv锁3.10;磁盘不足要out分区大于50GB。烧录RK3568注意驱动权限和镜像型号匹配,rk3566和rk3568的dts不同烧错起不来。产物在out/rk3568/packages/phone/images/下有system.img、vendor.img、userdata.img、boot.img、u-boot.img,用fastboot分别烧录。hdc工具类似adb,hdc install装hap,签名问题用userdebug版本绕开。
本文来源:https://www.hqwc.cn/a/2001213.html
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。
Building OpenHarmony from source and building Android are two entirely different worlds. Android's AOSP is quite smooth after more than a decade of iteration, while OpenHarmony is still in a period of rapid evolution: toolchains, build scripts and dependency repositories change frequently, the official documentation tends to describe an ideal state, and following the steps can easily get you stuck. Precisely because it is immature, source compilation is especially valuable. This article records building the OpenHarmony standard system from scratch on a brand-new Ubuntu machine, covering environment setup, source retrieval, build parameters and the pitfalls encountered.
Build environment: Ubuntu 22.04 LTS, 64GB RAM, a 16-core CPU, an NVMe system drive plus a mechanical data drive. The target is the OpenHarmony 4.0 Release branch, building the image for the RK3568 development board. The official minimum is 16GB RAM and 200GB disk, but building the standard system with 16GB will most likely hit OOM midway; with less than 32GB of RAM it is advisable to disable parallel tasks. After building, the out directory takes 68GB, so the main build drive must be NVMe — a mechanical drive will be crippled by I/O.
Ubuntu 22.04 is recommended, as the 4.0 build scripts have been extensively validated on it; on 20.04 some dependency versions trigger compatibility warnings; on 24.04 the Python version is too new and requires switching environments. Install the basic toolchain with apt: git, gnupg, flex, bison, gperf, build-essential, libssl-dev, python3-pip and so on; the new libssl-dev is 3.x while older toolchains need a 1.x compatibility layer. The file-handle limit is an unavoidable pitfall — the source tree has over 200,000 files, and the default limit of 1024 causes random "Too many open files" errors; add nofile 65536 to /etc/security/limits.conf and log in again for it to take effect. It is recommended to run build scripts with bash rather than dash.
Source retrieval: install repo and configure the Tsinghua pip mirror, then run repo init -u gitee.com/openharmony/manifest.git -b OpenHarmony-4.0-Release and repo sync -c -j8. The full source is on the order of 10GB; after an interruption, first run repo sync -l for local verification and then repo sync -c -j8 --no-clone-bundle to continue; the .repo directory takes 43GB. In the directory structure, device/rockchip contains the RK3568 image scripts and the build directory is the core of the build system.
Build system: the hb tool works in three stages — load the build configuration, generate ninja build files, and execute the ninja build. GN (Generate Ninja) is a meta-build system that describes target dependencies rather than command execution sequences, giving more accurate dependency tracking than Makefile; ninja reads flat .ninja files and schedules extremely fast. hb needs python3 -m pip install --user build/lite in the source root and source init_env.sh, and you must re-source it in every new terminal. Use hb set to select the product and build type, and hb build -f for a full build. Common parameters are -f for full build, -T to specify a module, -c for the cache directory, --ccache and --v for verbose logs; the most practical is -T to build only a certain subsystem, speeding up debugging.
Measured first full build takes about one and a half hours: configuration loading and GN parsing about 8 minutes, ninja scheduling preparation about 3 minutes, kernel build about 15 minutes, system libraries and applications 50 to 70 minutes, and image packaging about 2 minutes. A sensible rule for parallelism is RAM in GB divided by 2.5 — use -j12 for 32GB and -j20 for 64GB; the 16-core default of -j16 pushes peak memory near 44GB and causes OOM. ccache reduces a second full build from 1.5 hours to 30 minutes. Incremental builds degrade to nearly full builds when header files change; after changing code you need hb build --pkg to repackage the image, otherwise flashing changes nothing.
Error handling: for OOM, three levels — reduce parallelism, add 16GB of swap, and use top to locate memory hogs; for network timeouts use --no-clone-bundle plus -c; Python 3.12 removed distutils, so use pyenv to pin 3.10; for insufficient disk make the out partition larger than 50GB. When flashing the RK3568, pay attention to driver permissions and matching the image model — the rk3566 and rk3568 have different DTS files and flashing the wrong one will not boot. The artefacts under out/rk3568/packages/phone/images/ include system.img, vendor.img, userdata.img, boot.img and u-boot.img, flashed separately with fastboot. The hdc tool is similar to adb; hdc install installs a hap, and signature issues can be worked around with a userdebug build.
本文来源:https://www.hqwc.cn/a/2001213.html
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。