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