Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
A smaller app package size can improve app download and installation experience. By compressing, simplifying, or reusing code or resources, you can effectively minimize an app package size. Before reducing the package size, you need to understand the app package structure in stage model of a HarmonyOS app and the features of the HAP, HAR, and HSP files. And then, you can use a scanning tool to scan the package and optimize the app based on the output report. The following provides some optimization methods:
The app check tool is used to analyze and detect app packages. Based on the parameter settings, it scans the App Pack, HAP, or HSP file in the specified path and generates detection reports, providing data support for you to optimize the package structure or identify problems.
Based on the scanning result, optimize the app:
2. Large files
3. Files of a specific type
You can configure .so library compression options to compress and package .so files.
By default, DevEco Studio does not compress .so library files during app packaging. After the .so compression options are configured, DevEco Studio compresses .so library files into a package to reduce the app package size.
Configuration Method
Set the compressNativeLibs field to true in the module.json5 configuration file of the app module, and recompile and package the app.
- {
- "module": {
- // ...
- "compressNativeLibs": true // Identify whether the so library is packaged in compressed storage. 'true' means compressed so library, 'false' means non-compressed.
- }
- }
Compression Effect
The following uses the default C++ library file in DevEco Studio as an example. The file sizes before and after compression are compared as follows.
File | Original Size | Size After Compression | Compression Ratio |
|---|---|---|---|
armeabi-v7a/libc++_shared.so | 1,108 KB | 386 KB | 34% |
In versions earlier than ohpm 1.5.0, if a HAP depends on different versions of HAR (for example, harC of V1 and harC of V2 in the following figure), the harCs of V1 and V2 are packed into the HAP package by default. You can use the override mechanism of the ohpm to specify that only one version is packaged.

If you are using ohpm 1.4.0, you can use the override mechanism. You can add the overrides configuration to the project-level oh-package.json5 file (that is, oh-package.json5 in the root directory of the project). Replace the dependency package in the dependency tree with another version. The version to be replaced can be a specific version number, a local HAR package, or source code directory.
Note:
overrides must be configured in the project-level oh-package.json5 file. The configuration does not take effect in the module-level oh-package.json5 file.
The foo library is used as an example. If the 1.0.0 version is always required in a project, add the following configuration to the project-level oh-package.json5 file:
- {
- "overrides": {
- "foo": "1.0.0"
- }
- }
If the foo source code or HAR package exists on the local host, you can configure the following information in the project-level oh-package.json5 file to ensure that foo always uses your local version:
1. The source code for foo exists locally, located in the foo directory at the root of the project. Its path is specified as file:./foo.
- {
- "overrides": {
- "foo": "file:./foo"
- }
- }
2. The foo.har package exists locally and is stored in the libs directory. Its path is specified using file:./libs/foo.har.
- {
- "overrides": {
- "foo": "file:./libs/foo.har"
- }
- }
For ohpm later than 1.5.0, you can enable resolve_conflict to automatically resolve dependency conflicts. If your project depends on different versions of a third-party library, ohpm selects the latest version for installation, avoiding possible version conflicts.
For functions that are not frequently used in an app, you can use the on-demand feature distribution method to allow users to select the download time. When using the functions, users can obtain and install them from HUAWEI AppGallery, which reduces the size of the package downloaded by users for the first time.
Currently, the system provides two types of sharing packages: HAR and HSP. Both HAR and HSP are used to share code and resources and can contain code, C++ libraries, resources, and configuration files. The code and resources in the HAR are built with the invoking module, and if there are multiple invoking modules, the build product contains multiple copies of the same code and resources. The code and resources in the HSP are built independently, and the build product contains only one copy of the code and resources.
In the multi-package scenario, if multiple HAP or HSP files of an app use the HAR package to share code and resources, each packaged HAP or HSP file contains a copy of the shared HAR package. As a result, the App Pack contains redundant code and resources. As shown in the following figure, both the app modules HAP1 and HAP2/HSP1 reference HAR2 and HAR3. Once packaged, HAR2 and HAR3 in the App Pack have multiple duplicate copies which are large in size.

It is recommended that you use HSP instead of HAR to share code and resources. As shown in the following figure, HSP2 is used to upgrade and reconstruct the original app. After packaging, only one copy of HAR2 and HAR3 exists in the app package. If the total size of HAR2 and HAR3 is greater than HSP, you can reduce the app package size.
