In multi-target projects, target configuration and dependency chains are two key factors affecting the build efficiency.
Both scenarios cause unnecessary build tasks to be executed, increasing the build time and memory usage. The following optimization solutions are recommended.
During build command execution, if no target is specified for a module, the target used for compilation is determined by product.
For example, the default and test targets are configured in the build-profile.json5 file of the entry module, but applyToProducts of the test target is not configured in the project-level build-profile.json5 file.
{
"targets": [
{ "name": "default" },
{ "name": "test" },
]
} Run the following command without specifying the target:
hvigorw assembleHap -p product=default
The default target is applied to the default product by default, and applyToProducts of the test target is not explicitly specified in the project-level build-profile.json5 file. As a result, the test target is also applied to the default product by default, causing both targets to be compiled.

When adding a target, explicitly set the applyToProducts field for the target.
{
"modules": [
{
"name": "entry",
"srcPath": "./entry",
"targets": [
{
"name": "default",
"applyToProducts": ["default"]
},
{
"name": "test",
"applyToProducts": ["test"] // Explicitly specify that this target applies to the test product.
}
]
}
]
} {
"app": {
"products": [
{ "name": "default", },
{ "name": "test", } // Add the test product.
]
}
} In this case, when you run the hvigorw assembleHap -p product=default command, only the default target is involved in the build, while the test target is excluded.
When a HAR module is depended on by multiple modules with different targets, redundant build tasks may be generated.
Take the following dependency relationship as an example:
entry(default,default2) → har1(default) → har2(default,default2) // The targets of har1 are not completely the same as those of the entry module. entry(default,default2) → har2(default,default2)
Run the following command:
hvigorw assembleHap -p product=default -p module=entry@default2
Since the targets of a HAR module are determined by the modules that depend on it, the actual targets participating in the build are:
entry(default2) → har1(default) → har2(default) entry(default2) → har2(default2)
As shown above, two targets of har2 (default and default2) take part in the build. Since entry(default2) directly depends on har2(default2), only har2(default2) is packaged into the final output, and har2(default) is excluded from packaging. However, before packaging, tasks related to har2(default) (such as har2:default@PreBuild) are also executed. These are redundant tasks and introduce unnecessary build-time overhead.

You are advised to configure identical targets for the HAR module and its dependents. Taking the preceding example, add the default2 target to har1.
entry(default,default2) → har1(default,default2) → har2(default,default2) entry(default,default2) → har2(default,default2)
In this way, the HAR module can provide the corresponding target regardless of which target is used by the dependent, avoiding redundant build tasks.