1# HAR 2<!--Kit: Ability Kit--> 3<!--Subsystem: BundleManager--> 4<!--Owner: @wanghang904--> 5<!--Designer: @hanfeng6--> 6<!--Tester: @kongjing2--> 7<!--Adviser: @Brilliantry_Rui--> 8 9A Harmony Archive (HAR) is a static shared package that can contain code, C++ libraries, resource files, and configuration files (also called profiles). It enables modules and projects to share code of ArkUI components, resources, and more. 10 11## When to Use 12- Supports intra-application sharing or, after being released, intra-application sharing. 13- As a second-party library for internal applications, by being released to the [OHPM private repository](https://developer.huawei.com/consumer/en/doc/harmonyos-guides/ide-ohpm-repo). 14- As a third-party library for external applications, by being released to the [OHPM central repository](https://ohpm.openharmony.cn/#/en/home). 15 16## Constraints 17 18- A HAR can only be referenced as a dependency of an application module. It cannot be installed or run independently on a device. 19- A HAR does not support the declaration of the [ExtensionAbility](../application-models/extensionability-overview.md) component in the configuration file. Since API version 14, the declaration of the [UIAbility](../application-models/uiability-overview.md) component in the configuration file is supported. For details about how to configure UIAbility, see [Adding a UIAbility to a Module](https://developer.huawei.com/consumer/en/doc/harmonyos-guides/ide-add-new-ability#section18658758104318). Starting UIAbility in HAR is the same as [in an application](../application-models/uiability-intra-device-interaction.md). 20> **NOTE** 21> 22> If the [startAbility](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#startability) API is used to start the UIAbility in the HAR, the value of **moduleName** in the API parameter must be the module name of the [HAP](hap-package.md) or [HSP](in-app-hsp.md) that depends on the HAR. 23- A HAR does not support the declaration of the [pages](./module-configuration-file.md#pages) tag in the configuration file. Still, it can include pages, which can be redirected through [routing operations](../ui/arkts-navigation-navigation.md#routing-operations). 24- A HAR does not support referencing resources in the **AppScope** folder. This is because the content in the **AppScope** folder is not packaged into the HAR during building. 25- As the HSP supports only intra-application sharing, a HAR that depends on any HSP can be shared only within the same application. Do not release such a HAR to a second-party or third-party repository for other applications to use; otherwise, build failures will occur. 26- When multiple HAPs or HSPs reference the same HAR, the application package may contain multiple copies of code and resource files for the HAPs or HSPs, resulting in an unwelcome large package size. 27- A HAR can depend on other HARs or HSPs, but does not support cyclic dependency or dependency transfer. 28- When a HAP references a HAR, the system automatically combines their permission configurations during compilation and build. Therefore, you do not need to repeatedly request the same permission in the HAP and HAR. 29 30> **NOTE** 31> 32> Cyclic dependency: For example, there are three HARs. HAR-A depends on HAR-B, HAR-B depends on HAR-C, and HAR-C depends on HAR-A. 33> 34> Dependency transfer: For example, there are three HARs. HAR-A depends on HAR-B, and HAR-B depends on HAR-C. If dependency transfer is not supported, HAR-A can use the methods and components of HAR-B, but cannot directly use that of HAR-C. 35 36 37## Creating a HAR 38You can create a HAR module for calling C++ code in DevEco Studio. During the creation, turn on **Enable native** on the **Configure New Module** page. For details, see [Creating a HAR Module](https://developer.huawei.com/consumer/en/doc/harmonyos-guides/ide-har#section643521083015). 39 40 41## Developing a HAR 42 43You can export the ArkUI components, APIs, and other resources of a HAR for other applications or intra-application modules to reference. 44 45The **Index.ets** file acts as the entry of the HAR export declaration file and is where the HAR exports APIs. This file is automatically generated by DevEco Studio by default. You can specify another file as the entry declaration file in the **main** field in the **oh-package.json5** file of the module. The code snippet is as follows: 46```json 47{ 48 "main": "Index.ets" 49} 50``` 51> **NOTE** 52> 53> When compiling together with the host application, the HAR code is directly compiled into the host application. A HAR package serves as an intermediate build product rather than a final runtime entity. At runtime, the HAR operates under the identity of its host application, and the system differentiates behaviors based on the version of the host application. If it is necessary to implement version-specific behaviors for the host application within the HAR, you can call the [getBundleInfoForSelf](../reference/apis-ability-kit/js-apis-bundleManager.md#bundlemanagergetbundleinfoforself) API to obtain the host application's **targetVersion**, and then execute different logic based on the obtained **targetVersion**. 54 55### Exporting ArkUI Components 56Export ArkUI components using **export**. The code snippet is as follows: 57```ts 58// library/src/main/ets/components/mainpage/MainPage.ets 59@Component 60export struct MainPage { 61 @State message: string = 'HAR MainPage'; 62 63 build() { 64 Column() { 65 Row() { 66 Text(this.message) 67 .fontSize(32) 68 .fontWeight(FontWeight.Bold) 69 } 70 .margin({ top: '32px' }) 71 .height(56) 72 .width('624px') 73 74 Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center, alignContent: FlexAlign.Center }) { 75 Column() { 76 Image($r('app.media.pic_empty')).width('33%') 77 Text($r('app.string.empty')) 78 .fontSize(14) 79 .fontColor($r('app.color.text_color')) 80 } 81 }.width('100%') 82 .height('90%') 83 } 84 .width('100%') 85 .height('100%') 86 .backgroundColor($r('app.color.page_background')) 87 } 88} 89``` 90In the **Index.ets** file, declare the APIs that the HAR exposes to external systems. The code snippet is as follows: 91```ts 92// library/Index.ets 93export { MainPage } from './src/main/ets/components/mainpage/MainPage'; 94``` 95### Exporting Classes and Methods 96Use **export** to export classes and methods. Multiple classes and methods can be exported. The code snippet is as follows: 97```ts 98// library/src/main/ets/test.ets 99export class Log { 100 static info(msg: string) { 101 console.info(msg); 102 } 103} 104 105export function func() { 106 return 'har func'; 107} 108 109export function func2() { 110 return 'har func2'; 111} 112``` 113In the **Index.ets** file, declare the APIs that the HAR exposes to external systems. The code snippet is as follows: 114```ts 115// library/Index.ets 116export { Log } from './src/main/ets/test'; 117export { func } from './src/main/ets/test'; 118export { func2 } from './src/main/ets/test'; 119``` 120 121### Exporting Native Methods 122The HAR can contain .so files written in C++. Native methods in the .so file can be exported from the HAR in the following way. In the example, the **add** API in the **liblibrary.so** file is exported. 123```ts 124// library/src/main/ets/utils/nativeTest.ets 125import native from 'liblibrary.so'; 126 127export function nativeAdd(a: number, b: number): number { 128 let result: number = native.add(a, b); 129 return result; 130} 131``` 132In the **Index.ets** file, declare the APIs that the HAR exposes to external systems. The code snippet is as follows: 133```ts 134// library/Index.ets 135export { nativeAdd } from './src/main/ets/utils/nativeTest'; 136``` 137 138### Exporting Resources 139Specifically, DevEco Studio collects resource files from the HAP module and its dependent modules, and overwrites the resource files with the same name (if any) based on the following priorities (in descending order): 140- AppScope (supported only by the stage model) 141- Modules in the HAP 142- Dependent HAR modules<br>If resource conflicts occur between dependent HAR modules, they are overwritten based on the dependency sequence indicated under **dependencies** in the **oh-package.json5** file. The module that is higher in the dependency sequence list has a higher priority. For example, in the following example, if **dayjs** and **lottie** folders contain files with the same name, resources in **dayjs** are used preferentially. 143> **NOTE** 144> 145> With regard to resources in the internationalization folder of **AppScope**, HAP, and HAR directories, the preceding priority rules also apply to resources with the same internationalization qualifier. In addition, resources with internationalization qualifiers are prioritized over those in the **base** folder. For example, if resources with the same name are configured in both the **base** folder under **AppScope** and the **en_US** folder of a HAR, the one in the **en_US** folder is prioritized for internationalization purposes. 146``` 147// oh-package.json5 148{ 149 "dependencies": { 150 "dayjs": "^1.10.4", 151 "lottie": "^2.0.0" 152 } 153} 154``` 155 156## Using a HAR 157 158You can reference the ArkUI components, APIs, and resources in a HAR. 159 160Before referencing the HAR, you need to configure the dependency on it. For details, see [Referencing a Shared Package](https://developer.huawei.com/consumer/en/doc/harmonyos-guides/ide-har-import). 161 162### Referencing ArkUI Components 163 164After configuring the dependency on the HAR, you can reference ArkUI components exported from the HAR by using **import**. The code snippet is as follows: 165```ts 166// entry/src/main/ets/pages/IndexSec.ets 167import { MainPage } from 'library'; 168 169@Entry 170@Component 171struct IndexSec { 172 build() { 173 Row() { 174 // Reference the ArkUI component in the HAR. 175 MainPage() 176 } 177 .height('100%') 178 } 179} 180``` 181### Referencing ETS Classes and Methods 182Use **import** to reference the classes and methods exported from the HAR. The code snippet is as follows: 183```ts 184// entry/src/main/ets/pages/Index.ets 185import { Log, func } from 'library'; 186 187@Entry 188@Component 189struct Index { 190 @State message: string = 'Hello World'; 191 192 build() { 193 Column() { 194 Text(this.message) 195 .fontFamily('HarmonyHeiTi') 196 .fontWeight(FontWeight.Bold) 197 .fontSize(32) 198 199 // Reference ETS classes and methods. 200 Button($r('app.string.button')) 201 .id('button') 202 .height(48) 203 .width('624px') 204 .margin({ top: '4%' }) 205 .type(ButtonType.Capsule) 206 .onClick(() => { 207 // Reference ETS classes and methods in the HAR. 208 Log.info('har msg'); 209 this.message = 'func return: ' + func(); 210 }) 211 } 212 .width('100%') 213 .backgroundColor($r('app.color.page_background')) 214 .height('100%') 215 } 216} 217``` 218 219### Referencing Native Methods 220Use **import** to reference the native methods exported from the HAR. The code snippet is as follows: 221```ts 222// entry/src/main/ets/pages/Index.ets 223import { nativeAdd } from 'library'; 224 225@Entry 226@Component 227struct Index { 228 @State message: string = 'Hello World'; 229 230 build() { 231 Column() { 232 Text(this.message) 233 .fontFamily('HarmonyHeiTi') 234 .fontWeight(FontWeight.Bold) 235 .fontSize(32) 236 237 // Reference the native method in the HAR. 238 Button($r('app.string.native_add')) 239 .id('nativeAdd') 240 .height(48) 241 .width('624px') 242 .margin({ top: '4%', bottom: '6%' }) 243 .type(ButtonType.Capsule) 244 .onClick(() => { 245 this.message = 'result: ' + nativeAdd(1, 2); 246 }) 247 } 248 .width('100%') 249 .backgroundColor($r('app.color.page_background')) 250 .height('100%') 251 } 252} 253``` 254 255### Referencing Resources 256Use **$r** to reference resources in the HAR. For example, add the **name: hello_har** string (defined in the **string.json** file) and **icon_har.png** image to the **src/main/resources** directory of the HAR module, and then reference the string and image in the entry module. The code snippet is as follows: 257```ts 258// entry/src/main/ets/pages/Index.ets 259@Entry 260@Component 261struct Index { 262 @State message: string = 'Hello World'; 263 264 build() { 265 Column() { 266 // Reference the string in the HAR. 267 Text($r('app.string.hello_har')) 268 .id('stringHar') 269 .fontFamily('HarmonyHeiTi') 270 .fontColor($r('app.color.text_color')) 271 .fontSize(24) 272 .fontWeight(500) 273 .margin({ top: '40%' }) 274 275 List() { 276 ListItem() { 277 // Reference the image in the HAR. 278 Image($r('app.media.icon_har')) 279 .id('iconHar') 280 .borderRadius('48px') 281 } 282 .margin({ top: '5%' }) 283 .width('312px') 284 } 285 .alignListItem(ListItemAlign.Center) 286 } 287 .width('100%') 288 .backgroundColor($r('app.color.page_background')) 289 .height('100%') 290 } 291} 292``` 293## Building a HAR 294 295HAR can be used as a second-party or third-party library for other applications. To protect code assets, you are advised to [enable code obfuscation](../arkts-utils/source-obfuscation-guide.md#enabling-source-code-obfuscation). 296 297After [code obfuscation](../arkts-utils/source-obfuscation.md) is enabled, DevEco Studio compiles, obfuscates, and compresses code when building HARs to protect code assets. 298 299The obfuscation capability is enabled by default for the HAR module. When the compilation module is release, simple code obfuscation is automatically performed for the HAR module of API version 10 or later. **Since DevEco Studio 5.0.3.600, the code obfuscation is disabled by default when a project is created.** You can enable this feature by setting **enable** in the **ruleOptions** field in the **build-profile.json5** file of the HAR module. For details, see [Using Obfuscation for Code Hardening](https://developer.huawei.com/consumer/en/doc/harmonyos-guides/ide-build-obfuscation). The configuration is as follows: 300 301 ```json 302 { 303 "apiType": "stageMode", 304 "buildOption": { 305 }, 306 "buildOptionSet": [ 307 { 308 "name": "release", 309 "arkOptions": { 310 "obfuscation": { 311 "ruleOptions": { 312 "enable": true, 313 "files": [ 314 "./obfuscation-rules.txt" 315 ] 316 }, 317 "consumerFiles": [ 318 "./consumer-rules.txt" 319 ] 320 } 321 } 322 }, 323 ], 324 "targets": [ 325 { 326 "name": "default" 327 } 328 ] 329 } 330 ``` 331 332### Building TS Files 333 334> **Scenario Description** 335> 336>Enable this configuration when [Sendable](../arkts-utils/arkts-sendable.md) is used in a HAR. 337 338> **Constraints** 339> 340> When depend on TS HAR, the ArkUI component in TS HAR cannot be referenced. 341 342 343After the ArkTS file in the HAR module is built, the product is a JS file by default. To change the product to a TS file, set **name** under the **metadata** field in the **module.json5** file of the HAR module to **UseTsHar**. The configuration is as follows: 344> 345> **NOTE** 346> 347> Since DevEco Studio 5.0.3.800, the bytecode HAR is built by default. For details, see [Building a HAR](https://developer.huawei.com/consumer/en/doc/harmonyos-guides/ide-hvigor-build-har). 348> 349 350 ```json 351 { 352 "module": { 353 "name": "TsClosedHar", 354 "type": "har", 355 "deviceTypes": [ 356 "default", 357 "tablet", 358 "2in1" 359 ], 360 "metadata": [ 361 { 362 "name": "UseTsHar", 363 "value": "true" 364 } 365 ] 366 } 367 } 368 ``` 369 370## Publishing a HAR 371 372For details, see [Publishing a Shared Package](https://developer.huawei.com/consumer/en/doc/harmonyos-guides/ide-har-publish).