π 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
}
}
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
}
}
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"
}
}
Optimization Plan 2 (OHPM β₯1.5.0): Enable automatic conflict resolution, defaulting to the highest version:
ohpm install --resolve_conflict
β³ 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();
});
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
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
-
Icon Optimization:
- Use SVG instead of PNG (smaller size, lossless scaling)
- Use HarmonyOS built-in icon library to reduce bundled resource files.
- Resource Obfuscation:
// Enable resource obfuscation in build-profile.json5
"buildTasks": ["resource_obfuscation"]
- 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)