目录

Adding Dependencies

Apps/Atomic services support installation, sharing, and code distribution, and project dependency management through ohpm, a package manager. This section describes how to configure dependencies for your project, as well as the processing logic and compilation results of different configuration modes during compilation.

To specify the dependencies for your project or module, you must list them under the dependencies (production dependencies) or devDependencies (development dependencies) field in the oh-package.json5 file of the project or module. The dependencies can be any of the following types: remote third-party package, local module, and local HAR/HSP. If an HSP needs to be loaded or executed only under certain conditions, add it under the dynamicDependencies field in the oh-package.json5 file. The following uses dependencies as an example. For details about dependency settings, see oh-package.json5.

Configuring Dependencies

[h2]Referencing a Remote Third-Party Package

Set the package as a dependency in the oh-package.json5 file of the target module. The following is a configuration example:
"dependencies": {
  "@ohos/lottie": "^2.0.0"
}

[h2]Local Module

You can set the dependency on a local module in the oh-package.json5 file of the module in either of the following ways:

[h2]Referencing a Local HAR or HSP

After the dependency is set, run the ohpm install command to install it to the oh_modules directory of the target module.

Compilation Behavior Differences

You can specify dependencies in the following fields based on the current dependency configuration specifications:

Although all the preceding fields can be used to specify dependencies, their processing logic and compilation results differ depending on the field used. For example:

  1. Create a project and a HAR module.
  2. In dependencies of the entry module-level oh-package.json5 file, set the local HAR folder path as a dependency, then compile the entry module.
    "dependencies": {
      "har": "file:../har"
    }
  3. In devDependencies of the entry module-level oh-package.json5 file, set the local HAR folder path as a dependency, then compile the entry module.
    "devDependencies": {
      "har": "file:../har"
    }

Compare the build logs of steps 2 and 3. You will find that step 2 prints HAR-related logs (as shown in the red box below), while step 3 does not print any HAR-related logs.

The differences above are merely log differences. To ensure normal compilation, you are not advised to configure dependencies in the project-level oh-package.json5 file. The specific compilation processing logic and potential abnormal results will be illustrated in the table below.

Terms are defined as follows and used only in this document:

  • In the table below, devDependencies refers to devDependencies in both the project-level and module-level oh-package.json5 files.
  • This table only explains scenarios that may lead to compilation abnormalities and does not cover scenarios with normal compilation (for example, configuring dependencies in the module-level oh-package.json5 file).
Table 1 Compilation behavior differences

Dependency

Compiling HAPs

Compiling HSPs

Compiling Bytecode HARs with bundledDependencies Disabled

Compiling Bytecode HARs with bundledDependencies Enabled

Source code HAR

Third-party package dependency

devDependencies

1

1

2

1

Project-level dependencies

/

/

3

3

Local dependency

devDependencies

1

1

2

1

Project-level dependencies

/

1

2

1

Bytecode HAR

Third-party package dependency

devDependencies

2

2

2

2

Project-level dependencies

/

/

3

3

HSP

Third-party package dependency

devDependencies

1

1

2

1

Project-level dependencies and dynamicDependencies

/

/

/

/

Local dependency

devDependencies

1

1

2

1

Project-level dependencies and dynamicDependencies

/

1

2

1

The meanings of the symbols in the table are as follows:

To optimize the scenarios marked with bold symbols (1, 2, or 3) in the table, since DevEco Studio 5.1.1 Beta1, the ohos.byteCodeHar.integratedOptimization field has been available in the hvigor-config.json5 file. Enabling this field optimizes the underlying dependency collection logic for these scenarios.

While this feature improves dependency collection, it does not optimize the logic for collecting the dependency configuration data. Consequently, in the scenarios marked with bold symbols, full optimization (with no compilation or runtime exceptions guaranteed) is only achievable under both of the following conditions: (1) The dependency in question is a compiled bytecode HAR. (2) The dependency is explicitly declared in the project-level dependencies or dynamicDependencies configuration. For any bold-marked scenario not meeting both of these conditions, compilation or runtime exceptions may occur.

"properties": {
  "ohos.byteCodeHar.integratedOptimization": true
}

This field must be configured for both the dependency package provider and integrator.

Based on the above, you are advised to: