1# HAR转HSP指导
2目前HAR的使用存在打包多份,包膨胀的问题,导致整体应用包的体积很大,HSP可以很好地解决该问题,本文介绍了HAR转HSP的步骤,主要是通过配置项的变更将HAR工程变成HSP工程。
3## HAR转HSP的操作步骤
4
51. 修改HAR模块下的module.json5文件,修改type字段为shared,新增deliveryWithInstall和pages字段。
6    ```json
7    // MyApplication\library\src\main\module.json5
8    {
9      "module": {
10        "type": "shared",
11        "deliveryWithInstall": true,
12        "pages": "$profile:main_pages"
13        // ...
14      }
15    }
16    ```
17
182. 在resources下的base,en_US和zh_CN的element下新增一个string字段shared_desc。
19    ```json
20    // MyApplication\library\src\main\resources\base\element\string.json
21    {
22      "string": [
23        {
24          "name": "shared_desc",
25          "value": "description"
26        }
27      ]
28    }
29    ```
30
313. 在resources\base下新增profile文件夹,在profile下新增一个main_pages.json文件,并配置如下内容。
32    ```json
33    // MyApplication\library\src\main\resources\base\profile\main_pages.json
34    {
35      "src": [
36        "pages/PageIndex"
37      ]
38    }
39    ```
40
414. 在ets目录下新增pages目录,并在pages目录下新增PageIndex.ets文件,并配置如下内容。
42    ```ts
43    // MyApplication\library\src\main\ets\pages\PageIndex.ets
44    @Entry
45    @Component
46    struct PageIndex {
47      @State message: string = 'hello world';
48
49      build() {
50        Row() {
51          Column() {
52            Text(this.message)
53              .fontSize(50)
54              .fontWeight(FontWeight.Bold)
55          }
56          .width('100%')
57        }
58        .height('100%')
59      }
60    }
61    ```
62
635. 删除HAR模块的build-profile.json5文件中的consumerFiles字段配置。
64
656. 修改HAR模块的hvigorfile.ts文件,将下面内容替换该文件内容。
66    ```ts
67    // MyApplication\library\hvigorfile.ts
68    import { hspTasks } from '@ohos/hvigor-ohos-plugin';
69
70    export default {
71      system: hspTasks,  /* Built-in plugin of Hvigor. It cannot be modified. */
72      plugins:[]         /* Custom plugin to extend the functionality of Hvigor. */
73    }
74    ```
75
767. 修改oh-package.json5文件,新增packageType配置。
77    ```json
78    // MyApplication\library\oh-package.json5
79    {
80      "packageType": "InterfaceHar"
81    }
82    ```
83
848. 修改项目根目录下的配置文件build-profile.json5,在modules标签下找到library的配置,新增targets标签。
85
86    ```json
87    // MyApplication\build-profile.json5
88    "modules": [
89      {
90        "name": "library",
91        "srcPath": "./library",
92        "targets": [
93          {
94            "name": "default",
95            "applyToProducts": [
96              "default"
97            ]
98          }
99        ]
100      }
101    ]
102