跪拜 Guibai
← All articles
HarmonyOS

Rust Rewrites HarmonyOS Builds: arkdown Cuts Cold Builds 8.4x and Memory 75%

By Ranger0929 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

HarmonyOS developers facing 10-minute cold builds and memory OOMs now have a Rust alternative that produces byte-identical packages to the official toolchain. arkdown's 75% memory reduction makes large builds feasible on 16 GB dev machines and constrained CI containers, and its npm/CLI interface drops into existing workflows without a separate runtime.

Summary

arkdown, an open-source Rust build toolchain for HarmonyOS, replaces the official Hvigor pipeline end-to-end: parsing ArkTS/TypeScript, type checking, UI syntax transformation, module bundling, Native C/C++ compilation, resource handling, ABC bytecode generation, and signing into HAP, HSP, HAR, and APP packages. It's built on OXC for parsing/AST/codegen, Rolldown for module graph scanning, and tsrs for TypeScript semantics—all modified to understand ArkTS's custom syntax and ArkUI DSL, with tsrs embedded in-process to eliminate subprocess IPC.

The payoff is measured on a real production app (11,627 TS files + 23 ETS files, with 1.5 GB of native debug symbols). Cold builds drop from 629.69s (Hvigor) to 75.36s with full type checking, or 62.94s with --skip-checker. Peak physical memory falls from 9.68 GiB to 2.39 GiB. The toolchain passes 37/37 strict output-parity fixture tests against hvigorw with zero allowlist, covering HAP, HSP, HAR, APP, hot-reload HQF, and ArkGuard obfuscation. It installs via npm and runs with a single npx command; a --enable-zig flag cross-compiles native code without extra config.

Remaining gaps: cross-process hot rebuilds are still slower than Hvigor (15-17s vs 5.96s) because Rolldown lacks a structured disk graph cache, and HAR signing still falls back to a Java jar. Both are on the roadmap. The author also plans to extend the codebase into millisecond-level formatters, linters, and LSP support for ArkTS in editors like Zed and VS Code.

Takeaways
— Cold build on a 11,627 TS + 23 ETS file production app: 75.36s with full checker, 62.94s with --skip-checker, versus Hvigor's 629.69s.
— Peak physical memory footprint falls from 9.68 GiB (Hvigor) to 2.39 GiB (arkdown full checker), a 75.3% reduction.
— 37/37 strict Hvigor parity fixtures pass with zero differences, covering HAP, HSP, HAR, APP, HQF, ArkGuard, and signing.
— arkdown uses OXC for parsing/codegen, Rolldown for module graph scanning, and tsrs embedded in-process for type checking and linter—no subprocess or JSON-RPC.
— Rolldown was modified with a 'scan_module_sources' channel and 'retain_link_output' to drop AST/symbol memory immediately after each file's dependency scan.
— tsrs ported 1,003 ArkTS 1.1 Linter test cases and 243 full-project diagnostics with 100% alignment to the official compiler.
— Hot rebuilds (no changes) are slower: 15-17s vs Hvigor's 5.96s, due to missing disk module graph cache and 1.5GB content hashing; both are targeted for optimization.
— HAR signing still uses the SDK's Java-based sdk-sign-tool-full.jar; arkdown plans to replace it with native Rust hapsigner-rs.
Conclusions

The strict parity test suite (37/37 with no allowlist) is the credibility anchor; it forces the Rust implementation to match Hvigor output exactly, which is what makes the toolchain trustworthy for production installs.

Embedding tsrs in-process, instead of running a separate compiler process, eliminates IPC overhead and is a major reason for the memory and speed gains; this trend of avoiding subprocesses is likely to spread to other build toolchains.

The article shows that a JavaScript/TypeScript-based build pipeline deeply coupled to Node.js can be fully reimplemented in Rust, but only because ArkTS is a TS superset with strict syntax rules; the authors had to port 1,003 linter rules and 243 diagnostics to match exactly, a huge effort most tooling projects wouldn't attempt.

Hot rebuild performance is currently worse than Hvigor because the toolchain hasn't yet built a disk cache for the module graph; this is the classic tradeoff: a fresh Rust implementation can win on cold builds and memory, but incremental builds require good cache design.

Concepts & terms
ArkTS
HarmonyOS's application language, a strict superset of TypeScript with a declarative UI syntax (struct, @Component, @Builder) and ArkTS-specific lint rules.
HAP/HSP/HAR
HarmonyOS package formats: HAP (HarmonyOS Ability Package), HSP (HarmonyOS Shared Package), HAR (HarmonyOS Archive). They are analogous to Android's APK/AAR bundles.
ABC
Ark Bytecode, the compiled bytecode format that runs on the Ark compiler runtime in HarmonyOS.
Hvigor
The official HarmonyOS build tool, a Node.js-based task orchestrator using a modified TypeScript 4.9.5 compiler and Rollup.
ArkGuard
Source and bytecode obfuscator for HarmonyOS release builds.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗