• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# HAP
2<!--Kit: Ability Kit-->
3<!--Subsystem: BundleManager-->
4<!--Owner: @wanghang904-->
5<!--Designer: @hanfeng6-->
6<!--Tester: @kongjing2-->
7<!--Adviser: @Brilliantry_Rui-->
8
9The Harmony Ability Package (HAP) is the basic unit for installing and running applications. A HAP is a module package consisting of code, resource files, third-party libraries, and an application configuration file. There are two types of HAPs: entry and feature.
10
11- entry: main module of and entry to an application, providing the basic application functionality.
12- feature: dynamic feature module of an application, extending the application functionality. This type of HAP can be installed based on user needs and device types.
13
14An application package can contain either only one entry HAP or one entry HAP plus one or more feature HAPs.
15
16## When to Use
17
18- Single HAP: If your application only uses the UIAbility (that is, no ExtensionAbility is required), a single HAP (entry HAP) is recommended. While a HAP can contain one or more UIAbilities, adopt the "one UIAbility + multiple pages" mode to avoid unnecessary resource loading.
19
20- Multi-HAP: If your application needs to use ExtensionAbilities, develop multiple HAPs (one entry HAP and one or more feature HAPs) for it, with each HAP containing one UIAbility or one ExtensionAbility. In this scenario, multiple HAPs reference the same library file, which may cause repeated packaging.
21
22
23## Constraints
24
25- APIs and ArkUI components cannot be exported from the HAP to other modules.
26
27- In an App Pack that contains multiple HAPs, each type of device supports zero or one entry HAP and zero, one, or more feature HAPs.
28
29- If an application has multiple HAPs, the settings of the following parameters must be consistent across the configuration files of these HAPs: **bundleName**, **versionCode**, **versionName**, **minCompatibleVersionCode**, **debug**, **minAPIVersion**, **targetAPIVersion**, and **apiReleaseType**. The value of **moduleName** for any HAP of the same device type must be unique. The IDE validates the settings of these parameters when packaging the HAPs into an App Pack.
30
31- If an application has multiple HAPs, the signing certificates of all HAPs and HSPs of this application must be the same. Applications are released to the AppGallery in the form of App Pack after being signed. Before distribution, the AppGallery splits an App Pack into HAPs and resigns them to ensure the consistency of HAP signing certificates. In the debugging phase, ensure that all HAP signing certificates are the same when you install HAP on the device using the CLI or DevEco Studio. Otherwise, the installation may fail. For details about the signing operations, see [Configuring a Debug Signature](https://developer.huawei.com/consumer/en/doc/harmonyos-guides/ide-signing).
32
33## Creating a HAP
34
35To create a HAP in DevEco Studio:
36
371. Create a project to build the first ArkTS application.
382. Right-click the project directory and choose **New** > **Module** from the shortcut menu.
393. In the dialog box displayed, select **Empty Ability** as the template and click **Next**.
40
414. On the module configuration page, set **Module name**, **Module Type**, and **Device Type**, and click **Next**.
42
435. On the ability configuration page, set **Ability name** and click **Finish**.
44
45## Developing a HAP
46
47- You can add a UIAbility, an ExtensionAbility, or a page to a HAP. For details, see [Adding an Ability to a Module](https://developer.huawei.com/consumer/en/doc/harmonyos-guides/ide-add-new-ability) and [Adding a Page](https://developer.huawei.com/consumer/en/doc/harmonyos-guides/ide-add-page).
48
49- You can also configure a HAP to reference a HAR or HSP. For details, see [Using a HAR](./har-package.md#using-a-har) and [Using an HSP](./in-app-hsp.md#using-an-hsp).
50
51## Debugging a HAP
52
53After building code into one or more HAPs and installing or updating these HAPs, you can debug them. For details about how to compile the same HAP into different versions based on the deployment environment, target user group, and running environment, see [Customizing Multi-Target Builds](https://developer.huawei.com/consumer/en/doc/harmonyos-guides/ide-customized-multi-targets-and-products-guides#section1011341611469).
54
55To debug a HAP, use either of the following tools:
56
57- **Method 1**: Use DevEco Studio for debugging. For details, see [Running/Debugging Configuration](https://developer.huawei.com/consumer/en/doc/harmonyos-guides/ide-run-debug-configurations).
58
59- **Method 2**: Use [hdc](../dfx/hdc.md) for debugging.
60
61   Before debugging a HAP, install or update it using either of the methods:
62
63   - Use hdc to install and update the HAP.
64
65      When specifying the HAP, use the path to it on the operating system. In the following example, the operating system is Windows:
66
67      ```shell
68      # Install and update. Multiple file paths can be specified for multiple HAPs.
69      hdc install entry.hap feature.hap
70      # Review the execution result.
71      install bundle successfully.
72      # Uninstall.
73      hdc uninstall com.example.myapplication
74      # Review the execution result.
75      uninstall bundle successfully.
76      ```
77
78   - Run the **hdc shell** command, and then use the Bundle Manager (bm) tool to install and update the HAP.
79
80      When specifying the HAP, use the path to it on the real device. The sample code is as follows:
81
82      ```shell
83      # Run the hdc shell command before using the bm tool.
84      hdc shell
85      # Install and update. Multiple file paths can be specified for multiple HAPs.
86      bm install -p /data/app/entry.hap /data/app/feature.hap
87      # Review the execution result.
88      install bundle successfully.
89      # Uninstall.
90      bm uninstall -n com.example.myapplication
91      # Review the execution result.
92      uninstall bundle successfully.
93      ```
94
95   After the HAP is installed or updated, you can debug it using the [attach](../tools/aa-tool.md#attach) command.
96
97<!--RP4-->
98<!--RP4End-->
99