16e7c69efSopenharmony_ci# HAR 26e7c69efSopenharmony_ci 36e7c69efSopenharmony_ci### 介绍 46e7c69efSopenharmony_ci 56e7c69efSopenharmony_ciHAR(Harmony Archive)是静态共享包,可以包含代码、C++库、资源和配置文件。通过HAR可以实现多个模块或多个工程共享ArkUI组件、资源等相关代码。HAR不同于HAP,不能独立安装运行在设备上,只能作为应用模块的依赖项被引用。 66e7c69efSopenharmony_ci 76e7c69efSopenharmony_ci### 效果预览 86e7c69efSopenharmony_ci 96e7c69efSopenharmony_ci| 主页 | 导出ArkUI组件页面 | 导出ts类和方法页面 | 106e7c69efSopenharmony_ci|----------------------------------|-------------------------------------------|-------------------------------------------| 116e7c69efSopenharmony_ci|  |  |  | 126e7c69efSopenharmony_ci| **导出native方法页面** | | | 136e7c69efSopenharmony_ci|  | | | 146e7c69efSopenharmony_ci 156e7c69efSopenharmony_ci使用说明: 166e7c69efSopenharmony_ci 176e7c69efSopenharmony_ci1.启动HarSample(静态共享包)应用,首页正常显示所引用HAR的ArkUI组件、ts类和方法、native方法、资源。 186e7c69efSopenharmony_ci 196e7c69efSopenharmony_ci2.点击主页"引用HAR的ArkUI组件"按钮,正确引用HAR的ArkUI组件,跳转至HAR的MainPage页面,页面显示内容为:暂无内容。 206e7c69efSopenharmony_ci 216e7c69efSopenharmony_ci3.点击主页"引用HAR的ts类和方法"按钮,正确引用HAR导出的ts类和方法,页面Hello World字段变更为func return: har func。 226e7c69efSopenharmony_ci 236e7c69efSopenharmony_ci4.点击主页"引用HAR导出的native方法"按钮,正确引用HAR导出的native方法,页面Hello World字段变更为result: 3。 246e7c69efSopenharmony_ci 256e7c69efSopenharmony_ci### 工程目录 266e7c69efSopenharmony_ci``` 276e7c69efSopenharmony_cientry/src/main/ets/ 286e7c69efSopenharmony_ci|---entryability 296e7c69efSopenharmony_ci| └---EntryAbility.ts 306e7c69efSopenharmony_ci|---logger 316e7c69efSopenharmony_ci| └---Logger.ts // 日志工具 326e7c69efSopenharmony_ci└---pages 336e7c69efSopenharmony_ci |---Index.ets // 首页 346e7c69efSopenharmony_ci └---IndexSec.ets // 引用HAR的ArkUI组件的页面 356e7c69efSopenharmony_cilibrary 366e7c69efSopenharmony_ci|---src 376e7c69efSopenharmony_ci| └---main 386e7c69efSopenharmony_ci| |---cpp 396e7c69efSopenharmony_ci| | |---types 406e7c69efSopenharmony_ci| | |---CMakeLists.txt 416e7c69efSopenharmony_ci| | └---hello.cpp 426e7c69efSopenharmony_ci| |---ets 436e7c69efSopenharmony_ci| | |---components 446e7c69efSopenharmony_ci| | | └---MyTitleBar.ets // ArkUI组件页面 456e7c69efSopenharmony_ci| | └---utils 466e7c69efSopenharmony_ci| | └---nativeTest.ts // native方法页面 476e7c69efSopenharmony_ci| |---resources // 资源文件 486e7c69efSopenharmony_ci| |---ts 496e7c69efSopenharmony_ci| | └---tset.ts // ts类和方法页面 506e7c69efSopenharmony_ci| └---module.json5 516e7c69efSopenharmony_ci|---index.ets 526e7c69efSopenharmony_ci└---oh-package.json5 536e7c69efSopenharmony_ci``` 546e7c69efSopenharmony_ci 556e7c69efSopenharmony_ci### 具体实现 566e7c69efSopenharmony_ci 576e7c69efSopenharmony_ci* 本示例为HarSample(静态共享包)应用,提供导出及引用HAR的ArkUI组件、接口、资源功能: 586e7c69efSopenharmony_ci * 导出HAR的ArkUI组件、接口、资源 596e7c69efSopenharmony_ci * Index.ets文件是HAR导出声明文件的入口,HAR需要导出的接口,统一在Index.ets文件中导出。Index.ets文件是DevEco Studio默认自动生成的,用户也可以自定义,在模块的oh-package.json5文件中的main字段配置入口声明文件。源码链接:[Index.ets](library/index.ets),[oh-package.json5](library/oh-package.json5) 606e7c69efSopenharmony_ci * HAR中MainPage.ets文件的ArkUI组件,源码链接:[MainPage.ets](library/src/main/ets/components/mainpage/MainPage.ets) 616e7c69efSopenharmony_ci * HAR中test.ets文件的ts类和方法,源码链接:[test.ets](library/src/main/ts/test.ets) 626e7c69efSopenharmony_ci * HAR中可以包含C++编写的so,so中的native方法,HAR通过import方式导出,nativeTest.ts文件导出liblibrary.so的加法接口add,链接: [nativeTest.ts](library/src/main/ets/utils/nativeTest.ts) 636e7c69efSopenharmony_ci * 引用HAR的ArkUI组件、接口、资源 646e7c69efSopenharmony_ci * 在使用方中,引用HAR的ArkUI组件、接口、资源及 页面路由跳转,源码链接:[Index.ets](entry/src/main/ets/pages/Index.ets),[IndexSec.ets](entry/src/main/ets/pages/IndexSec.ets) 656e7c69efSopenharmony_ci 666e7c69efSopenharmony_ci 676e7c69efSopenharmony_ci### 相关权限 686e7c69efSopenharmony_ci 696e7c69efSopenharmony_ci不涉及。 706e7c69efSopenharmony_ci 716e7c69efSopenharmony_ci### 依赖 726e7c69efSopenharmony_ci 736e7c69efSopenharmony_ci不涉及。 746e7c69efSopenharmony_ci 756e7c69efSopenharmony_ci### 约束与限制 766e7c69efSopenharmony_ci 776e7c69efSopenharmony_ci1.本示例仅支持标准系统上运行,支持设备:RK3568。 786e7c69efSopenharmony_ci 796e7c69efSopenharmony_ci2.本示例为Stage模型,支持API11版本SDK,SDK版本号(API Version 11 Canary1)。 806e7c69efSopenharmony_ci 816e7c69efSopenharmony_ci3.本示例需要使用DevEco Studio版本号(3.1 Release)及以上版本才可编译运行。 826e7c69efSopenharmony_ci 836e7c69efSopenharmony_ci### 下载 846e7c69efSopenharmony_ci 856e7c69efSopenharmony_ci如需单独下载本工程,执行如下命令: 866e7c69efSopenharmony_ci 876e7c69efSopenharmony_ci``` 886e7c69efSopenharmony_cigit init 896e7c69efSopenharmony_cigit config core.sparsecheckout true 906e7c69efSopenharmony_ciecho code/DocsSample/BundleManagerDocsSample/HarSample/ > .git/info/sparse-checkout 916e7c69efSopenharmony_cigit remote add origin https://gitee.com/openharmony/applications_app_samples.git 926e7c69efSopenharmony_cigit pull origin master 936e7c69efSopenharmony_ci``` 94