We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

HAR

A 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.

When to Use

  • Supports intra-application sharing or, after being released, intra-application sharing.
  • As a second-party library for internal applications, by being released to the OHPM private repository.
  • As a third-party library for external applications, by being released to the OHPM central repository.

Constraints

  • A HAR can only be referenced as a dependency of an application module. It cannot be installed or run independently on a device.
  • Since API version 14, the declaration of the UIAbility component in the configuration file is supported. For details about how to configure a UIAbility, see Adding a UIAbility to a Module. Starting a UIAbility in a HAR is the same as starting one in an application.
NOTE

If the startAbility 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 or HSP that depends on the HAR.

  • Since API version 18, HAR supports the declaration of the ExtensionAbility component in the configuration file. However, ExtensionAbility with entry capabilities (that is, entity.system.home and ohos.want.action.home configured for the skill tag) is not supported. For details about how to configure an ExtensionAbility in a HAR, see Adding an ExtensionAbility to a Module. For API version 17 and earlier versions, the ExtensionAbility component cannot be declared in the configuration file.
  • A HAR does not support the declaration of the pages tag in the configuration file. Still, it can include pages, which can be redirected through Navigation.
  • A 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.
  • As the HSP supports only intra-application sharing, a HAR that depends on any HSP can be shared only within the same application. Do not release such a HAR to a second-party or third-party repository for other applications to use; otherwise, build failures will occur.
  • 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.
  • A HAR can depend on other HARs or HSPs, but does not support cyclic dependency or dependency transfer.
  • When a HAP references a HAR, the system automatically combines their permission configurations during compilation and build. Therefore, you do not need to repeatedly request the same permission in the HAP and HAR.
NOTE

Cyclic dependency: For example, there are three HARs. HAR-A depends on HAR-B, HAR-B depends on HAR-C, and HAR-C depends on HAR-A.

Dependency transfer: For example, there are three HARs. HAR-A depends on HAR-B, and HAR-B depends on HAR-C. If dependency transfer is not supported, HAR-A can use the methods and components of HAR-B, but cannot directly use that of HAR-C.

Creating a HAR

You can create a HAR module for calling C++ code in DevEco Studio. During the creation, turn on Enable native on the Configure New Module page. For details, see Creating a HAR Module.

Developing a HAR

You can export the ArkUI components, APIs, and other resources of a HAR for other applications or intra-application modules to reference.

The 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:

Collapse
Word wrap
Dark theme
Copy code
  1. {
  2. // ...
  3. "main": "Index.ets",
  4. // ...
  5. }
NOTE

When compiling together with the host application, the HAR code is directly compiled into the host application. A HAR package serves as an intermediate build product rather than a final runtime entity. At runtime, the HAR operates under the identity of its host application, and the system differentiates behaviors based on the version of the host application. If it is necessary to implement version-specific behaviors for the host application within the HAR, you can call the getBundleInfoForSelf API to obtain the host application's targetVersion, and then execute different logic based on the obtained targetVersion.

Exporting ArkUI Components

Export ArkUI components using export. The code snippet is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. // library/src/main/ets/components/mainpage/MainPage.ets
  2. @Component
  3. export struct MainPage {
  4. @State message: string = 'HAR MainPage';
  5. build() {
  6. Column() {
  7. Row() {
  8. Text(this.message)
  9. .fontSize(32)
  10. .fontWeight(FontWeight.Bold)
  11. }
  12. .margin({ top: '32px' })
  13. .height(56)
  14. .width('624px')
  15. Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center, alignContent: FlexAlign.Center }) {
  16. Column() {
  17. Image($r('app.media.pic_empty')).width('33%')
  18. Text($r('app.string.empty'))
  19. .fontSize(14)
  20. .fontColor($r('app.color.text_color'))
  21. }
  22. }.width('100%')
  23. .height('90%')
  24. }
  25. .width('100%')
  26. .height('100%')
  27. .backgroundColor($r('app.color.page_background'))
  28. }
  29. }

In the Index.ets file, declare the APIs that the HAR exposes to external systems. The code snippet is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. // library/Index.ets
  2. export { MainPage } from './src/main/ets/components/mainpage/MainPage';

Exporting Classes and Methods

Use export to export classes and methods. Multiple classes and methods can be exported. The code snippet is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. // library/src/main/ets/test.ets
  2. export class Log {
  3. static info(msg: string) {
  4. console.info(msg);
  5. }
  6. }
  7. export function func() {
  8. return 'har func';
  9. }
  10. export function func2() {
  11. return 'har func2';
  12. }

In the Index.ets file, declare the APIs that the HAR exposes to external systems. The code snippet is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. // library/Index.ets
  2. export { Log, func, func2 } from './src/main/ets/test';

Exporting Native Methods

The 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.

Collapse
Word wrap
Dark theme
Copy code
  1. // library/src/main/ets/utils/nativeTest.ets
  2. import native from 'liblibrary.so';
  3. export function nativeAdd(a: number, b: number): number {
  4. let result: number = native.add(a, b);
  5. return result;
  6. }

In the Index.ets file, declare the APIs that the HAR exposes to external systems. The code snippet is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. // library/Index.ets
  2. export { nativeAdd } from './src/main/ets/utils/nativeTest';

Exporting Resources

Specifically, 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):

  • AppScope (supported only by the stage model)
  • Modules in the HAP
  • Dependent HAR modules

    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.

    NOTE

    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 a HAR, the one in the en_US folder is prioritized for internationalization purposes.

Collapse
Word wrap
Dark theme
Copy code
  1. {
  2. // ...
  3. "dependencies": {
  4. // ...
  5. "dayjs": "file:../dayjs",
  6. "lottie": "file:../lottie",
  7. },
  8. }

Using a HAR

You can reference the ArkUI components, APIs, and resources in a HAR.

Before referencing the HAR, you need to configure the dependency on it. For details, see Referencing a Shared Package.

Referencing ArkUI Components

After configuring the dependency on the HAR, you can use import to import the ArkUI component exported from HAR. The following is an example:

Collapse
Word wrap
Dark theme
Copy code
  1. // entry/src/main/ets/pages/IndexSec.ets
  2. import { MainPage } from 'library';
  3. @Entry
  4. @Component
  5. struct IndexSec {
  6. build() {
  7. Row() {
  8. // Reference the ArkUI component in the HAR.
  9. MainPage()
  10. }
  11. .height('100%')
  12. }
  13. }

Referencing ETS Classes and Methods

Use import to reference the classes and methods exported from the HAR. The code snippet is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. // entry/src/main/ets/pages/Index.ets
  2. import { Log, func } from 'library';
  3. // ...
  4. @Entry
  5. @Component
  6. struct Index {
  7. @State message: string = 'Hello World';
  8. build() {
  9. Column() {
  10. Text(this.message)
  11. .fontFamily('HarmonyHeiTi')
  12. .fontWeight(FontWeight.Bold)
  13. .fontSize(32)
  14. // Reference ETS classes and methods.
  15. Button($r('app.string.button'))
  16. .id('button')
  17. .height(48)
  18. .width('624px')
  19. .margin({ top: '4%' })
  20. .type(ButtonType.Capsule)
  21. .onClick(() => {
  22. // Reference ETS classes and methods in the HAR.
  23. Log.info('har msg');
  24. this.message = 'func return: ' + func();
  25. })
  26. // ...
  27. // ...
  28. }
  29. .width('100%')
  30. .backgroundColor($r('app.color.page_background'))
  31. .height('100%')
  32. }
  33. }

Referencing Native Methods

Use import to reference the native methods exported from the HAR. The code snippet is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. // entry/src/main/ets/pages/Index.ets
  2. import { nativeAdd } from 'library';
  3. // ...
  4. @Entry
  5. @Component
  6. struct Index {
  7. @State message: string = 'Hello World';
  8. build() {
  9. Column() {
  10. Text(this.message)
  11. .fontFamily('HarmonyHeiTi')
  12. .fontWeight(FontWeight.Bold)
  13. .fontSize(32)
  14. // ...
  15. // Reference the native method in the HAR.
  16. Button($r('app.string.native_add'))
  17. .id('nativeAdd')
  18. .height(48)
  19. .width('624px')
  20. .margin({ top: '4%', bottom: '6%' })
  21. .type(ButtonType.Capsule)
  22. .onClick(() => {
  23. this.message = 'result: ' + nativeAdd(1, 2);
  24. })
  25. // ...
  26. }
  27. .width('100%')
  28. .backgroundColor($r('app.color.page_background'))
  29. .height('100%')
  30. }
  31. }

Referencing Resources

Use $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:

Collapse
Word wrap
Dark theme
Copy code
  1. // entry/src/main/ets/pages/Index.ets
  2. @Entry
  3. @Component
  4. struct Index {
  5. @State message: string = 'Hello World';
  6. build() {
  7. Column() {
  8. Text(this.message)
  9. .fontFamily('HarmonyHeiTi')
  10. .fontWeight(FontWeight.Bold)
  11. .fontSize(32)
  12. // ...
  13. // Reference the string in the HAR.
  14. Text($r('app.string.hello_har'))
  15. .id('stringHar')
  16. .fontFamily('HarmonyHeiTi')
  17. .fontColor($r('app.color.text_color'))
  18. .fontSize(24)
  19. .fontWeight(500)
  20. .margin({ top: '40%' })
  21. List() {
  22. ListItem() {
  23. // Reference the image in the HAR.
  24. Image($r('app.media.icon_har'))
  25. .id('iconHar')
  26. .borderRadius('48px')
  27. }
  28. .margin({ top: '5%' })
  29. .width('312px')
  30. }
  31. .alignListItem(ListItemAlign.Center)
  32. }
  33. .width('100%')
  34. .backgroundColor($r('app.color.page_background'))
  35. .height('100%')
  36. }
  37. }

Building a HAR

For details, see Building a HAR.

Configuring Obfuscation

HAR 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.

After code obfuscation is enabled, DevEco Studio compiles, obfuscates, and compresses code when building HARs to protect code assets.

The 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 Using Obfuscation for Code Hardening. The configuration is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. {
  2. "apiType": "stageMode",
  3. "buildOption": {
  4. // ...
  5. },
  6. "buildOptionSet": [
  7. {
  8. "name": "release",
  9. "arkOptions": {
  10. "obfuscation": {
  11. "ruleOptions": {
  12. "enable": true,
  13. "files": [
  14. "./obfuscation-rules.txt"
  15. ]
  16. },
  17. "consumerFiles": [
  18. "./consumer-rules.txt"
  19. ]
  20. }
  21. },
  22. // ...
  23. },
  24. ],
  25. "targets": [
  26. {
  27. "name": "default"
  28. },
  29. // ...
  30. ]
  31. }

Publishing a HAR

For details, see Publishing a Shared Package.

Search in Guides
Enter a keyword.