• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# HAR转HSP指导
2<!--Kit: Ability Kit-->
3<!--Subsystem: BundleManager-->
4<!--Owner: @wanghang904-->
5<!--Designer: @hanfeng6-->
6<!--Tester: @kongjing2-->
7<!--Adviser: @Brilliantry_Rui-->
8
9目前HAR的使用存在打包多份,包膨胀的问题,导致整体应用包的体积很大,HSP可以很好地解决该问题,本文介绍如何通过配置项的变更将HAR工程转换为HSP工程。
10## HAR转HSP的操作步骤
11
121. 修改HAR模块下的module.json5文件,将type字段设置为shared,并新增deliveryWithInstall和pages字段。
13    ```json
14    // MyApplication\library\src\main\module.json5
15    {
16      "module": {
17        "type": "shared",
18        "deliveryWithInstall": true,
19        "pages": "$profile:main_pages"
20        // ...
21      }
22    }
23    ```
24
252. 在resources\base下新增profile文件夹,在profile下新增一个main_pages.json文件,并配置如下内容。
26    ```json
27    // MyApplication\library\src\main\resources\base\profile\main_pages.json
28    {
29      "src": [
30        "pages/PageIndex"
31      ]
32    }
33    ```
34
353. 在ets目录下新增pages目录,并在pages目录下新增PageIndex.ets文件,配置如下内容。
36    ```ts
37    // MyApplication\library\src\main\ets\pages\PageIndex.ets
38    @Entry
39    @Component
40    struct PageIndex {
41      @State message: string = 'hello world';
42
43      build() {
44        Row() {
45          Column() {
46            Text(this.message)
47              .fontSize(50)
48              .fontWeight(FontWeight.Bold)
49          }
50          .width('100%')
51        }
52        .height('100%')
53      }
54    }
55    ```
56
574. 删除HAR模块的build-profile.json5文件中的consumerFiles字段配置。
58
595. 修改HAR模块的hvigorfile.ts文件,将以下内容替换文件内容。
60    ```ts
61    // MyApplication\library\hvigorfile.ts
62    import { hspTasks } from '@ohos/hvigor-ohos-plugin';
63
64    export default {
65      system: hspTasks,  /* Built-in plugin of Hvigor. It cannot be modified. */
66      plugins:[]         /* Custom plugin to extend the functionality of Hvigor. */
67    }
68    ```
69
706. 修改oh-package.json5文件,新增packageType配置。
71    ```json
72    // MyApplication\library\oh-package.json5
73    {
74      "packageType": "InterfaceHar"
75    }
76    ```
77
787. 修改项目根目录下的build-profile.json5文件,在modules标签下找到library的配置,新增targets标签。
79
80    ```json
81    // MyApplication\build-profile.json5
82    "modules": [
83      {
84        "name": "library",
85        "srcPath": "./library",
86        "targets": [
87          {
88            "name": "default",
89            "applyToProducts": [
90              "default"
91            ]
92          }
93        ]
94      }
95    ]
96