DEV Community

HarmonyOS
HarmonyOS

Posted on

🔧 Writing and Publishing Your Own Third-Party Library for HarmonyOS Next

Read the original article:🔧 Writing and Publishing Your Own Third-Party Library for HarmonyOS Next

✨Introduction

In this article, we will explain how to create and share an OHPM package. This package functions like a third-party library. OHPM serves as a storage space for third-party packages designed for HarmonyOS, allowing developers to easily access and share libraries from various sources around the world.

📚Developing and Publishing a Third-Party Library

In HarmonyOS Next, third-party libraries are shared through HAR packages, which stand for Harmony Archive. These packages serve as versatile static shared resources that can contain code, C++ libraries, resources, and configuration files. By using HAR packages, developers can share code for ArkUI components and other resources across various modules and projects, promoting better collaboration and efficiency in development.

🚀Create a Project

To start developing a HAR package, we first need to create a project. Open DevEco Studio, then click on File > New > Create Project from the top menu as shown below.

You can simply choose "Empty Ability" from the options here.

🗂️ Create and Develop a HAR Module

Once the project has been created, we need to develop a module for HAR. First, ensure that the project has been synchronized and built. Since we just created the project, it may be disabled at this stage. After the synchronization and build are complete, right-click on the project name, select "New," and then choose "Module."

In the open window, select "Static Library," which corresponds to HAR.

Once the configurations are complete, you should see a folder named after your module. In this case, I kept the default name "library”. We will develop our library in this folder.

In the root directory of the library folder, there is a file named Index.ets, which is created by default. In this file, we need to export the contents developed in the src/main/ets/ directory. This file serves as the entry point for the HAR export declaration and contains the HAR export APIs.

💡If you’re looking for more information on how to develop an HAR, feel free to check out the documentation!

📢Prepare the HAR Package for Publication

To publish the HAR package we created, we need to add and configure several files: CHANGELOG.md, LICENSE, and README.md. Additionally, we must configure the oh-package.json5.

⚠️ Just a quick note: all the files I talk about here will be created and set up right in the root directory of the library module.

The first file we need to configure is the oh-package.json5 file. Below is a sample of the oh-package.json5 file that I created during this article. You are advised to pay attention to the comments I wrote.

{
  "types": "",
  "keywords": [
    "{keyword}", // You can define keywords here.
    "OpenHarmony",
    "HarmonyOS"
  ],
  "author": "username", // If you are in an Organization Group, 
  // Put Organization Name That Will Publish
  "description": "Well defined but not long description",
  "main": "Index.ets",
  "type": "module",
  "repository": "You can give remote repository link here ",
  "version": "1.0.0", // You can indicate a version
  "tags": [
    "{ tags }", // You can define tags here.
    "OpenHarmony",
    "HarmonyOS"
  ],
  "dependencies": {},
  "license": "Apache License 2.0", // Apache OpenSource License
  "devDependencies": {},
  "name": "package_name"
}
Enter fullscreen mode Exit fullscreen mode
🤔If you wonder what this file does, you can refer to this documentationfor further information!

When naming your third-party libraries, keep these helpful guidelines in mind! First, each library name must be unique worldwide. You can format it as either @group/packagename or package_name. Just ensure you follow some basic character and structure rules to avoid using reserved ArkTS keywords or ending with any problematic characters.

💡You can check out the detailed rules and constraints right here!

Our second required file is README.md. To help the community find our third-party library, it is essential to create a well-described README.md.

👀If you want to write a good README.md file, you can check other third-party libraries at the OpenHarmony Third-Party Library Repositories.

In my view, these are the typical sections commonly found in a README.md file. You can use this structure as a guideline; however, since there is no strict standard, feel free to modify it to improve it based on your preferences.

Our third file is CHANGELOG.md, which tracks changes by version of the library. You can create CHANGELOG.md as shown below.

Our final file is the LICENSE. Since we will be publishing our library, it’s important to include a license that reflects our commitment to open-source principles. Don't worry; it's very easy to do. Simply visit the provided link, copy the license text, replace [yyyy] and [name of copyright owner] with the appropriate information, and you’re all set!

📦Build HAR Package

After creating the necessary files, it’s time to build our module and generate a HAR package. We’ll be publishing this package to the OpenHarmony 3rd Party Repositories Center that we set up earlier. To proceed, simply click Build > Make Module '{your static library module name}'.

Then, DevEco Studio will generate an output file with the .har extension. You can find it at: build > default > outputs > {static package name}.har, as shown below.

If you get any errors while building, feel free to exaggerate.

Create an OpenHarmony Central Profile

At this step, we need to create an OpenHarmony Central profile. If you already have an account, you can skip this step. If not, no worries — let’s go through it step by step 🪜:

  1. Open OpenHarmony Central
  2. Click the Sign Up button
  3. After clicking, you'll be redirected to the registration page

There are a lot of login and signup methods, as you can see. I suggest using GitCode to log in. I found it easier and more convenient than the others.​​​​​​

After you've signed in, return to Central again. Now, you should see your profile icon at the top right of the screen. Click on it, then select Profile from the dropdown menu.

🔒Create and Add SSH Keygen to Central

To be able to publish the HAP package, we first need to add our generated SSH key here. Don’t worry, I’m going to show you exactly how to generate one.

First, go to the Security Management tab and open the screen shown below.

To generate ssh-keygen, we need to return to DevEco Studio. If you’ve closed it, go ahead and open your project again. Then, click the Terminal to open a terminal in the project folder.

👌You can open a terminal by pressing Alt + F12 or by clicking the terminal tab at the bottom.

Now, run the following command to generate an SSH key:,

ssh-keygen -m PEM -t RSA -b 4096 -f {filepath of project}\{ssh-keygen file name}

Enter fullscreen mode Exit fullscreen mode

You can see the example below 👇. Please, don't mind the blurred parts

⚠️You need to enter a passphrase during the SSH key generation when prompted. OpenHarmony Central does not accept SSH keys that are generated without a passphrase, so make sure to set.

Now, as you see in the image at the top, it says {ssh-keygen file name}.pub has been saved. Go to its directory, open the file with your default text editor, select all the content, and copy it entirely.

Click the “Add” button ➕ on the Security Management page.

And add a public key like this. You can give it any title you like. Then, don't forget to save.


This part is also done, but not finished yet. We have one final part before publishing.

🚀Last Configurations and Publication

Now, before we finish and close both DevEco Studio and the OpenHarmony Central page, we need to do the final configurations 🔧.

In the Profile screen at OpenHarmony Central, you will see the Copy publish_id button at the top left. Click on it, then you will get your publish_id as shown below.

First, before we use the publish ID, let’s configure our key for publishing. Run the following command to set the keygen file we generated earlier:

ohpm config set key_path {filepath of project}\{ssh-keygen file name}.pub
Enter fullscreen mode Exit fullscreen mode

Secondly, we need to set our publish_id. Use the publish_id you copied from OpenHarmony Central here:

ohpm config set publish_id {publish_id}

Enter fullscreen mode Exit fullscreen mode

Thirdly, we must set the publish registry. Run the following command to configure it.

🧐If you try to open the link below, it won’t open. Don’t worry, it’s not wrong.
ohpm config set publish_registry https://ohpm.openharmony.cn/ohpm
Enter fullscreen mode Exit fullscreen mode

Finally, we are ready to publish our library. Run the following command (don’t forget to replace the file path with yours) to send your package for review:

ohpm publish .\library\build\default\outputs\default\library.har \\ package filepath

Enter fullscreen mode Exit fullscreen mode

Then, you should see your result in the terminal. You can check your review status from the Package tab and track updates or feedback in the Messages section️.

That’s it. We are done!
After the review is completed and your package is published, you can search for your library on OpenHarmony Central to see it live.

🏁 Conclusion

TL;DR ✍️
In this article, we learned how to publish an OHPM Package (HAP), which is a third-party library. I encourage everyone to publish their libraries to help the community, especially where you see room for improvement or gaps.

Remember, without the vast experience, knowledge bases, and know-how shared before us, starting development would have been much harder. Thanks to the amazing community and you, the generous developers, I believe the HarmonyOS Next ecosystem will continue to grow stronger every day.

🔗References

DevEco Studio - HUAWEI Developers

oh-package.json5-ohpm-Command Line Tools - HUAWEI Developers

HAR-Application Package Development and Usage-Application Package Fundamentals-Development Fundamentals-Getting Started - HUAWEI Developers

Developing a Static Shared Package-Developing and Referencing Shared Packages-Developing Your App/Atomic Service-DevEco Studio - HUAWEI Developers

Written by Muaz Kartal

Top comments (0)