目录

Referencing and Managing a Shared Package

You can reference third-party HARs or HSPs (third-party packages for short) from the ohpm repository, local folders, or local HAR or HSP files.

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

ohpm install

When installing or uninstalling a shared package, you can add hooks to the oh-package.json5 file of the project or target module to manage the lifecycle of the install and uninstall commands. The following is an example:

 "hooks": {
    "preInstall": "echo 00 preInstall", // Before the install command is executed
    "postInstall": "echo 00 postInstall", // After the install command is executed
    "preUninstall": "echo 00 preUninstall", // Before the uninstall command is executed
    "postUninstall": "echo 00 postUninstall"  // After the uninstall command is executed
  }
  • Currently, hooks in the oh-package.json5 file of the project or target module can be executed, but hooks in dependencies cannot.
  • Only the dependencies declared in dependencies of the oh-package.json5 file in the module or project directories are processed as dependencies during compilation and build.

Managing Third-Party Packages Using the OpenHarmony Third-Party Library Repository

Currently, this feature is available only in the Chinese mainland.

Since DevEco Studio 6.0.0 Beta5, the OHPM Index entry has been added, providing efficient filtering and management capabilities of the OpenHarmony Third-Party Library Repository. This improves developer productivity in package selection, reduces risks from information asymmetry, and enables quick discovery and positioning of required open-source third-party libraries.

  1. On the menu bar, choose Tools > OHPM Index.
  2. In the left search box, you can search for a shared package by name. Alternatively, browse the directory tree by category to view recommended dependencies. Once you have selected the desired shared package, click the blue Install button in the upper-right corner to install it.

  3. If a popup appears during installation, click the Add button. This adds the address of the OpenHarmony Third-Party Library Repository to your .ohpmrc file.

  4. After the shared package is installed successfully, you can find its name and version information in your project-level oh-package.json5 file. The package will also be added to your oh_modules directory.
  5. Click in the upper-left corner of the page to view a list of installed shared packages. If a package is not the latest version, you can click the Update button to update it, or click the Delete button to remove it.

  6. If recommended alternatives exist for your current third-party dependencies, click the yellow light bulb icon in the editor. In the dialog, choose Replace selected with recommended library to swap the current dependency, or Replace all with recommended libraries to replace all matching dependencies in the current file in one click.