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