DEV Community

HarmonyOS Magician
HarmonyOS Magician

Posted on

Essential Guide to HarmonyOS 5 Development: In-Depth Understanding of the Cangjie Language Package Manager cjpm

For those of us in the development field, we're all too familiar with how frustrating dependency management and the build process can be. It's like trying to find the end of a tangled ball of yarn—one wrong move and you're completely lost. However, in HarmonyOS 5 development, the Cangjie language's package manager, cjpm, acts like a sharp pair of scissors, effortlessly cutting through this "tangle of yarn."

Today, let’s take a detailed look at just how remarkable cjpm really is.

Basic Knowledge of cjpm

In the past, during Android development, managing dependencies was nothing short of a nightmare. Version conflicts among libraries often led to compilation failures, and developers spent countless hours troubleshooting.

In contrast, the emergence of cjpm in HarmonyOS 5 development is a game-changer. Designed specifically for Cangjie language projects, it handles project-level compilation and building.

Analogy: Think of building a complex Lego castle (a project), with each Lego piece (dependency module) having different specifications. Without a proper organizer, everything gets chaotic.
cjpm is that thoughtful Lego organizer—it ensures every piece fits just right.

Using cjpm in HarmonyOS 5 is straightforward. For instance, to initialize a new project, simply run:

cjpm init
Enter fullscreen mode Exit fullscreen mode

This sets up the project structure, laying the foundation like prepping the Lego baseplate for your castle.

Exploring Automatic Dependency Management

Imagine this scenario:
You add Module A and Module B to your project.

  • Module A depends on Version 1.3 of Module C.
  • Module B depends on Version 1.4 of Module C.

In Android, this likely causes a compilation error, and you'd have to manually resolve it. But with cjpm, things are different.

cjpm features automatic dependency management—like having "wise eyes" that see through dependency trees and resolve them smartly.

With a simple command:

cjpm build
Enter fullscreen mode Exit fullscreen mode

It analyzes and merges all dependencies for you—no conflict resolution nightmares. Like a smart assistant organizing your Lego sets by type and color.

Example configuration (dependencies field):

{
  "dependencies": {
    "moduleA": "^1.0",
    "moduleB": "^2.0"
  }
}
Enter fullscreen mode Exit fullscreen mode

cjpm handles internal dependencies in the background, making development smooth and conflict-free.

The Power of Customized Building

Real-world projects often have specific build requirements:

  • Setting environment variables,
  • Compiling C/C++ code,
  • Copying third-party libraries,
  • Cleaning up after build.

In Android, this might need multiple tools. But in HarmonyOS 5, cjpm simplifies everything with custom build stages.

Pre-Build Example (Compile C code first)

In your build.cj:

func stagePreBuild(): Int64 {
    // Custom command to compile C files before the main build
    exec("cd {workspace}/cffi && cmake && make install")
    return 0
}
Enter fullscreen mode Exit fullscreen mode

Post-Build Example (Clean up after build)

func stagePostBuild(): Int64 {
    // Delete temporary CFFI source files
    exec("rm -rf {workspace}/cffi")
    return 0
}
Enter fullscreen mode Exit fullscreen mode

It's like customizing your Lego workflow: prepping special bricks before building and tidying up afterward.

Conclusion

In HarmonyOS 5 development, the automatic dependency management and custom build capabilities of the Cangjie language's package manager cjpm significantly enhance development efficiency.

It reduces repetitive tasks, avoids version conflicts, and offers flexible control over the build process—all with minimal effort.

So, give cjpm a try in your next HarmonyOS 5 project and unlock a smoother, smarter development experience.

Got questions or tips? Let’s share and discuss—maybe your next “aha” moment will ignite someone else’s development journey too!

Top comments (0)