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). 6 7To better protect your source code, enable obfuscation for the HAR module so that DevEco Studio compiles, obfuscates, and compresses code during HAR building. 8> **NOTE** 9> 10> Obfuscation is only available for ArkTS projects in the stage model. 11 12Whether obfuscation is enabled by default varies by version. 13 14- In API version 9, obfuscation is disabled by default, and can be enabled by setting **artifactType** to **obfuscation** in the **build-profile.json5** file of the HAR module. The configuration is as follows: 15 16 ```json 17 { 18 "apiType": "stageMode", 19 "buildOption": { 20 "artifactType": "obfuscation" 21 } 22 } 23 ``` 24 The value options of **artifactType** are as follows, with the default value being **original**: 25 - **original**: Code is not obfuscated. 26 - **obfuscation**: Code is obfuscated using Uglify. 27 28- In API version 10, obfuscation is enabled by default, and can be set through the **enable** field under **ruleOptions** in the **build-profile.json5** file of the HAR module. The configuration is as follows: 29 30 ```json 31 { 32 "apiType": "stageMode", 33 "buildOption": { 34 }, 35 "buildOptionSet": [ 36 { 37 "name": "release", 38 "arkOptions": { 39 "obfuscation": { 40 "ruleOptions": { 41 "enable": true, 42 "files": [ 43 "./obfuscation-rules.txt" 44 ] 45 }, 46 "consumerFiles": [ 47 "./consumer-rules.txt" 48 ] 49 } 50 } 51 }, 52 ], 53 "targets": [ 54 { 55 "name": "default" 56 } 57 ] 58 } 59 ``` 60### Adaptation Guide 61 62The **artifactType** field is forward compatible, and the original function is not affected. Yet, it is deprecated since API version 10, and you are advised to use the substitute as soon as possible. 63 64## Precautions for HAR Development 65- The HAR does not support the declaration of **abilities** and **extensionAbilities** in its configuration file. 66- The HAR does not support the declaration of pages in its configuration file. 67- The HAR does not support **worker** configuration under **buildOption** in the **build-profile.json5** file. 68- The HAR of the FA model and that of the stage model cannot be referenced by each other. 69- 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. 70 71## Exporting ArkUI Components, APIs, and Resources of the HAR 72The **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: 73```json 74{ 75 "main": "index.ets" 76} 77``` 78### Exporting ArkUI Components 79Use **export** to export the ArkUI components. The code snippet is as follows: 80```ts 81// library/src/main/ets/components/MainPage/MainPage.ets 82@Component 83export struct MainPage { 84 @State message: string = 'Hello World' 85 build() { 86 Row() { 87 Column() { 88 Text(this.message) 89 .fontSize(50) 90 .fontWeight(FontWeight.Bold) 91 } 92 .width('100%') 93 } 94 .height('100%') 95 } 96} 97``` 98In the **index.ets** file, declare the APIs that the HAR exposes to external systems. The code snippet is as follows: 99```ts 100// library/index.ets 101export { MainPage } from './src/main/ets/components/MainPage/MainPage' 102``` 103### Exporting TS Classes and Methods 104Use **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: 105```ts 106// library/src/main/ts/test.ets 107export class Log { 108 static info(msg: string) { 109 console.info(msg); 110 } 111} 112 113export function func() { 114 return "har func"; 115} 116 117export function func2() { 118 return "har func2"; 119} 120``` 121In the **index.ets** file, declare the APIs that the HAR exposes to external systems. The code snippet is as follows: 122```ts 123// library/index.ets 124export { Log } from './src/main/ts/test' 125export { func } from './src/main/ts/test' 126export { func2 } from './src/main/ts/test' 127``` 128### Resources 129Resources 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): 130- AppScope (only for the stage model of API version 9) 131- Modules in the HAP file 132- 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.) 133 134## Referencing ArkUI Components, APIs, and Resources in the HAR 135To start with, [configure dependency](https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/creating_har_api9-0000001518082393-V3#section611662614153) on the HAR. 136 137### Reference ArkUI Components in the HAR 138 139After configuring the dependency on the HAR, you can reference ArkUI components exported from the HAR by using **import**. The code snippet is as follows: 140```ts 141// entry/src/main/ets/pages/index.ets 142import { MainPage } from "library" 143 144@Entry 145@Component 146struct Index { 147 @State message: string = 'Hello World' 148 build() { 149 Row() { 150 // Reference the ArkUI component in the HAR. 151 MainPage() 152 Column() { 153 Text(this.message) 154 .fontSize(50) 155 .fontWeight(FontWeight.Bold) 156 } 157 .width('100%') 158 } 159 .height('100%') 160 } 161} 162``` 163### Referencing TS Classes and Methods in the HAR 164To reference the TS classes and methods exported from the HAR, use **import** as follows: 165```ts 166// entry/src/main/ets/pages/index.ets 167import { Log } from "library" 168import { func } from "library" 169 170@Entry 171@Component 172struct Index { 173 build() { 174 Row() { 175 Column() { 176 Button('Button') 177 .onClick(()=>{ 178 // Reference TS classes and methods in the HAR. 179 Log.info("har msg"); 180 func(); 181 }) 182 } 183 .width('100%') 184 } 185 .height('100%') 186 } 187} 188``` 189### Referencing Resources in the HAR 190Use **$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: 191```ts 192// entry/src/main/ets/pages/index.ets 193@Entry 194@Component 195struct Index { 196 build() { 197 Row() { 198 Column() { 199 // Reference the string in the HAR. 200 Text($r("app.string.hello_har")) 201 .fontSize(50) 202 .fontWeight(FontWeight.Bold) 203 // Reference the image in the HAR. 204 Image($r("app.media.icon_har")) 205 } 206 .width('100%') 207 } 208 .height('100%') 209 } 210} 211``` 212 213## Releasing a HAR 214 215Follow the [instructions](https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/creating_har_api9-0000001518082393-V3#section1213451811512) to release a HAR. 216