1e41f4b71Sopenharmony_ci# HAP 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciHAP(Harmony Ability Package)是应用安装和运行的基本单元。HAP包是由代码、资源、第三方库、配置文件等打包生成的模块包,其主要分为两种类型:entry和feature。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci- entry:应用的主模块,作为应用的入口,提供了应用的基础功能。 6e41f4b71Sopenharmony_ci- feature:应用的动态特性模块,作为应用能力的扩展,可以根据用户的需求和设备类型进行选择性安装。 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci应用程序包可以只包含一个基础的entry包,也可以包含一个基础的entry包和多个功能性的feature包。 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## 使用场景 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci- 单HAP场景:如果只包含UIAbility组件,无需使用ExtensionAbility组件,优先采用单HAP(即一个entry包)来实现应用开发。虽然一个HAP中可以包含一个或多个UIAbility组件,为了避免不必要的资源加载,推荐采用“一个UIAbility+多个页面”的方式。 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci- 多HAP场景:如果应用的功能比较复杂,需要使用ExtensionAbility组件,可以采用多HAP(即一个entry包+多个feature包)来实现应用开发,每个HAP中包含一个UIAbility组件或者一个ExtensionAbility组件。在这种场景下,可能会存在多个HAP引用相同的库文件,导致重复打包的问题。 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## 约束限制 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci- 不支持导出接口和ArkUI组件,给其他模块使用。 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci- 多HAP场景下,App Pack包中同一设备类型的所有HAP中必须有且只有一个Entry类型的HAP,Feature类型的HAP可以有一个或者多个,也可以没有。 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci- 多HAP场景下,同一应用中的所有HAP的配置文件中的bundleName、versionCode、versionName、minCompatibleVersionCode、debug、minAPIVersion、targetAPIVersion、apiReleaseType相同,同一设备类型的所有HAP对应的moduleName标签必须唯一。HAP打包生成App Pack包时,会对上述参数配置进行校验。 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci- 多HAP场景下,同一应用的所有HAP、HSP的签名证书要保持一致。上架应用市场是以App Pack形式上架,应用市场分发时会将所有HAP从App Pack中拆分出来,同时对其中的所有HAP进行重签名,这样保证了所有HAP签名证书的一致性。在调试阶段,开发者通过命令行或DevEco Studio将HAP安装到设备上时,要保证所有HAP签名证书一致,否则会出现安装失败的问题。 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci## 创建 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci下面简要介绍如何通过DevEco Studio新建一个HAP模块。 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci1. 创建工程,构建第一个ArkTS应用。 32e41f4b71Sopenharmony_ci2. 在工程目录上单击右键,选择**New > Module**。 33e41f4b71Sopenharmony_ci3. 在弹出的对话框中选择**Empty Ability**模板,单击**Next**。 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci4. 在Module配置界面,配置**Module name**,选择**Module Type**和**Device Type**,然后单击**Next**。 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci5. 在Ability配置界面,配置**Ability name**,然后单击**Finish**完成创建。 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci## 开发 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci- HAP中支持添加UIAbility组件或ExtensionAbility组件,添加pages页面。具体操作可参考<!--RP1-->[应用/服务开发](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-add-new-ability-V5)<!--RP1End-->。 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci- HAP中支持引用HAR或HSP共享包,详见[HAR的使用](./har-package.md#使用)、[HSP的使用](./in-app-hsp.md#使用)。 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci## 调试 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci通过DevEco Studio编译打包,生成单个或者多个HAP,即可基于HAP进行调试。如需根据不同的部署环境、目标人群、运行环境等,将同一个HAP定制编译为不同版本,请参见<!--RP2-->[定制编译指导](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-customized-multi-targets-and-products-guides-V5)<!--RP2End-->。 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci开发者可以采用DevEco Studio或者hdc工具进行调试: 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci- **方法一:** 使用DevEco Studio进行调试,详见<!--RP3-->[应用程序包调试方法](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-run-debug-configurations-V5)<!--RP3End-->。 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci- **方法二:** 使用<!--Del-->[<!--DelEnd-->hdc工具<!--Del-->](../../device-dev/subsystems/subsys-toolchain-hdc-guide.md)<!--DelEnd-->(可通过OpenHarmony SDK获取,在SDK的toolchains目录下)进行调试。 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci 在调试前,需要先安装或更新HAP,此处有两种方式: 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci - 直接使用hdc安装、更新HAP。 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci HAP的路径为开发平台上的文件路径,以Windows开发平台为例,命令参考如下: 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci ```shell 62e41f4b71Sopenharmony_ci // 安装、更新,多HAP可以指定多个文件路径 63e41f4b71Sopenharmony_ci hdc install entry.hap feature.hap 64e41f4b71Sopenharmony_ci // 执行结果 65e41f4b71Sopenharmony_ci install bundle successfully. 66e41f4b71Sopenharmony_ci // 卸载 67e41f4b71Sopenharmony_ci hdc uninstall com.example.myapplication 68e41f4b71Sopenharmony_ci // 执行结果 69e41f4b71Sopenharmony_ci uninstall bundle successfully. 70e41f4b71Sopenharmony_ci ``` 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci - 先执行hdc shell,再使用bm工具安装、更新HAP。 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci HAP的文件路径为真机上的文件路径,命令参考如下: 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci ```shell 77e41f4b71Sopenharmony_ci // 先执行hdc shell才能使用bm工具 78e41f4b71Sopenharmony_ci hdc shell 79e41f4b71Sopenharmony_ci // 安装、更新,多HAP可以指定多个文件路径 80e41f4b71Sopenharmony_ci bm install -p /data/app/entry.hap /data/app/feature.hap 81e41f4b71Sopenharmony_ci // 执行结果 82e41f4b71Sopenharmony_ci install bundle successfully. 83e41f4b71Sopenharmony_ci // 卸载 84e41f4b71Sopenharmony_ci bm uninstall -n com.example.myapplication 85e41f4b71Sopenharmony_ci // 执行结果 86e41f4b71Sopenharmony_ci uninstall bundle successfully. 87e41f4b71Sopenharmony_ci ``` 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci 完成HAP安装或更新后,即可参考相关调试命令进行[调试](../tools/aa-tool.md)。 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci<!--RP4--> 92e41f4b71Sopenharmony_ci<!--RP4End--> 93