• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# HAR
2A Harmony Archive (HAR) is a static shared package that can contain code, C++ libraries, resources, and configuration files. It enables modules and projects to share code related to ArkUI components, resources, and more. Unlike a Harmony Ability Package (HAP), a HAR cannot be independently installed on a device. Instead, it can be referenced only as the dependency of an application module.
3
4## Creating a HAR Module
5You can [create a HAR module in DevEco Studio](https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/creating_har_api9-0000001518082393-V3#section143510369612). To better protect your source code, enable obfuscation for the HAR module so that DevEco Studio compiles, obfuscates, and compresses code during HAR building. To enable obfuscation, open the **build-profile.json5** file of the HAR module and set **artifactType** to **obfuscation** as follows:
6
7```json
8{
9  "apiType": "stageMode",
10  "buildOption": {
11      "artifactType": "obfuscation"
12  }
13}
14```
15The value options of **artifactType** are as follows, with the default value being **original**:
16- **original**: Code is not obfuscated.
17- **obfuscation**: Code is obfuscated using Uglify.
18
19> **NOTE**
20>
21> Obfuscation is available only in the stage model. Therefore, if **artifactType** is set to **obfuscation**, **apiType** must be set to **stageMode**.
22
23## Precautions for HAR Development
24- The HAR does not support the declaration of **abilities** and **extensionAbilities** in its configuration file.
25- The HAR does not support the declaration of pages in its configuration file.
26- The HAR does not support **worker** configuration under **buildOption** in the **build-profile.json5** file.
27- The HAR of the FA model and that of the stage model cannot be referenced by each other.
28- The HAR of the stage model cannot reference content in the **AppScope** folder. This is because the content in the **AppScope** folder is not packaged into the HAR during compilation and building.
29
30## Exporting ArkUI Components, APIs, and Resources of the HAR
31The **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:
32```json
33{
34  "main": "index.ets"
35}
36```
37### Exporting ArkUI Components
38Use **export** to export the ArkUI components. The code snippet is as follows:
39```js
40// library/src/main/ets/components/MainPage/MainPage.ets
41@Component
42export struct MainPage {
43  @State message: string = 'Hello World'
44  build() {
45    Row() {
46      Column() {
47        Text(this.message)
48          .fontSize(50)
49          .fontWeight(FontWeight.Bold)
50      }
51      .width('100%')
52    }
53    .height('100%')
54  }
55}
56```
57In the **index.ets** file, declare the APIs that the HAR exposes to external systems. The code snippet is as follows:
58```js
59// library/index.ets
60export { MainPage } from './src/main/ets/components/MainPage/MainPage'
61```
62### Exporting TS Classes and Methods
63Use **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:
64```js
65// library/src/main/ts/test.ets
66export class Log {
67    static info(msg) {
68        console.info(msg);
69    }
70}
71
72export function func() {
73  return "har func";
74}
75
76export function func2() {
77  return "har func2";
78}
79```
80In the **index.ets** file, declare the APIs that the HAR exposes to external systems. The code snippet is as follows:
81```js
82// library/index.ets
83export { Log } from './src/main/ts/test'
84export { func } from './src/main/ts/test'
85export { func2 } from './src/main/ts/test'
86```
87### Resources
88Resources are packed into the HAR when it is being compiled and packaged. During compilation and building of a HAP, DevEco Studio collects resource files from the HAP module and its dependent modules. If the resource files of different modules have the same name, DevEco Studio overwrites the resource files based on the following priorities (in descending order):
89- AppScope (only for the stage model of API version 9)
90- Modules in the HAP file
91- If resource conflicts occur between dependent HAR modules, they are overwritten based on the dependency sequence. (The module that is higher in the dependency sequence list has higher priority.)
92
93## Referencing ArkUI Components, APIs, and Resources in the HAR
94To start with, [configure dependency](https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/creating_har_api9-0000001518082393-V3#section611662614153) on the HAR.
95
96### Reference ArkUI Components in the HAR
97
98After configuring the dependency on the HAR, you can reference ArkUI components exported from the HAR by using **import**. The sample code is as follows:
99```js
100// entry/src/main/ets/pages/index.ets
101import { MainPage } from "library"
102
103@Entry
104@Component
105struct Index {
106  @State message: string = 'Hello World'
107  build() {
108    Row() {
109      // Reference the ArkUI component in the HAR.
110      MainPage()
111      Column() {
112        Text(this.message)
113          .fontSize(50)
114          .fontWeight(FontWeight.Bold)
115      }
116      .width('100%')
117    }
118    .height('100%')
119  }
120}
121```
122### Referencing TS Classes and Methods in the HAR
123To reference the TS classes and methods exported from the HAR, use **import** as follows:
124```js
125// entry/src/main/ets/pages/index.ets
126import { Log } from "library"
127import { func } from "library"
128
129@Entry
130@Component
131struct Index {
132  build() {
133    Row() {
134      Column() {
135        Button('Button')
136          .onClick(()=>{
137            // Reference TS classes and methods in the HAR.
138            Log.info("har msg");
139            func();
140        })
141      }
142      .width('100%')
143    }
144    .height('100%')
145  }
146}
147```
148### Referencing Resources in the HAR
149Use **$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:
150```js
151// entry/src/main/ets/pages/index.ets
152@Entry
153@Component
154struct Index {
155  build() {
156    Row() {
157      Column() {
158        // Reference the string in the HAR.
159        Text($r("app.string.hello_har"))
160          .fontSize(50)
161          .fontWeight(FontWeight.Bold)
162        // Reference the image in the HAR.
163        Image($r("app.media.icon_har"))
164      }
165      .width('100%')
166    }
167    .height('100%')
168  }
169}
170```
171
172## Releasing a HAR
173
174Follow the [instructions](https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/creating_har_api9-0000001518082393-V3#section1213451811512) to release a HAR.
175