1# 原子化服务 2 3## 原子化服务分包预加载 4 5### 使用分包 6 7为了加快首次启动原子化服务时的速度,可以采用分包策略,原子化服务的代码包可以被划分为几个:一个是类型为entry的模块,包含原子化服务启动时会打开的页面代码和相关资源;其余是“分包”,包含其余的代码和资源。这样,启动原子化服务时,只需下载和安装entry模块,即可立刻启动原子化服务,大大降低原子化服务下载的时间。 8 9#### 配置方法 10 11原子化服务分包可以在`DevEco Studio`中创建,我们以创建一个原子化服务为例。基本的工程目录结构大致如下: 12 13``` 14├── AppScope 15| ├── resources 16| └── app.json5 17├── entry 18| └── src/main 19| ├── ets 20| ├── resources 21| └── module.json5 22├── feature 23| └── src/main 24| ├── ets 25| ├── resources 26| └── module.json5 27├── library 28| └── src/main 29| ├── ets 30| ├── resources 31| └── module.json5 32├── node_modules 33``` 34 35注意,开发者需要在[app.json5](app-configuration-file.md)中`bundleType`字段指定为`atomicService`,标识当前应用是原子化服务。`app.json5`(路径为:`AppScope/app.json5`)的内容为: 36 37```json 38{ 39 "app": { 40 "bundleName": "com.example.hmservice", 41 "bundleType":"atomicService", 42 "vendor": "example", 43 "versionCode": 1000000, 44 "versionName": "1.0.0", 45 "icon": "$media:app_icon", 46 "label": "$string:app_name", 47 "distributedNotificationEnabled": true, 48 "targetAPIVersion": 9 49 } 50} 51``` 52 53具体分包攻略,详细可参考[多hap开发](multi-hap-objective.md)。 54 55#### 限制 56 571. 原子化服务中每个模块对应的配置文件[module.json5](module-configuration-file.md)中`installationFree`字段必须为`true`。 58 592. 打包原子化服务时,需遵循以下大小校验规则: 60 61- 原子化服务中,所有包的大小总和不能超过10M。 62 63- 单个包加上其依赖的所有[共享包](in-app-hsp.md),大小不能超过2M。 64 65 66### 使用预加载 67 68开发者可以通过配置,在原子化服务进入某个模块时,由系统自动预下载可能需要的模块,从而提升进入后续模块的速度。 69 70原子化服务预加载目前只支持通过配置方式使用,暂不支持通过调用API使用。 71 72#### 配置方法 73 74原子化服务预加载行为在点击进入某个模块时,第一帧绘制结束之后触发,可以通过在相应模块的[module.json5](module-configuration-file.md)文件中配置`atomicService`标签下的`preloads`字段来控制,以下为entry模块的`module.json5`(路径为:`entry/src/main/module.json5`)文件内容: 75 76```json 77{ 78 "module": { 79 "name": "entry", 80 "type": "entry", 81 "srcEntrance": "./ets/Application/MyAbilityStage.ts", 82 "description": "$string:entry_desc", 83 "mainElement": "MainAbility", 84 "deviceTypes": [ 85 "default", 86 "tablet" 87 ], 88 "deliveryWithInstall": true, 89 "installationFree": true, 90 "pages": "$profile:main_pages", 91 "atomicService": { 92 "preloads": [ 93 { 94 "moduleName": "feature" 95 } 96 ] 97 }, 98 "abilities": [ 99 { 100 "name": "MainAbility", 101 "srcEntrance": "./ets/MainAbility/MainAbility.ts", 102 "description": "$string:MainAbility_desc", 103 "icon": "$media:icon", 104 "label": "$string:MainAbility_label", 105 "startWindowIcon": "$media:icon", 106 "startWindowBackground": "$color:white", 107 "visible": true, 108 "skills": [ 109 { 110 "entities": [ 111 "entity.system.home" 112 ], 113 "actions": [ 114 "action.system.home" 115 ] 116 } 117 ] 118 } 119 ] 120 } 121} 122``` 123 124在完成entry模块的第一帧绘制结束后,会自动执行预加载去下载和安装模块名为feature的模块。 125 126#### 限制 127 128preloads列表配置的moduleName对应的[moduleType(模块类型)](../reference/apis/js-apis-bundleManager.md#moduletype)不能为entry。 129 130## 原子化服务中使用动态共享包 131 132[Harmony动态共享包](shared-guide.md)(Harmony Shared Package),简称HSP,其可以包含其他模块公用的代码、C++库、资源和配置文件等。 133在原子化服务中使用HSP,可以参考[应用内HSP开发指导](in-app-hsp.md)。 134 135#### 配置方法 136 137假设工程目录结构如下: 138``` 139├── AppScope 140| ├── resources 141| └── app.json5 142├── entry 143| └── src/main 144| ├── ets 145| ├── entryAbility 146| └── pages 147| └── Index.ets 148| ├── resources 149| └── module.json5 150├── feature 151| └── src/main 152| ├── ets 153| ├── resources 154| └── module.json5 155├── library 156| └── src/main 157| ├── ets 158| └── pages 159| └── menu.ets 160| ├── resources 161| └── module.json5 162├── node_modules 163``` 164 165若开发者想在entry模块中,添加一个按钮跳转至library模块中的menu页面(路径为:`library/src/main/ets/pages/menu.ets`),那么可以在使用方的代码(entry模块下的Index.ets,路径为:`entry/src/main/ets/MainAbility/Index.ets`)里这样使用: 166 167```ts 168import router from '@ohos.router'; 169 170@Entry 171@Component 172struct Index { 173 @State message: string = 'Hello World' 174 175 build() { 176 Row() { 177 Column() { 178 Text(this.message) 179 .fontSize(50) 180 .fontWeight(FontWeight.Bold) 181 // 添加按钮,以响应用户点击 182 Button() { 183 Text('click to menu') 184 .fontSize(30) 185 .fontWeight(FontWeight.Bold) 186 } 187 .type(ButtonType.Capsule) 188 .margin({ 189 top: 20 190 }) 191 .backgroundColor('#0D9FFB') 192 .width('40%') 193 .height('5%') 194 // 绑定点击事件 195 .onClick(() => { 196 router.pushUrl({ 197 url: '@bundle:com.example.hmservice/library/ets/pages/menu' 198 }).then(() => { 199 console.log("push page success"); 200 }).catch(err => { 201 console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`); 202 }) 203 } 204 .width('100%') 205 } 206 .height('100%') 207 } 208} 209``` 210 211其中`router.pushUrl`方法的入参中`url`的内容为: 212```ets 213'@bundle:com.example.hmservice/library/ets/pages/menu' 214``` 215`url`内容的模板为: 216```ets 217'@bundle:包名(bundleName)/模块名(moduleName)/路径/页面所在的文件名(不加.ets后缀)' 218```