Read the original article:How to Optimize Package Size Of Your HarmonyOS Next Apps
Problem Description
How to optimize the application package size?
Background Knowladge
Reducing the size of an application package is an important way to improve download and installation experience. By compressing, streamlining, or reusing the code and resources in the application, you can effectively reduce the package size, saving space and accelerating both download and install speeds.
Before optimizing, it is necessary to understand the structure of HarmonyOS application packages. To analyze package size issues, scanning tools can be used to inspect the app package. Based on the generated reports, you can take specific actions to optimize it.
Troubleshooting Progress
- Scanned the HarmonyOS application package using the provided analysis tools to identify large files and duplicate resources.
- Detected that .so libraries were not compressed by default during packaging.
- Found multiple HAR dependencies of different versions being included, increasing redundancy.
- Observed that some rarely used features were bundled directly into the base package instead of being delivered on-demand.
- Applied the following actions:
- Enabled
compressNativeLibsoption inmodule.json5. - Replaced redundant HAR dependencies using override mechanism in
oh-package.json5. - Migrated common code/resources to HSP for sharing across modules.
- Converted infrequently used features into on-demand modules.
- Enabled
Analysis Conclusion
- The excessive application size was primarily due to uncompressed native libraries, duplicate dependencies, and unused features bundled in the base package.
- By enabling compression, consolidating dependencies, and using modular strategies (HSP + on-demand delivery), the package structure became more efficient.
- These optimizations reduced overall package size, improved download speed, and enhanced installation performance without affecting core functionality.
Solution
The main methods to optimize app package size are:
-
Compress
.soLibraries:
For apps with native libraries, you can configure compression for .so libraries. By default, DevEco Studio does not compress them when packaging. Enabling compression will package them in compressed format, reducing size.Configuration: In module.json5, set "compressNativeLibs": true and rebuild the app.
{
"module": {
// ...
"compressNativeLibs": true
}
}
- Use HSP for Multi-Package Apps: When using multiple packages (HAP, HSP), use HSP (dynamic shared packages) to share code and resources across modules. This avoids duplication caused by HAR (static shared packages).
Refer to: Using HSP to Share Code and Resources in Multi-Package Scenarios
-
Resolve Dependency Conflicts with
overrideorresolve_conflict: In OHPM versions before 1.5.0, if multiple HAPs depend on different versions of the same HAR, both versions may be packaged. Use theoverridemechanism inoh-package.json5to specify which version to keep.
Example: Replace a dependency with another version or local HAR directory.
- Load Infrequently Used Features On-Demand: For features not commonly used, implement them as on-demand modules.
Refer to: On-Demand Delivery
Verification Result
- After implementing the optimizations, the package was rebuilt and scanned again.
-
Results showed:
- 20–30% reduction in total package size (depending on configuration).
- No duplicate HAR libraries included.
- Native
.solibraries successfully compressed. - On-demand modules correctly delivered and loaded only when triggered.
Application passed installation and runtime verification, confirming functionality remained intact.
Top comments (0)