DEV Community

ZHZL-m
ZHZL-m

Posted on

【Journey of HarmonyOS Next】DevEco Studio User Guide (22)

Image description

1 -> Develop static shared packages

A HAR (Harmony Archive) is a static shared package that can contain code, C++ libraries, resources, and configuration files. You can use HAR to share ArkUI components, resources, and other related code with multiple modules or projects. Unlike HAP, HAR cannot be installed and run on a device independently, but can only be referenced as a dependency of an application module.

Next, we will briefly introduce the engineering structure of the library module, as shown in the following figure:

Image description

The relevant fields are described below.

libs: used to store .so files.
src > main > cpp > types: used to store C++ API description files, and the subdirectories are divided by so dimension.
src > main > cpp > types > liblibrary > Index.d.ts: describes the method name, input parameters, and return parameters of the C++ interface.
src > main > cpp > types > liblibrary > oh-package.json5: describes the entry of the so third-party package declaration file and the so package name.
src > main > cpp > CMakeLists.txt:CMake configuration file, which provides CMake build scripts.
src > main > cpp > napi_init.cpp: Shared package C++ code source files.
Index.ets: the entry point for the export declaration of shared packages.
This section will cover how to create library modules, compile shared packages, reference shared package resources, and publish shared packages.

1.1 -> Create a library module

  1. Move the mouse to the top of the project directory, right-click, select New > Module, and add a module to the project.

  2. In the Choose Your Ability Template page, select Static Library and click Next.

Image description

  1. On the Configure New Module page, set the information of the newly added module, and click Finish to complete the creation.

Module name: the name of the new module.
Device type: The supported device type.
Enable native: Whether to create a module for invoking C++ code.

Image description

After the creation is complete, the library module and related files are generated in the project directory.

Image description

1.2 -> Compile library modules

After developing the library module, select the module name, and then compile and build the HAR by running the Build > Make Module ${libraryName} in the DevEco Studio menu bar. The HAR can be used to reference other modules in the project, or to upload the HAR to the ohpm repository for download. If some source code files do not need to be packaged into the HAR, you can create a .ohpmignore file to configure the files/folders to be ignored during packaging.

Image description

The compiled and built HAR can be obtained in the build directory of the module, and the package format is *.har.

Image description

When compiling and building the HAR, keep the following in mind:

During the compilation and construction of the HAR, the C++ code in the module is not directly packaged into the .har file, but the C++ code is compiled into a dynamic dependency library .so file and placed in the libs directory of the .har file.
During the process of compiling and building the HAR, a resource file ResourceTable.txt is generated so that the editor can associate the resource files in the HAR. Therefore, if you do not use DevEco Studio to build the HAR, the editor of DevEco Studio will not be able to associate the resources in the HAR.
If you are using Hvigor 2.5.0-s or later, the local dependencies in the dependencies that are in the path of this module will be packaged into the .har file during the compilation and construction of the HAR.

2 -> Develop dynamic shared packages

DevEco Studio supports the development of dynamic Harmony Shared Package (HSP). HSPs can be used to implement some functions that can be dynamically downloaded on demand during application/meta-service development, or if they need to be subcontracted and loaded during the development of meta-service scenarios. When multiple installation packages need to be shared, HSP can also be used to reduce the duplication of common resources and code.

illustrate

In-app HSP: It is strongly coupled to the application package name during the compilation process and can only be used by a specific application.
Integrated HSP: During the build and release process, it is not coupled with a specific application package name. When used, the toolchain can automatically replace the package name of the integrated HSP with the package name of the host application.

2.1 -> Use Constraints

The HSP and its users must be Stage models of API 10 or later.
Both the HSP and its users must use the modular compilation model.

2.2 -> Develop dynamic shared packages

2.2.1 -> Create an HSP module

  1. Add a new module to the project in the following two ways:

Method 1: Move the mouse over the top of the project directory, right-click, and select New > Module to start creating a new Module.
Method 2: Select any file in the project directory and select File > New > Module in the menu bar to start creating a new Module.

  1. Select Shared Library as the template type and click Next.

Image description

  1. On the Configure New Module page, set the information of the newly added module, and click Finish to complete the creation.

Module name: the name of the added module, for example, sharedlibrary.
Device type: The supported device type.
Enable native: Whether to create a module for invoking C++ code.

Image description

After the creation is complete, the library module and related files are generated in the project directory.

Image description

2.2.2 -> Compile HSP modules

illustrate

If obfuscation is not enabled for HSPs, HSP packets will no longer be obfuscated when HSPs are integrated and used.

After developing the library module, select the module name, and then run the Build > Make Module ${libraryName} in the DevEco Studio menu bar to compile and build the HSP.

Image description

When the HSP is packaged, the HAR is packaged by default, and you can see *.har and *.hsp in the build directory of the module.

Image description

If you want to share HSPs in an application, upload the HSP package to a private database, and perform the following steps to compile and generate a *.tgz package:

  1. Click the toolbar icon to switch the compilation mode to release mode.

Image description

  1. Select the root directory of the HSP module and click Build > Make Module ${libraryName} to start the build.

Image description

After the build is complete, the HSP package is generated in the build directory, and the .tgz is used to upload the product to the private database.

Image description

3 -> Publish the shared package

Publish a packaged HAR that can be installed and referenced. Next, we'll show you how to publish a HAR shared package.

illustrate

The OpenHarmony third-party repository supports only HAR shared package publishing, but does not support HSP shared package publishing. If you want to share HSPs within an application, you can publish the HSP shared package to a private database.

  1. In the library module (in the same level directory as the src folder), add the following files:

Create a new README.md file: The README.md file must include the description and reference of the package, and you can add more details based on the content of the package.
Create a CHANGELOG.md file: Enter the version update record of the HAR.
ADD A LICENSE FILE: LICENSE FILE.

  1. Recompile the library module and generate a *.har file.

  2. Use the tool ssh-keygen to generate public and private keys, and run the following commands:

ssh-keygen -m PEM -t RSA -b 4096 -f ~/.ssh_ohpm/mykey 
Enter fullscreen mode Exit fullscreen mode

illustrate

~/.ssh_ohpm/mykey is the file path of the mykey file of the private key file, which is specified as required. The specified private key storage directory must exist.
The public key file with the .pub suffix appended to it will be stored in the same directory as the private key.
The OHPM package manager only supports encryption key authentication, so enter the password when generating the public and private keys.

  1. Log in to the official website of OpenHarmony Third-Party Library, click Personal Center in the upper right corner of the homepage, add the OHPM public key, and paste the contents of the public key file (mykey.pub) into the public key input box.

Image description

  1. Open the command line tool and configure the path of the corresponding private key file to the key_path field in the .ohpmrc file, and run the following command to configure it:
ohpm config set key_path  ~/.ssh_ohpm/mykey
Enter fullscreen mode Exit fullscreen mode
  1. Log in to the official website of OpenHarmony Third-Party Library Center, click Personal Center in the upper right corner of the homepage, copy the release code, obtain the release code, and configure it in the .ohpmrc file, run the following commands:
ohpm config set publish_id your_publish_id
Enter fullscreen mode Exit fullscreen mode
  1. Run the following command to publish the HAR < the HAR path > specify the specific path of the .har file:
ohpm publish <HAR路径>  
Enter fullscreen mode Exit fullscreen mode

Top comments (0)