目录

Performance/Memory Optimization Practices: Multi-Target Build

Overview

In multi-target projects, target configuration and dependency chains are two key factors affecting the build efficiency.

  1. Default build behavior of HAP/HSP targets: When targets are configured in the build-profile.json5 file for a HAP or an HSP module, if these targets are not explicitly declared under modules in the project-level build-profile.json5 file with applyToProducts (except for ohosTest), they are applied by default to the product named default. This may result in redundant packages being built during the default product build. For details, see Explicitly Configuring applyToProducts for Targets.
  2. HAR targets determined by dependents: The targets of a HAR module are determined by the modules that depend on it. When a HAR is depended on by modules with different targets, redundant build tasks may be generated. For details, see Configuring Identical Targets for the HAR Module and Its Dependents.

Both scenarios cause unnecessary build tasks to be executed, increasing the build time and memory usage. The following optimization solutions are recommended.

Explicitly Configuring applyToProducts for Targets

[h2]Incorrect Example

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.

[h2]Optimization Solution

When adding a target, explicitly set the applyToProducts field for the target.

  1. In the project-level build-profile.json5 file, explicitly specify the product to which each target applies using the applyToProducts field.
    {
      "modules": [
        {
          "name": "entry",
          "srcPath": "./entry",
          "targets": [
            {
              "name": "default",
              "applyToProducts": ["default"]
            },
            {
              "name": "test",
              "applyToProducts": ["test"]   // Explicitly specify that this target applies to the test product.
            }
          ]
        }
      ]
    }
  2. Add the test product under app.products in the project-level build-profile.json5 file.
    {
      "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.

Configuring Identical Targets for the HAR Module and Its Dependents

[h2]Incorrect Example

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.

[h2]Optimization Solution

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.