We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
Best PracticesPerformance Performance Optimization CasesOptimizing Resource and StorageReducing Application Package Size

Reducing Application Package Size

Overview

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:

  1. If an app project contains the .so library, you can configure .so compression options to reduce the app package size.
  2. If an app contains multiple packages (HAPs and HSPs), you can use the dynamic shared package (HSP) to share code and resources among HAPs and HSPs, which eliminates repeatedly copying codes and resources resulting from the use of the static shared package (HAR). In this way, you can reduce the size of the app package. You also need to evaluate the impact on the compilation performance. If a large number of HSPs are used to replace HARs, more language compilation tasks are triggered when modules that reference these HSPs are compiled. As a result, the compilation duration and memory usage increases.
  3. Use the override mechanism of the ohpm or enable resolve_conflict to resolve dependency conflicts and reduce repeated compilation caused by dependency packages.
  4. Load the infrequently used functions as on-demand modules.

Using a Scanning Tool to Analyze the Application Size

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:

1. Duplicate files

2. Large files

  • Check whether the files are mandatory for the app and whether they can be deleted.
  • For JPG, PNG, and GIF files, you can compress images.

3. Files of a specific type

You can configure .so library compression options to compress and package .so files.

Method of Reducing the Size of an Application Package

Configuring .so Library Compression Options

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.

Collapse
Word wrap
Dark theme
Copy code
  1. {
  2. "module": {
  3. // ...
  4. "compressNativeLibs": true // Identify whether the so library is packaged in compressed storage. 'true' means compressed so library, 'false' means non-compressed.
  5. }
  6. }

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.

Expand

File

Original Size

Size After Compression

Compression Ratio

armeabi-v7a/libc++_shared.so

1,108 KB

386 KB

34%

Reducing Repeated Compilation of Dependency Packages

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:

Collapse
Word wrap
Dark theme
Copy code
  1. {
  2. "overrides": {
  3. "foo": "1.0.0"
  4. }
  5. }

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.

Collapse
Word wrap
Dark theme
Copy code
  1. {
  2. "overrides": {
  3. "foo": "file:./foo"
  4. }
  5. }

2. The foo.har package exists locally and is stored in the libs directory. Its path is specified using file:./libs/foo.har.

Collapse
Word wrap
Dark theme
Copy code
  1. {
  2. "overrides": {
  3. "foo": "file:./libs/foo.har"
  4. }
  5. }

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.

On-demand Distribution

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.

Using HSP to Share Code and Resources in Multi-Package Scenarios

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.