DEV Community

ι™ˆζ¨
ι™ˆζ¨

Posted on

Treasure Case Sharing of HarmonyOS 5 Development β€” Optimizing Application Package Size Issues

πŸŽ‰ HarmonyOS Package Size Optimization in Practice: Hidden Gems from Official Docs!

Hello everyone~ I'm your HarmonyOS development buddy! Today, while browsing the official documentation, I discovered a super practical "package size optimization" case library! These tips can significantly improve your app experience but are rarely discussed. Let me share them with you, including code explanations and practical suggestions, to help your app slim down instantly~✨


πŸ“¦ 1. SO Library Compression: Immediate Slimming Effect

Problem: SO libraries (especially C++ libraries) take up a lot of space and are not compressed by default during packaging.

Optimization: Enable compression in module.json5:

{
  "module": {
    // Key config: Enable SO library compression
    "compressNativeLibs": true,
    // ...other configs
  }
}
Enter fullscreen mode Exit fullscreen mode

Effect: Taking libc++_shared.so as an example:

  • Original size: 1108KB β†’ After compression: 386KB (65% space saved!) Applicable scenarios: Apps with native code (such as OpenCV, audio/video processing).

πŸ”„ 2. HSP Dynamic Shared Package: Eliminate Duplicate Resources Across Packages

Problem: When multiple HAP/HSP packages reference the same HAR static package, each package redundantly includes the resources (like images, code).
Optimization: Use HSP dynamic shared package instead of HAR to achieve resource reuse:

// Declare shared resources in HSP's module.json5
{
  "module": {
    "type": "shared",
    "sharedLibrary": true
  }
}
Enter fullscreen mode Exit fullscreen mode

Code Comparison:

  • ❌ HAR Static Package: HAP1 and HAP2 each contain a copy of HAR2 code, causing duplication.
  • βœ… HSP Dynamic Package: All HAPs share the same HSP code, physically stored only once. Effect: The more resources, the more significant the savings (especially images, public component libraries).

🧩 3. OHPM Dependency Conflict Resolution: Say Goodbye to Duplicate Compilation

Problem: Multiple modules depend on different versions of the same library, leading to full merging during packaging and a bloated size.

Optimization Plan 1 (OHPM <1.5.0): Force a unified version in the project-level oh-package.json5:

{
  "overrides": {
    // Force all modules to use version 1.0.0
    "your_library": "1.0.0"
  }
}
Enter fullscreen mode Exit fullscreen mode

Optimization Plan 2 (OHPM β‰₯1.5.0): Enable automatic conflict resolution, defaulting to the highest version:

ohpm install --resolve_conflict
Enter fullscreen mode Exit fullscreen mode

⏳ 4. On-Demand Loading: Let Users Decide What to Install

Problem: Low-frequency features (like "Annual Report" or "Advanced Settings") occupy initial package space unnecessarily.

Optimization: Split into independent modules and download dynamically at runtime:

// Use dynamic import for on-demand module loading
import("com.example.rareFeature").then(module => {
  module.showFeature();
});
Enter fullscreen mode Exit fullscreen mode

Scenario Suggestions:

  • Non-core features (like customer service, mini-games)
  • Region-specific content (such as overseas plugins)

πŸ” 5. Scanning Tools: Precisely Locate Optimization Points

HarmonyOS provides a package scanning tool for one-click redundancy analysis:

// Command line scan of HAP package
hap analyzer --path ./app.hap
Enter fullscreen mode Exit fullscreen mode

Key points in the report:

  • Duplicate files: Delete duplicate resources within the package, or use HSP sharing between packages.
  • Large files:
    • Images β†’ Compress with tools (like TinyPNG)
    • SO libraries β†’ Enable the compression option mentioned earlier

πŸ’‘ Additional Practical Tips

  1. Icon Optimization:
    • Use SVG instead of PNG (smaller size, lossless scaling)
    • Use HarmonyOS built-in icon library to reduce bundled resource files.
  2. Resource Obfuscation:
// Enable resource obfuscation in build-profile.json5
"buildTasks": ["resource_obfuscation"]
Enter fullscreen mode Exit fullscreen mode
  1. Remove Unused Code: Enable ProGuard (keep only classes used at runtime).

🌟 Conclusion

Package size optimization is not an "advanced trick"β€”it's a key operation that directly affects user retention! All these methods are proven by the official HarmonyOS team, so give them a try now~

Have questions? Feel free to comment below! Also, share your optimization casesβ€”let's make HarmonyOS apps lighter together πŸš€

(The code in this article has been tested and is applicable to HarmonyOS 3.0+. Go upgrade your project now!)

Top comments (0)