16e7c69efSopenharmony_ci# HSP 26e7c69efSopenharmony_ci 36e7c69efSopenharmony_ci### 介绍 46e7c69efSopenharmony_ci 56e7c69efSopenharmony_ciHSP(Harmony Shared Package)是动态共享包,按照使用场景可以分为应用内HSP和应用间HSP。应用内HSP指的是专门为某一应用开发的HSP,只能被该应用内部其他HAP/HSP使用,用于应用内部代码、资源的共享。应用内HSP跟随其宿主应用的APP包一起发布,与宿主应用同进程,具有相同的包名和生命周期。 66e7c69efSopenharmony_ci 76e7c69efSopenharmony_ci>**说明** 86e7c69efSopenharmony_ci> 96e7c69efSopenharmony_ci>由于当前不支持应用间HSP,这里提到的HSP特指应用内HSP。 106e7c69efSopenharmony_ci 116e7c69efSopenharmony_ci### 效果预览 126e7c69efSopenharmony_ci 136e7c69efSopenharmony_ci| 主页 | **资源引用页面** | **路由跳转页面** | 146e7c69efSopenharmony_ci|----------------------------------|-------------------------------------------|-------------------------------------------| 156e7c69efSopenharmony_ci|  |  |  | 166e7c69efSopenharmony_ci| **路由返回页面** | | | 176e7c69efSopenharmony_ci|  | | | 186e7c69efSopenharmony_ci 196e7c69efSopenharmony_ci使用说明: 206e7c69efSopenharmony_ci 216e7c69efSopenharmony_ci1.启动HspSample(动态共享包)应用,首页正常显示所引用HSP的ArkUI组件、接口、资源。 226e7c69efSopenharmony_ci 236e7c69efSopenharmony_ci2.点击主页"引用HSP的ts类和方法"按钮,正确引用HSP的ts类和方法,页面Hello World字段变更为result: 3。 246e7c69efSopenharmony_ci 256e7c69efSopenharmony_ci3.点击主页"引用HSP中的资源"按钮,正确引用HSP中的资源,页面Hello World字段变更为getStringValue is shared。 266e7c69efSopenharmony_ci 276e7c69efSopenharmony_ci4.点击主页"引用HSP导出的native方法"按钮,正确引用HSP导出的native方法,页面Hello World字段变更为result: 12。 286e7c69efSopenharmony_ci 296e7c69efSopenharmony_ci5.点击主页"访问HSP中的资源"按钮,跳转至HSP中Index页面,Index页面正确访问HSP中的资源,显示内容为example.png的图片信息。 306e7c69efSopenharmony_ci 316e7c69efSopenharmony_ci6.点击主页"页面路由跳转"按钮,正确引用HSP的路由跳转,跳转至menu页面,页面显示内容为:The page is menu。menu页面点击"router to HSP page"按钮,跳转至HSP的新页面,HSP的新页面上点击"back to HSP page"按钮,返回menu页面。 326e7c69efSopenharmony_ci 336e7c69efSopenharmony_ci7.点击主页"页面路由返回"按钮,正确引用HSP的路由跳转,跳转至新页面,新页面上点击"back to HAP page"按钮,返回至主页。 346e7c69efSopenharmony_ci 356e7c69efSopenharmony_ci### 工程目录 366e7c69efSopenharmony_ci``` 376e7c69efSopenharmony_cientry/src/main/ets/ 386e7c69efSopenharmony_ci|---entryability 396e7c69efSopenharmony_ci| └---EntryAbility.ts 406e7c69efSopenharmony_ci|---logger 416e7c69efSopenharmony_ci| └---Logger.ts // 日志工具 426e7c69efSopenharmony_ci└---pages 436e7c69efSopenharmony_ci └---Index.ets // 首页 446e7c69efSopenharmony_cilibrary 456e7c69efSopenharmony_ci|---src 466e7c69efSopenharmony_ci| └---main 476e7c69efSopenharmony_ci| |---cpp 486e7c69efSopenharmony_ci| | |---types 496e7c69efSopenharmony_ci| | |---CMakeLists.txt 506e7c69efSopenharmony_ci| | └---hello.cpp 516e7c69efSopenharmony_ci| |---ets 526e7c69efSopenharmony_ci| | |---components 536e7c69efSopenharmony_ci| | | └---MyTitleBar.ets // ArkUI组件页面 546e7c69efSopenharmony_ci| | |---page 556e7c69efSopenharmony_ci| | | |---back.ets // 实现页面路由返回 566e7c69efSopenharmony_ci| | | |---Index.ets // 访问HSP中的图片资源 576e7c69efSopenharmony_ci| | | └---menu.ts 586e7c69efSopenharmony_ci| | └---utils 596e7c69efSopenharmony_ci| | └---nativeTest.ts // native方法页面 606e7c69efSopenharmony_ci| | └---tset.ts // ts类和方法页面 616e7c69efSopenharmony_ci| |---resources // 资源文件 626e7c69efSopenharmony_ci| └---module.json5 636e7c69efSopenharmony_ci└---oh-package.json5 646e7c69efSopenharmony_ci``` 656e7c69efSopenharmony_ci 666e7c69efSopenharmony_ci### 具体实现 676e7c69efSopenharmony_ci 686e7c69efSopenharmony_ci* 本示例为HspSample(动态共享包)应用,提供 导出HSP的ArkUI组件、接口、资源功能: 696e7c69efSopenharmony_ci * 导出HSP的ArkUI组件、接口、资源,HSP中的资源,源码链接:[index.ets](library/src/main/ets/Index.ets) 706e7c69efSopenharmony_ci * HSP中MyTitleBar.ets文件的ArkUI组件,源码链接:[MyTitleBar.ets](library/src/main/ets/components/MyTitleBar.ets) 716e7c69efSopenharmony_ci * HSP中test.ts文件的ts类和方法,源码链接:[test.ts](library/src/main/ets/utils/test.ts) 726e7c69efSopenharmony_ci * HSP中nativeTest.ts文件的native方法。在HSP中也可以包含C++编写的so,对于so中的native方法,HSP通过间接的方式导出,如导出libnative.so的乘法接口multi,源码链接:[nativeTest.ts](library/src/main/ets/utils/nativeTest.ts) 736e7c69efSopenharmony_ci * 通过$r访问HSP中的资源,源码链接:[Index.ets](library/src/main/ets/pages/Index.ets) 746e7c69efSopenharmony_ci * HSP中的资源管理类,跨包访问HSP内资源时,推荐实现一个资源管理类,以封装对外导出的资源,源码链接:[ResManager.ets](library/src/main/ets/ResManager.ets) 756e7c69efSopenharmony_ci * 引用HSP的ArkUI组件、接口、资源 766e7c69efSopenharmony_ci * 在使用方中,引用HSP的ArkUI组件、接口、资源及 页面路由跳转,源码链接:[index.ets](entry/src/main/ets/pages/Index.ets) 776e7c69efSopenharmony_ci 786e7c69efSopenharmony_ci### 相关权限 796e7c69efSopenharmony_ci 806e7c69efSopenharmony_ci不涉及。 816e7c69efSopenharmony_ci 826e7c69efSopenharmony_ci### 依赖 836e7c69efSopenharmony_ci 846e7c69efSopenharmony_ci不涉及。 856e7c69efSopenharmony_ci 866e7c69efSopenharmony_ci### 约束与限制 876e7c69efSopenharmony_ci 886e7c69efSopenharmony_ci1.本示例仅支持标准系统上运行,支持设备:RK3568。 896e7c69efSopenharmony_ci 906e7c69efSopenharmony_ci2.本示例为Stage模型,支持API11版本SDK,SDK版本号(API Version 11 Canary1)。 916e7c69efSopenharmony_ci 926e7c69efSopenharmony_ci3.本示例需要使用DevEco Studio版本号(3.1 Release)及以上版本才可编译运行。 936e7c69efSopenharmony_ci 946e7c69efSopenharmony_ci### 下载 956e7c69efSopenharmony_ci 966e7c69efSopenharmony_ci如需单独下载本工程,执行如下命令: 976e7c69efSopenharmony_ci 986e7c69efSopenharmony_ci``` 996e7c69efSopenharmony_cigit init 1006e7c69efSopenharmony_cigit config core.sparsecheckout true 1016e7c69efSopenharmony_ciecho code/DocsSample/BundleManagerDocsSample/HspSample/ > .git/info/sparse-checkout 1026e7c69efSopenharmony_cigit remote add origin https://gitee.com/openharmony/applications_app_samples.git 1036e7c69efSopenharmony_cigit pull origin master 1046e7c69efSopenharmony_ci``` 1056e7c69efSopenharmony_ci 106