In a project with multiple HAP/HSP modules, when you execute a build (Build Hap(s)), a separate compilation task is initiated for each module. Each compilation task performs syntax checking, conversion, and other compilation steps on the module's dependencies. If different HAPs/HSPs depend on the same HAR module, repeated compilation checking increases the build time and memory usage.
This topic describes several common optimization solutions to reduce the build time and memory usage in certain scenarios by specifying the build startup mode, adjusting module types, and enabling common configurations.
By selecting a proper build and run start mode, you can avoid compiling all HAPs/HSPs and only compile the affected modules and their dependencies.
After modifying a single module, you can build it using either Build Hap(s) or Make Module. The differences between the two are as follows.
Start Mode | Behavior | Application Scenarios | Resource Consumption |
|---|---|---|---|
Build Hap(s) | All HAPs and their dependencies are compiled and packaged. | When you need to generate all HAP/HSP installation packages for device installation. | High (compiling all HAPs) |
Make Module | Only the current module and its dependencies are compiled and packaged. | When only a single module is modified and you need to quickly verify the logic. | Low (compiling a single module) |
Therefore, after modifying a single module, you are advised to build it using Make Module. This reduces the number of installation packages compiled per modification in multi-HAP/multi-HSP scenarios, thereby reducing the build time and memory usage.

Starting the build by pushing and running the package can the reduce build time and memory usage in certain scenarios. For example, when frequently making incremental changes to an HSP module without modifying its exported APIs, prioritize running this HSP module. This avoids recompiling and repackaging other unchanged HAP and HSP modules.

HAPs and HSPs, as well as different HSPs, may depend on the same HARs, resulting in redundant build and increased package size. If the modules do not require on-demand loading, converting HSPs to HARs can reduce the build time and memory usage, and shrink the package size.


Symptom
In a single-HAP project structure, after multiple HSPs are converted to HARs, their resource files are merged into the entry module. If different HSP modules contain resources with the same name, the overwrite logic is applied based on the order of global dependency configuration, which may result in resource loss or errors.
Solution
Standardize resource names by adding unique identifiers to resource files in each module.

Symptom
If a Worker thread is used in an HSP module, after HSP-to-HAR conversion, the Worker loading path rules may change due to the module type transition.
Solution
Adjust the Worker loading mode by referring to File URL Rules in Stage Model.
Additionally, there are some configurations that can further reduce the build time and memory usage, improving the overall development experience. Add the following fields to the hvigor-config.json5 file. For details about the fields, see Experimental Features.
"properties": {
"hvigor.task.schedule.optimization": true, // Enable task scheduling optimization, which prioritizes execution of CompileArkTs and its dependent tasks to start the CompileArkTs task earlier and reduce the build time.
"ohos.arkCompile.noEmitJs": true, // Optimize intermediate compilation product generation. After optimization, no intermediate JavaScript code is generated during ArkTS compilation, reducing the compilation time and memory usage.
"ohos.arkCompile.singleFileEmit": true // Optimize compilation product writing. After optimization, when an individual file is parsed during ArkTS compilation, it is written to the disk, reducing the peak memory usage.
}