1# HAR 2A 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. 3 4## When to Use 5- Supports intra-application sharing or, after being released, intra-application sharing. 6- 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-V13/ide-ohpm-repo-V13). 7- As a third-party library for external applications, by being released to the [OHPM central repository](https://ohpm.openharmony.cn/#/en/home). 8- 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. 9 10## Constraints 11 12- An HAR can only be referenced as a dependency of an application module. It cannot be installed or run independently on a device. 13- An HAR does not support the declaration of the [ExtensionAbility](../application-models/extensionability-overview.md) component in the configuration file, but supports the [UIAbility](../application-models/uiability-overview.md) component. 14> **NOTE** 15> 16> If the [startAbility](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) 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. 17- An 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 a [named route](../ui/arkts-routing.md#named-route). 18- An 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. 19- An HAR can depend on other HARs, but does not support cyclic dependency or dependency transfer. 20 21## Creating an HAR 22Create an HAR module in DevEco Studio. For details, see [Creating a HAR Module](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V13/ide-har-V13#section643521083015). 23 24 25## Developing an HAR 26 27You can export the ArkUI components, APIs, and other resources of an HAR for other applications or intra-application modules to reference. 28 29The **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: 30```json 31{ 32 "main": "Index.ets" 33} 34``` 35### Exporting ArkUI Components 36Use **export** to export the ArkUI components. The code snippet is as follows: 37```ts 38// library/src/main/ets/components/mainpage/MainPage.ets 39@Component 40export struct MainPage { 41 @State message: string = 'HAR MainPage'; 42 43 build() { 44 Column() { 45 Row() { 46 Text(this.message) 47 .fontSize(32) 48 .fontWeight(FontWeight.Bold) 49 } 50 .margin({ top: '32px' }) 51 .height(56) 52 .width('624px') 53 54 Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center, alignContent: FlexAlign.Center }) { 55 Column() { 56 Image($r('app.media.pic_empty')).width('33%') 57 Text($r('app.string.empty')) 58 .fontSize(14) 59 .fontColor($r('app.color.text_color')) 60 } 61 }.width('100%') 62 .height('90%') 63 } 64 .width('100%') 65 .height('100%') 66 .backgroundColor($r('app.color.page_background')) 67 } 68} 69``` 70In the **Index.ets** file, declare the APIs that the HAR exposes to external systems. The code snippet is as follows: 71```ts 72// library/Index.ets 73export { MainPage } from './src/main/ets/components/mainpage/MainPage'; 74``` 75### Exporting TS Classes and Methods 76Use **export** to export TS classes and methods. Multiple TS classes and methods can be exported at the same time. The code snippet is as follows: 77```ts 78// library/src/main/ts/test.ets 79export class Log { 80 static info(msg: string) { 81 console.info(msg); 82 } 83} 84 85export function func() { 86 return 'har func'; 87} 88 89export function func2() { 90 return 'har func2'; 91} 92``` 93In the **Index.ets** file, declare the APIs that the HAR exposes to external systems. The code snippet is as follows: 94```ts 95// library/Index.ets 96export { Log } from './src/main/ts/test'; 97export { func } from './src/main/ts/test'; 98export { func2 } from './src/main/ts/test'; 99``` 100 101### Exporting Native Methods 102The 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. 103```ts 104// library/src/main/ets/utils/nativeTest.ets 105import native from 'liblibrary.so'; 106 107export function nativeAdd(a: number, b: number): number { 108 let result: number = native.add(a, b); 109 return result; 110} 111``` 112In the **Index.ets** file, declare the APIs that the HAR exposes to external systems. The code snippet is as follows: 113```ts 114// library/Index.ets 115export { nativeAdd } from './src/main/ets/utils/nativeTest'; 116``` 117 118### Exporting Resources 119Specifically, 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): 120- AppScope (supported only by the stage model) 121- Modules in the HAP 122- 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. 123> **NOTE** 124> 125> 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 an HAR, the one in the **en_US** folder is prioritized for internationalization purposes. 126``` 127// oh-package.json5 128{ 129 "dependencies": { 130 "dayjs": "^1.10.4", 131 "lottie": "^2.0.0" 132 } 133} 134``` 135 136## Using an HAR 137 138You can reference the ArkUI components, APIs, and resources in an HAR. 139 140Before 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-V13/ide-har-import-V13). 141 142### Referencing ArkUI Components 143 144After configuring the dependency on the HAR, you can reference ArkUI components exported from the HAR by using **import**. The code snippet is as follows: 145```ts 146// entry/src/main/ets/pages/IndexSec.ets 147import { MainPage } from 'library'; 148 149@Entry 150@Component 151struct IndexSec { 152 build() { 153 Row() { 154 // Reference the ArkUI component in the HAR. 155 MainPage() 156 } 157 .height('100%') 158 } 159} 160``` 161### Referencing TS Classes and Methods 162To reference the TS classes and methods exported from the HAR, use **import** as follows: 163```ts 164// entry/src/main/ets/pages/Index.ets 165import { Log } from 'library'; 166import { func } from 'library'; 167 168@Entry 169@Component 170struct Index { 171 @State message: string = 'Hello World'; 172 173 build() { 174 Column() { 175 Text(this.message) 176 .fontFamily('HarmonyHeiTi') 177 .fontWeight(FontWeight.Bold) 178 .fontSize(32) 179 .fontWeight(700) 180 .fontColor($r('app.color.text_color')) 181 .textAlign(TextAlign.Start) 182 .margin({ top: '32px' }) 183 .width('624px') 184 185 // Reference TS classes and methods. 186 Button($r('app.string.button')) 187 .id('button') 188 .height(48) 189 .width('624px') 190 .margin({ top: '4%' }) 191 .type(ButtonType.Capsule) 192 .fontFamily('HarmonyHeiTi') 193 .borderRadius($r('sys.float.ohos_id_corner_radius_button')) 194 .backgroundColor($r('app.color.button_background')) 195 .fontColor($r('sys.color.ohos_id_color_foreground_contrary')) 196 .fontSize($r('sys.float.ohos_id_text_size_button1')) 197 .onClick(() => { 198 // Reference TS classes and methods in the HAR. 199 Log.info('har msg'); 200 this.message = 'func return: ' + func(); 201 }) 202 } 203 .width('100%') 204 .backgroundColor($r('app.color.page_background')) 205 .height('100%') 206 } 207} 208``` 209 210### Referencing Native Methods 211To reference the native methods exported from the HAR, use **import** as follows: 212```ts 213// entry/src/main/ets/pages/Index.ets 214import { nativeAdd } from 'library'; 215 216@Entry 217@Component 218struct Index { 219 @State message: string = 'Hello World'; 220 221 build() { 222 Column() { 223 Text(this.message) 224 .fontFamily('HarmonyHeiTi') 225 .fontWeight(FontWeight.Bold) 226 .fontSize(32) 227 .fontWeight(700) 228 .fontColor($r('app.color.text_color')) 229 .textAlign(TextAlign.Start) 230 .margin({ top: '32px' }) 231 .width('624px') 232 233 // Reference the native method in the HAR. 234 Button($r('app.string.native_add')) 235 .id('nativeAdd') 236 .height(48) 237 .width('624px') 238 .margin({ top: '4%', bottom: '6%' }) 239 .type(ButtonType.Capsule) 240 .fontFamily('HarmonyHeiTi') 241 .borderRadius($r('sys.float.ohos_id_corner_radius_button')) 242 .backgroundColor($r('app.color.button_background')) 243 .fontColor($r('sys.color.ohos_id_color_foreground_contrary')) 244 .fontSize($r('sys.float.ohos_id_text_size_button1')) 245 .onClick(() => { 246 this.message = 'result: ' + nativeAdd(1, 2); 247 }) 248 } 249 .width('100%') 250 .backgroundColor($r('app.color.page_background')) 251 .height('100%') 252 } 253} 254``` 255 256### Referencing Resources 257Use **$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: 258```ts 259// entry/src/main/ets/pages/Index.ets 260@Entry 261@Component 262struct Index { 263 @State message: string = 'Hello World'; 264 265 build() { 266 Column() { 267 // Reference the string in the HAR. 268 Text($r('app.string.hello_har')) 269 .id('stringHar') 270 .fontFamily('HarmonyHeiTi') 271 .fontColor($r('app.color.text_color')) 272 .fontSize(24) 273 .fontWeight(500) 274 .margin({ top: '40%' }) 275 276 List() { 277 ListItem() { 278 // Reference the image in the HAR. 279 Image($r('app.media.icon_har')) 280 .id('iconHar') 281 .borderRadius('48px') 282 } 283 .margin({ top: '5%' }) 284 .width('312px') 285 } 286 .alignListItem(ListItemAlign.Center) 287 } 288 .width('100%') 289 .backgroundColor($r('app.color.page_background')) 290 .height('100%') 291 } 292} 293``` 294## Building an HAR 295 296HAR 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). 297 298After [code obfuscation](../arkts-utils/source-obfuscation.md) is enabled, DevEco Studio compiles, obfuscates, and compresses code when building HARs to protect code assets. 299 300The 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 [Code Obfuscation](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V13/ide-build-obfuscation-V13). The configuration is as follows: 301 302 ```json 303 { 304 "apiType": "stageMode", 305 "buildOption": { 306 }, 307 "buildOptionSet": [ 308 { 309 "name": "release", 310 "arkOptions": { 311 "obfuscation": { 312 "ruleOptions": { 313 "enable": true, 314 "files": [ 315 "./obfuscation-rules.txt" 316 ] 317 }, 318 "consumerFiles": [ 319 "./consumer-rules.txt" 320 ] 321 } 322 } 323 }, 324 ], 325 "targets": [ 326 { 327 "name": "default" 328 } 329 ] 330 } 331 ``` 332 333### Building TS Files 334 335> **Scenario Description** 336> 337>Enable this configuration when using **Sendable** in an HAR. 338 339> **Constraints** 340> 341>When depend on TS HAR, the ArkUI component in TS HAR cannot be referenced. 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 ```json 346 { 347 "module": { 348 "name": "TsClosedHar", 349 "type": "har", 350 "deviceTypes": [ 351 "default", 352 "tablet", 353 "2in1" 354 ], 355 "metadata": [ 356 { 357 "name": "UseTsHar", 358 "value": "true" 359 } 360 ] 361 } 362 } 363 ``` 364 365## Publishing an HAR 366 367For details, see [Publishing a Shared Package](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V13/ide-har-publish-V13). 368