• 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 kickstart your HAR module development with the module template of the **Library** type in DevEco Studio. By default, obfuscation is disabled for the HAR module. To enable this feature, set **artifactType** in the **build-profile.json5** file of the HAR module 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, and the default value is **original**:
16- **original**: Code is not obfuscated.
17- **obfuscation**: Code is obfuscated using Uglify.
18
19When obfuscation is enabled, DevEco Studio compiles, obfuscates, and compresses code during HAR building, thereby protecting your code assets.
20
21> **NOTE**
22>
23> If **artifactType** is set to **obfuscation**, **apiType** must be set to **stageMode**, because obfuscation is available only in the stage model.
24
25## Precautions for HAR Development
26- The HAR does not support the declaration of **abilities** and **extensionAbilities** in its configuration file.
27- The HAR does not support the declaration of pages in its configuration file.
28- The HAR does not support **worker** configuration under **buildOption** in the **build-profile.json5** file.
29- The HAR of the FA model and that of the stage model cannot be referenced by each other.
30- 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.
31
32## Exporting ArkUI Components, APIs, and Resources of the HAR
33The **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:
34```json
35{
36  "main": "index.ets"
37}
38```
39### Exporting ArkUI Components
40Use **export** to export the ArkUI components. The code snippet is as follows:
41```js
42// library/src/main/ets/components/MainPage/MainPage.ets
43@Component
44export struct MainPage {
45  @State message: string = 'Hello World'
46  build() {
47    Row() {
48      Column() {
49        Text(this.message)
50          .fontSize(50)
51          .fontWeight(FontWeight.Bold)
52      }
53      .width('100%')
54    }
55    .height('100%')
56  }
57}
58```
59In the **index.ets** file, declare the APIs that the HAR exposes to external systems. The code snippet is as follows:
60```js
61// library/index.ets
62export { MainPage } from './src/main/ets/components/MainPage/MainPage'
63```
64### Exporting TS Classes and Methods
65Use **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:
66```js
67// library/src/main/ts/test.ets
68export class Log {
69    static info(msg) {
70        console.info(msg);
71    }
72}
73
74export function func() {
75  return "har func";
76}
77
78export function func2() {
79  return "har func2";
80}
81```
82In the **index.ets** file, declare the APIs that the HAR exposes to external systems. The code snippet is as follows:
83```js
84// library/index.ets
85export { Log } from './src/main/ts/test'
86export { func } from './src/main/ts/test'
87export { func2 } from './src/main/ts/test'
88```
89### Resources
90Resources 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):
91- AppScope (supported only by the stage model of API version 9)
92- Modules in the HAP file
93- 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.)
94
95## Referencing ArkUI Components, APIs, and Resources in the HAR
96To start with, [configure dependency](https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ohos-development-npm-package-0000001222578434#section89674298391) on the HAR.
97
98### Reference ArkUI Components in the HAR
99
100After configuring the dependency on the HAR, you can reference ArkUI components exported from the HAR by using **import**. The sample code is as follows:
101```js
102// entry/src/main/ets/pages/index.ets
103import { MainPage } from "@ohos/library"
104
105@Entry
106@Component
107struct Index {
108  @State message: string = 'Hello World'
109  build() {
110    Row() {
111      // Reference the ArkUI component in the HAR.
112      MainPage()
113      Column() {
114        Text(this.message)
115          .fontSize(50)
116          .fontWeight(FontWeight.Bold)
117      }
118      .width('100%')
119    }
120    .height('100%')
121  }
122}
123```
124### Referencing TS Classes and Methods in the HAR
125To reference the TS classes and methods exported from the HAR, use **import** as follows:
126```js
127// entry/src/main/ets/pages/index.ets
128import { Log } from "@ohos/library"
129import { func } from "@ohos/library"
130
131@Entry
132@Component
133struct Index {
134  build() {
135    Row() {
136      Column() {
137        Button('Button')
138          .onClick(()=>{
139            // Reference TS classes and methods in the HAR.
140            Log.info("har msg");
141            func();
142        })
143      }
144      .width('100%')
145    }
146    .height('100%')
147  }
148}
149```
150### Referencing Resources in the HAR
151Use **$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:
152```js
153// entry/src/main/ets/pages/index.ets
154@Entry
155@Component
156struct Index {
157  build() {
158    Row() {
159      Column() {
160        // Reference the string in the HAR.
161        Text($r("app.string.hello_har"))
162          .fontSize(50)
163          .fontWeight(FontWeight.Bold)
164        // Reference the image in the HAR.
165        Image($r("app.media.icon_har"))
166      }
167      .width('100%')
168    }
169    .height('100%')
170  }
171}
172```
173