js-apis-installer.md 53.5 KB
Newer Older
Z
zengyawen 已提交
1
# @ohos.bundle.installer (installer模块)
W
wanghang 已提交
2

zyjhandsome's avatar
zyjhandsome 已提交
3
> **说明:**
W
wanghang 已提交
4 5 6 7 8 9 10 11 12 13 14 15
> 本模块首批接口从API version 9 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

在设备上安装、升级和卸载应用

## 导入模块

```js
import installer from '@ohos.bundle.installer';
```

## 权限列表

J
junyi233 已提交
16 17
| 权限                           | 权限等级    | 描述             |
| ------------------------------ | ----------- | ---------------- |
J
jiangminsen 已提交
18 19 20 21
| ohos.permission.INSTALL_BUNDLE | system_core | 允许应用安装、卸载其他应用(除了企业相关应用,目前有企业InHouse应用,企业MDM应用和企业normal应用)。 |
| ohos.permission.INSTALL_ENTERPRISE_BUNDLE | system_core | 允许应用安装企业InHouse应用。 |
| ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE | system_core | 允许在企业设备上安装企业MDM应用包。 |
| ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLE | system_core | 允许在企业设备上安装企业NORMAL应用包。 |
Z
zhaogan 已提交
22 23
| ohos.permission.UNINSTALL_BUNDLE | system_core | 允许应用卸载应用。 |
| ohos.permission.RECOVER_BUNDLE | system_core | 允许应用恢复预置应用。 |
X
xsz233 已提交
24
| ohos.permission.INSTALL_SELF_BUNDLE | system_core | 允许企业MDM应用在企业设备上自升级。|
J
jiangminsen 已提交
25

W
wanghang 已提交
26

J
junyi233 已提交
27
权限等级参考[权限等级说明](../../security/accesstoken-overview.md#权限等级说明)
W
wanghang 已提交
28 29 30 31 32 33 34

## BundleInstaller.getBundleInstaller

getBundleInstaller(callback: AsyncCallback\<BundleInstaller>): void;

获取BundleInstaller对象,使用callback形式返回结果。

J
junyi233 已提交
35
**系统接口:** 此接口为系统接口。
W
wanghang 已提交
36 37 38 39 40

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**参数:**

J
junyi233 已提交
41
| 参数名   | 类型                                                         | 必填 | 说明                                                         |
W
wanghang 已提交
42
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
J
junyi233 已提交
43
| callback | AsyncCallback\<[BundleInstaller](js-apis-installer.md#BundleInstaller)> | 是   | 回调函数,获取BundleInstaller对象,err为null,data为获取到的BundleInstaller对象;否则为错误对象。 |
W
wanghang 已提交
44 45 46 47 48

**示例:**

```ts
import installer from '@ohos.bundle.installer';
49
import { BusinessError } from '@ohos.base';
W
wanghang 已提交
50 51

try {
52
    installer.getBundleInstaller((err: BusinessError, data: installer.BundleInstaller) => {
W
wanghang 已提交
53 54 55 56 57 58 59
        if (err) {
            console.error('getBundleInstaller failed:' + err.message);
        } else {
            console.info('getBundleInstaller successfully');
        }
    });
} catch (error) {
60 61
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed:' + message);
W
wanghang 已提交
62 63 64 65 66 67 68 69 70
}
```

## BundleInstaller.getBundleInstaller

getBundleInstaller(): Promise\<BundleInstaller>;

获取BundleInstaller对象,使用callback形式返回结果。

J
junyi233 已提交
71
**系统接口:** 此接口为系统接口。
W
wanghang 已提交
72 73 74 75 76 77

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**返回值:**
| 类型                                                         | 说明                                 |
| ------------------------------------------------------------ | ------------------------------------ |
Z
zhaogan 已提交
78
| Promise\<[BundleInstaller](js-apis-installer.md#BundleInstaller)> | Promise对象,返回BundleInstaller对象。 |
W
wanghang 已提交
79 80 81 82 83

**示例:**

```ts
import installer from '@ohos.bundle.installer';
84
import { BusinessError } from '@ohos.base';
W
wanghang 已提交
85 86

try {
87
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
W
wanghang 已提交
88
        console.info('getBundleInstaller successfully.');
89
    }).catch((error: BusinessError) => {
W
wanghang 已提交
90 91 92
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
93 94
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
W
wanghang 已提交
95 96 97
}
```

L
lishiyu 已提交
98
## BundleInstaller.getBundleInstallerSync<sup>10+</sup>
L
lishiyu 已提交
99 100 101 102 103 104 105 106 107 108 109 110

getBundleInstallerSync(): BundleInstaller;

获取并返回BundleInstaller对象。

**系统接口:** 此接口为系统接口。

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**返回值:**
| 类型                                                         | 说明                                 |
| ------------------------------------------------------------ | ------------------------------------ |
L
lishiyu 已提交
111
| [BundleInstaller](js-apis-installer.md#BundleInstaller) | 返回BundleInstaller对象。 |
L
lishiyu 已提交
112 113 114 115 116

**示例:**

```ts
import installer from '@ohos.bundle.installer';
117
import { BusinessError } from '@ohos.base';
L
lishiyu 已提交
118 119

try {
L
lishiyu 已提交
120 121
    installer.getBundleInstallerSync();
    console.info('getBundleInstallerSync successfully.');
L
lishiyu 已提交
122
} catch (error) {
123 124
    let message = (error as BusinessError).message;
    console.error('getBundleInstallerSync failed. Cause: ' + message);
L
lishiyu 已提交
125 126 127
}
```

W
wanghang 已提交
128 129 130 131 132
## BundleInstaller.install
install(hapFilePaths: Array&lt;string&gt;, installParam: InstallParam, callback: AsyncCallback&lt;void&gt;): void;

以异步方法安装应用,使用callback形式返回结果。

J
junyi233 已提交
133
**系统接口:** 此接口为系统接口。
W
wanghang 已提交
134

135 136
**需要权限:** ohos.permission.INSTALL_BUNDLE 或 ohos.permission.INSTALL_ENTERPRISE_BUNDLE<sup>10+</sup> 或 ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLE<sup>10+</sup> 或 ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE<sup>10+</sup>
> **说明:** 从API version 10起,可通过ohos.permission.INSTALL_ENTERPRISE_BUNDLE 或 ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLE 或 ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE权限调用此接口。
J
jiangminsen 已提交
137 138 139
>
> 安装企业应用需要ohos.permission.INSTALL_ENTERPRISE_BUNDLE权限
>
140 141 142 143
> 安装企业NORMAL应用需要ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLE或ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE权限
>
> 安装企业MDM应用需要ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE权限
>
J
jiangminsen 已提交
144
> 安装普通应用需要ohos.permission.INSTALL_BUNDLE权限
W
wanghang 已提交
145 146 147 148 149

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**参数:**

J
junyi233 已提交
150
| 参数名           | 类型                                                 | 必填 | 说明                                                         |
W
wanghang 已提交
151
| --------------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ |
zyjhandsome's avatar
zyjhandsome 已提交
152
| hapFilePaths | Array&lt;string&gt;                                  | 是   | 存储应用程序包的路径。路径应该是当前应用程序中存放HAP的数据目录。当传入的路径是一个目录时, 该目录下只能放同一个应用的HAP,且这些HAP的签名需要保持一致。 |
Z
zhaogan 已提交
153
| installParam           | [InstallParam](#installparam)                        | 是   | 指定安装所需的其他参数。                                     |
J
junyi233 已提交
154
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,安装应用成功,err为null,否则为错误对象。 |
W
wanghang 已提交
155 156 157

**错误码:**

Z
zengyawen 已提交
158
以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)
W
wanghang 已提交
159

J
junyi233 已提交
160 161 162 163 164 165 166 167 168
| 错误码ID | 错误信息                                                     |
| -------- | ------------------------------------------------------------ |
| 17700004 | The specified user ID is not found.                          |
| 17700010 | Failed to install the HAP because the HAP fails to be parsed. |
| 17700011 | Failed to install the HAP because the HAP signature fails to be verified. |
| 17700012 | Failed to install the HAP because the HAP path is invalid or the HAP is too large. |
| 17700015 | Failed to install the HAPs because they have different configuration information. |
| 17700016 | Failed to install the HAP because of insufficient system disk space. |
| 17700017 | Failed to install the HAP since the version of the HAP to install is too early. |
L
Lotol 已提交
169
| 17700018 | Failed to install because the dependent module does not exist. |
S
shilei 已提交
170
| 17700031 | Failed to install the HAP because the overlay check of the HAP is failed. |
171 172
| 17700036 | Failed to install the HSP because lacks appropriate permissions. |
| 17700039 | Failed to install because disallow install a shared bundle by hapFilePaths. |
J
junyi233 已提交
173
| 17700041 | Failed to install because enterprise device management disallow install. |
X
xsz233 已提交
174 175
| 17700042 | Failed to install the HAP because of incorrect URI in the data proxy. |
| 17700043 | Failed to install the HAP because of low APL in the non-system data proxy (required APL: system_basic or system_core). |
H
Handsome_Afraican_Man 已提交
176
| 17700044 | Failed to install the HAP because the isolationMode configured is not supported. |
H
Handsome_Afraican_Man 已提交
177
| 17700047 | Failed to install the HAP because the VersionCode to be updated is not greater than the current VersionCode. |
S
shilei 已提交
178
| 17700048 | Failed to install the HAP because the code signature verification is failed. |
179
| 17700050 | Failed to install the HAP because enterprise normal/MDM bundle cannot be installed on non-enterprise device. |
W
wanghang 已提交
180 181 182 183 184

**示例:**

```ts
import installer from '@ohos.bundle.installer';
185 186
import { BusinessError } from '@ohos.base';

W
wanghang 已提交
187
let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];
188
let installParam: installer.InstallParam = {
W
wanghang 已提交
189 190 191 192 193 194
    userId: 100,
    isKeepData: false,
    installFlag: 1,
};

try {
195 196
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.install(hapFilePaths, installParam, (err: BusinessError) => {
W
wanghang 已提交
197 198 199 200 201 202
            if (err) {
                console.error('install failed:' + err.message);
            } else {
                console.info('install successfully.');
            }
        });
203
    }).catch((error: BusinessError) => {
W
wanghang 已提交
204 205 206
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
207 208
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
W
wanghang 已提交
209 210
}
```
S
shilei 已提交
211 212 213 214 215 216 217
## BundleInstaller.install
install(hapFilePaths: Array&lt;string&gt;, callback: AsyncCallback&lt;void&gt;): void;

以异步方法安装应用,使用callback形式返回结果。

**系统接口:** 此接口为系统接口。

218 219
**需要权限:** ohos.permission.INSTALL_BUNDLE 或 ohos.permission.INSTALL_ENTERPRISE_BUNDLE<sup>10+</sup> 或 ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLE<sup>10+</sup> 或 ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE<sup>10+</sup>
> **说明:** 从API version 10起,可通过ohos.permission.INSTALL_ENTERPRISE_BUNDLE 或 ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLE 或 ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE权限调用此接口。
J
jiangminsen 已提交
220 221 222
>
> 安装企业应用需要ohos.permission.INSTALL_ENTERPRISE_BUNDLE权限
>
223 224 225 226
> 安装企业NORMAL应用需要ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLE或ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE权限
>
> 安装企业MDM应用需要ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE权限
>
J
jiangminsen 已提交
227
> 安装普通应用需要ohos.permission.INSTALL_BUNDLE权限
S
shilei 已提交
228 229 230 231 232 233 234 235

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**参数:**

| 参数名           | 类型                                                 | 必填 | 说明                                                         |
| --------------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ |
| hapFilePaths | Array&lt;string&gt;                                  | 是   | 存储应用程序包的路径。路径应该是当前应用程序中存放HAP的数据目录。当传入的路径是一个目录时, 该目录下只能放同一个应用的HAP,且这些HAP的签名需要保持一致。 |
J
junyi233 已提交
236
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,安装应用成功,err为null,否则为错误对象。 |
S
shilei 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

**错误码:**

以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

| 错误码ID | 错误信息                                                     |
| -------- | ------------------------------------------------------------ |
| 17700010 | Failed to install the HAP because the HAP fails to be parsed. |
| 17700011 | Failed to install the HAP because the HAP signature fails to be verified. |
| 17700012 | Failed to install the HAP because the HAP path is invalid or the HAP is too large. |
| 17700015 | Failed to install the HAPs because they have different configuration information. |
| 17700016 | Failed to install the HAP because of insufficient system disk space. |
| 17700017 | Failed to install the HAP since the version of the HAP to install is too early. |
| 17700018 | Failed to install because the dependent module does not exist. |
| 17700031 | Failed to install the HAP because the overlay check of the HAP is failed. |
| 17700036 | Failed to install the HSP because lacks appropriate permissions. |
| 17700039 | Failed to install because disallow install a shared bundle by hapFilePaths. |
| 17700041 | Failed to install because enterprise device management disallow install. |
X
xsz233 已提交
255 256
| 17700042 | Failed to install the HAP because of incorrect URI in the data proxy. |
| 17700043 | Failed to install the HAP because of low APL in the non-system data proxy (required APL: system_basic or system_core). |
H
Handsome_Afraican_Man 已提交
257
| 17700044 | Failed to install the HAP because the isolationMode configured is not supported. |
H
Handsome_Afraican_Man 已提交
258
| 17700047 | Failed to install the HAP because the VersionCode to be updated is not greater than the current VersionCode. |
S
shilei 已提交
259
| 17700048 | Failed to install the HAP because the code signature verification is failed. |
260
| 17700050 | Failed to install the HAP because enterprise normal/MDM bundle cannot be installed on non-enterprise device. |
S
shilei 已提交
261 262 263 264 265

**示例:**

```ts
import installer from '@ohos.bundle.installer';
266 267
import { BusinessError } from '@ohos.base';

S
shilei 已提交
268 269 270
let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];

try {
271 272
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.install(hapFilePaths, (err: BusinessError) => {
S
shilei 已提交
273 274 275 276 277 278
            if (err) {
                console.error('install failed:' + err.message);
            } else {
                console.info('install successfully.');
            }
        });
279
    }).catch((error: BusinessError) => {
S
shilei 已提交
280 281 282
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
283 284
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
S
shilei 已提交
285 286
}
```
W
wanghang 已提交
287

J
junyi233 已提交
288 289 290 291 292 293 294 295
## BundleInstaller.install

install(hapFilePaths: Array\<string\>, installParam?: InstallParam) : Promise\<void\>;

以异步方法安装应用,使用Promise形式返回结果。

**系统接口:** 此接口为系统接口。

296 297
**需要权限:** ohos.permission.INSTALL_BUNDLE 或 ohos.permission.INSTALL_ENTERPRISE_BUNDLE<sup>10+</sup> 或 ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLE<sup>10+</sup> 或 ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE<sup>10+</sup>
> **说明:** 从API version 10起,可通过ohos.permission.INSTALL_ENTERPRISE_BUNDLE 或 ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLE 或 ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE权限调用此接口。
J
jiangminsen 已提交
298 299 300
>
> 安装企业应用需要ohos.permission.INSTALL_ENTERPRISE_BUNDLE权限
>
301 302 303 304
> 安装企业NORMAL应用需要ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLE或ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE权限
>
> 安装企业MDM应用需要ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE权限
>
J
jiangminsen 已提交
305
> 安装普通应用需要ohos.permission.INSTALL_BUNDLE权限
J
junyi233 已提交
306 307 308 309 310 311 312 313

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**参数:**

| 参数名       | 类型                          | 必填 | 说明                                                         |
| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ |
| hapFilePaths | Array\<string\>               | 是   | 存储应用程序包的路径。路径应该是当前应用程序中存放HAP的数据目录。当传入的路径是一个目录时, 该目录下只能放同一个应用的HAP,且这些HAP的签名需要保持一致。 |
314
| installParam | [InstallParam](#installparam) | 否   | 指定安装所需的其他参数,默认值:参照[InstallParam](#installparam)的默认值。                                     |
J
junyi233 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339

**返回值:**

| 类型            | 说明                                   |
| --------------- | -------------------------------------- |
| Promise\<void\> | Promise对象。无返回结果的Promise对象。 |

**错误码:**

以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

| 错误码ID | 错误信息                                                     |
| -------- | ------------------------------------------------------------ |
| 17700004 | The specified user ID is not found.                          |
| 17700010 | Failed to install the HAP because the HAP fails to be parsed. |
| 17700011 | Failed to install the HAP because the HAP signature fails to be verified. |
| 17700012 | Failed to install the HAP because the HAP path is invalid or the HAP is too large. |
| 17700015 | Failed to install the HAPs because they have different configuration information. |
| 17700016 | Failed to install the HAP because of insufficient system disk space. |
| 17700017 | Failed to install the HAP since the version of the HAP to install is too early. |
| 17700018 | Failed to install because the dependent module does not exist. |
| 17700031 | Failed to install the HAP because the overlay check of the HAP is failed. |
| 17700036 | Failed to install the HSP because lacks appropriate permissions. |
| 17700039 | Failed to install because disallow install a shared bundle by hapFilePaths. |
| 17700041 | Failed to install because enterprise device management disallow install. |
X
xsz233 已提交
340 341
| 17700042 | Failed to install the HAP because of incorrect URI in the data proxy. |
| 17700043 | Failed to install the HAP because of low APL in the non-system data proxy (required APL: system_basic or system_core). |
H
Handsome_Afraican_Man 已提交
342
| 17700044 | Failed to install the HAP because the isolationMode configured is not supported. |
H
Handsome_Afraican_Man 已提交
343
| 17700047 | Failed to install the HAP because the VersionCode to be updated is not greater than the current VersionCode. |
S
shilei 已提交
344
| 17700048 | Failed to install the HAP because the code signature verification is failed. |
345
| 17700050 | Failed to install the HAP because enterprise normal/MDM bundle cannot be installed on non-enterprise device. |
J
junyi233 已提交
346 347 348 349 350

**示例:**

```ts
import installer from '@ohos.bundle.installer';
351 352
import { BusinessError } from '@ohos.base';

J
junyi233 已提交
353
let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];
354
let installParam: installer.InstallParam = {
J
junyi233 已提交
355 356 357 358 359 360
    userId: 100,
    isKeepData: false,
    installFlag: 1,
};

try {
361
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
J
junyi233 已提交
362
        data.install(hapFilePaths, installParam)
363
            .then((data: void) => {
S
shilei 已提交
364
                console.info('install successfully: ' + JSON.stringify(data));
365
        }).catch((error: BusinessError) => {
S
shilei 已提交
366
            console.error('install failed:' + error.message);
J
junyi233 已提交
367
        });
368
    }).catch((error: BusinessError) => {
J
junyi233 已提交
369 370 371
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
372 373
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
J
junyi233 已提交
374 375 376
}
```

W
wanghang 已提交
377 378 379 380 381 382
## BundleInstaller.uninstall

uninstall(bundleName: string, installParam: InstallParam, callback: AsyncCallback&lt;void&gt;): void;

以异步方法卸载应用,使用callback形式返回结果。

J
junyi233 已提交
383
**系统接口:** 此接口为系统接口。
W
wanghang 已提交
384

J
junyi233 已提交
385
**需要权限:** ohos.permission.INSTALL_BUNDLE 或 ohos.permission.UNINSTALL_BUNDLE
W
wanghang 已提交
386 387 388 389 390

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**参数:**

J
junyi233 已提交
391
| 参数名      | 类型                                                 | 必填 | 说明                                           |
W
wanghang 已提交
392
| ---------- | ---------------------------------------------------- | ---- | ---------------------------------------------- |
Z
zhaogan 已提交
393 394
| bundleName | string                                               | 是   | 待卸载应用的包名。                                           |
| installParam      | [InstallParam](#installparam)                        | 是   | 指定安装所需的其他参数。                       |
J
junyi233 已提交
395
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,卸载应用成功,err为null,否则为错误对象。 |
W
wanghang 已提交
396 397 398

**错误码:**

Z
zengyawen 已提交
399
以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)
W
wanghang 已提交
400

J
junyi233 已提交
401 402
| 错误码ID | 错误信息                                                     |
| -------- | ------------------------------------------------------------ |
S
shilei 已提交
403 404
| 17700001 | The specified bundle name is not found. |
| 17700004 | The specified user ID is not found. |
Z
zhaogan 已提交
405
| 17700020 | The specified bundle is pre-installed bundle which cannot be uninstalled. |
406
| 17700040 | The specified bundle is a shared bundle which cannot be uninstalled. |
407
| 17700045 | Failed to uninstall because enterprise device management disallow uninstall. |
W
wanghang 已提交
408 409 410 411 412

**示例:**

```ts
import installer from '@ohos.bundle.installer';
413 414
import { BusinessError } from '@ohos.base';

W
wanghang 已提交
415
let bundleName = 'com.ohos.demo';
416
let installParam: installer.InstallParam = {
W
wanghang 已提交
417 418 419 420 421 422
    userId: 100,
    isKeepData: false,
    installFlag: 1
};

try {
423 424
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.uninstall(bundleName, installParam, (err: BusinessError) => {
W
wanghang 已提交
425 426 427 428 429 430
            if (err) {
                console.error('uninstall failed:' + err.message);
            } else {
                console.info('uninstall successfully.');
            }
        });
431
    }).catch((error: BusinessError) => {
W
wanghang 已提交
432 433 434
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
435 436
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
W
wanghang 已提交
437 438 439
}
```

S
shilei 已提交
440
## BundleInstaller.uninstall
441

S
shilei 已提交
442
uninstall(bundleName: string, callback: AsyncCallback&lt;void&gt;): void;
443

S
shilei 已提交
444
以异步方法卸载应用,使用callback形式返回结果。
445

J
junyi233 已提交
446
**系统接口:** 此接口为系统接口。
447

J
junyi233 已提交
448
**需要权限:** ohos.permission.INSTALL_BUNDLE 或 ohos.permission.UNINSTALL_BUNDLE
449 450 451 452 453

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**参数:**

S
shilei 已提交
454 455 456
| 参数名      | 类型                                                 | 必填 | 说明                                           |
| ---------- | ---------------------------------------------------- | ---- | ---------------------------------------------- |
| bundleName | string                                               | 是   | 待卸载应用的包名。                                           |
J
junyi233 已提交
457
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,卸载应用成功,err为null,否则为错误对象。 |
458 459 460 461 462 463 464

**错误码:**

以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

| 错误码ID | 错误信息                                                     |
| -------- | ------------------------------------------------------------ |
S
shilei 已提交
465
| 17700001 | The specified bundle name is not found. |
466
| 17700020 | The specified bundle is pre-installed bundle which cannot be uninstalled. |
S
shilei 已提交
467
| 17700040 | The specified bundle is a shared bundle which cannot be uninstalled. |
468
| 17700045 | Failed to uninstall because enterprise device management disallow uninstall. |
469 470 471 472 473

**示例:**

```ts
import installer from '@ohos.bundle.installer';
474 475
import { BusinessError } from '@ohos.base';

S
shilei 已提交
476
let bundleName = 'com.ohos.demo';
477 478

try {
479 480
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.uninstall(bundleName, (err: BusinessError) => {
481 482 483 484 485 486
            if (err) {
                console.error('uninstall failed:' + err.message);
            } else {
                console.info('uninstall successfully.');
            }
        });
487
    }).catch((error: BusinessError) => {
488 489 490
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
491 492
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
493 494
}
```
S
shilei 已提交
495
## BundleInstaller.uninstall
496

S
shilei 已提交
497
uninstall(bundleName: string, installParam?: InstallParam) : Promise\<void\>;
498

S
shilei 已提交
499
以异步方法卸载应用,使用Promise形式返回结果。
500

J
junyi233 已提交
501
**系统接口:** 此接口为系统接口。
502

J
junyi233 已提交
503
**需要权限:** ohos.permission.INSTALL_BUNDLE 或 ohos.permission.UNINSTALL_BUNDLE
504 505 506 507 508

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**参数:**

S
shilei 已提交
509 510 511
| 参数名       | 类型                          | 必填 | 说明                                                         |
| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ |
| bundleName | string                          | 是   | 待卸载应用的包名。                                           |
512
| installParam | [InstallParam](#installparam) | 否   | 指定安装所需的其他参数,默认值:参照[InstallParam](#installparam)的默认值。                                     |
513 514 515

**返回值:**

S
shilei 已提交
516 517
| 类型            | 说明                                   |
| --------------- | -------------------------------------- |
518
| Promise\<void\> | Promise对象。无返回结果的Promise对象。 |
519 520 521 522 523 524 525

**错误码:**

以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

| 错误码ID | 错误信息                                                     |
| -------- | ------------------------------------------------------------ |
S
shilei 已提交
526
| 17700001 | The specified bundle name is not found. |
J
jiangminsen 已提交
527
| 17700004 | The specified user ID is not found. |
528
| 17700020 | The specified bundle is pre-installed bundle which cannot be uninstalled. |
S
shilei 已提交
529
| 17700040 | The specified bundle is a shared bundle which cannot be uninstalled. |
530
| 17700045 | Failed to uninstall because enterprise device management disallow uninstall. |
531 532 533 534

**示例:**
```ts
import installer from '@ohos.bundle.installer';
535 536
import { BusinessError } from '@ohos.base';

S
shilei 已提交
537
let bundleName = 'com.ohos.demo';
538
let installParam: installer.InstallParam = {
S
shilei 已提交
539 540 541
    userId: 100,
    isKeepData: false,
    installFlag: 1,
542 543 544
};

try {
545
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
S
shilei 已提交
546
        data.uninstall(bundleName, installParam)
547
            .then((data: void) => {
S
shilei 已提交
548
                console.info('uninstall successfully: ' + JSON.stringify(data));
549
        }).catch((error: BusinessError) => {
S
shilei 已提交
550
            console.error('uninstall failed:' + error.message);
551
        });
552
    }).catch((error: BusinessError) => {
553 554 555
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
556 557
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
558 559 560
}
```

W
wanghang 已提交
561 562 563 564
## BundleInstaller.recover

recover(bundleName: string, installParam: InstallParam, callback: AsyncCallback&lt;void&gt;): void;

S
shilei 已提交
565
以异步方法回滚应用到初次安装时的状态,使用callback形式返回结果。
W
wanghang 已提交
566

J
junyi233 已提交
567
**系统接口:** 此接口为系统接口。
W
wanghang 已提交
568

J
junyi233 已提交
569
**需要权限:** ohos.permission.INSTALL_BUNDLE 或 ohos.permission.RECOVER_BUNDLE
W
wanghang 已提交
570 571 572 573 574

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**参数:**

J
junyi233 已提交
575
| 参数名      | 类型                                                 | 必填 | 说明                                           |
W
wanghang 已提交
576
| ---------- | ---------------------------------------------------- | ---- | ---------------------------------------------- |
Z
zhaogan 已提交
577 578
| bundleName | string                                               | 是   | 待恢复应用的包名。                                           |
| installParam      | [InstallParam](#installparam)                        | 是   | 指定安装所需的其他参数。                       |
J
junyi233 已提交
579
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,回滚应用成功,err为null,否则为错误对象。 |
W
wanghang 已提交
580 581 582

**错误码:**

Z
zengyawen 已提交
583
以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)
W
wanghang 已提交
584

J
junyi233 已提交
585 586
| 错误码ID | 错误信息                            |
| -------- | ----------------------------------- |
S
shilei 已提交
587
| 17700001 | The specified bundle name is not found. |
J
junyi233 已提交
588
| 17700004 | The specified user ID is not found. |
W
wanghang 已提交
589 590 591 592 593

**示例:**

```ts
import installer from '@ohos.bundle.installer';
594 595
import { BusinessError } from '@ohos.base';

W
wanghang 已提交
596
let bundleName = 'com.ohos.demo';
597
let installParam: installer.InstallParam = {
W
wanghang 已提交
598 599 600 601 602 603
    userId: 100,
    isKeepData: false,
    installFlag: 1
};

try {
604 605
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.recover(bundleName, installParam, (err: BusinessError) => {
W
wanghang 已提交
606 607 608 609 610 611
            if (err) {
                console.error('recover failed:' + err.message);
            } else {
                console.info('recover successfully.');
            }
        });
612
    }).catch((error: BusinessError) => {
W
wanghang 已提交
613 614 615
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
616 617
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
W
wanghang 已提交
618 619 620
}
```

S
shilei 已提交
621 622 623 624 625 626 627 628 629

## BundleInstaller.recover

recover(bundleName: string, callback: AsyncCallback&lt;void&gt;): void;

以异步方法回滚应用到初次安装时的状态,使用callback形式返回结果。

**系统接口:** 此接口为系统接口。

J
junyi233 已提交
630
**需要权限:** ohos.permission.INSTALL_BUNDLE 或 ohos.permission.RECOVER_BUNDLE
S
shilei 已提交
631 632 633 634 635 636 637 638

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**参数:**

| 参数名      | 类型                                                 | 必填 | 说明                                           |
| ---------- | ---------------------------------------------------- | ---- | ---------------------------------------------- |
| bundleName | string                                               | 是   | 待恢复应用的包名。                               |
J
junyi233 已提交
639
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,回滚应用成功,err为null,否则为错误对象。 |
S
shilei 已提交
640 641 642 643 644 645 646 647 648 649 650 651 652

**错误码:**

以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

| 错误码ID | 错误信息                            |
| -------- | ----------------------------------- |
| 17700001 | The specified bundle name is not found. |

**示例:**

```ts
import installer from '@ohos.bundle.installer';
653 654
import { BusinessError } from '@ohos.base';

S
shilei 已提交
655 656 657
let bundleName = 'com.ohos.demo';

try {
658 659
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.recover(bundleName, (err: BusinessError) => {
S
shilei 已提交
660 661 662 663 664 665
            if (err) {
                console.error('recover failed:' + err.message);
            } else {
                console.info('recover successfully.');
            }
        });
666
    }).catch((error: BusinessError) => {
S
shilei 已提交
667 668 669
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
670 671
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
S
shilei 已提交
672 673 674 675 676 677 678 679 680 681 682
}
```

## BundleInstaller.recover

recover(bundleName: string, installParam?: InstallParam) : Promise\<void\>;

以异步方法回滚应用到初次安装时的状态,使用Promise形式返回结果。

**系统接口:** 此接口为系统接口。

J
junyi233 已提交
683
**需要权限:** ohos.permission.INSTALL_BUNDLE 或 ohos.permission.RECOVER_BUNDLE
S
shilei 已提交
684 685 686 687 688 689 690 691

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**参数:**

| 参数名       | 类型                          | 必填 | 说明                                                         |
| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ |
| bundleName | string                          | 是   | 待卸载应用的包名。                                           |
692
| installParam | [InstallParam](#installparam) | 否   | 指定安装所需的其他参数,默认值:参照[InstallParam](#installparam)的默认值。                                     |
S
shilei 已提交
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711

**返回值:**

| 类型            | 说明                                   |
| --------------- | -------------------------------------- |
| Promise\<void\> | Promise对象。无返回结果的Promise对象。 |

**错误码:**

以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

| 错误码ID | 错误信息                            |
| -------- | ----------------------------------- |
| 17700001 | The specified bundle name is not found. |
| 17700004 | The specified user ID is not found. |

**示例:**
```ts
import installer from '@ohos.bundle.installer';
712 713
import { BusinessError } from '@ohos.base';

S
shilei 已提交
714
let bundleName = 'com.ohos.demo';
715
let installParam: installer.InstallParam = {
S
shilei 已提交
716 717 718 719 720 721
    userId: 100,
    isKeepData: false,
    installFlag: 1,
};

try {
722
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
S
shilei 已提交
723
        data.recover(bundleName, installParam)
724
            .then((data: void) => {
S
shilei 已提交
725
                console.info('recover successfully: ' + JSON.stringify(data));
726
        }).catch((error: BusinessError) => {
S
shilei 已提交
727 728
            console.error('recover failed:' + error.message);
        });
729
    }).catch((error: BusinessError) => {
S
shilei 已提交
730 731 732
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
733 734
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
S
shilei 已提交
735 736 737 738 739 740 741 742 743 744 745
}
```

## BundleInstaller.uninstall<sup>10+</sup>

uninstall(uninstallParam: UninstallParam, callback : AsyncCallback\<void>) : void ;

以异步方法卸载一个共享包,使用callback形式返回结果。

**系统接口:** 此接口为系统接口。

J
junyi233 已提交
746
**需要权限:** ohos.permission.INSTALL_BUNDLE 或 ohos.permission.UNINSTALL_BUNDLE
S
shilei 已提交
747 748 749 750 751 752 753 754

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**参数:**

| 参数名         | 类型                                | 必填 | 说明                                                     |
| -------------- | ----------------------------------- | ---- | -------------------------------------------------------- |
| uninstallParam | [UninstallParam](#uninstallparam10) | 是   | 共享包卸载需指定的参数信息。                             |
J
junyi233 已提交
755
| callback       | AsyncCallback&lt;void&gt;           | 是   | 回调函数,卸载应用成功,err为null,否则为错误对象。 |
S
shilei 已提交
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770

**错误码:**

以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

| 错误码ID | 错误信息                                                     |
| -------- | ------------------------------------------------------------ |
| 17700020 | The specified bundle is pre-installed bundle which cannot be uninstalled. |
| 17700037 | The version of shared bundle is dependent on other applications. |
| 17700038 | The specified shared bundle does not exist.                  |

**示例:**

```ts
import installer from '@ohos.bundle.installer';
771 772 773 774
import { BusinessError } from '@ohos.base';

let uninstallParam: installer.UninstallParam = {
    bundleName: "com.ohos.demo",
S
shilei 已提交
775 776 777
};

try {
778 779
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.uninstall(uninstallParam, (err: BusinessError) => {
S
shilei 已提交
780 781 782 783 784 785
            if (err) {
                console.error('uninstall failed:' + err.message);
            } else {
                console.info('uninstall successfully.');
            }
        });
786
    }).catch((error: BusinessError) => {
S
shilei 已提交
787 788 789
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
790 791
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
S
shilei 已提交
792 793 794 795 796 797 798 799 800 801 802
}
```

## BundleInstaller.uninstall<sup>10+</sup>

uninstall(uninstallParam: UninstallParam) : Promise\<void>;

以异步方法卸载一个共享包,使用Promise形式返回结果。

**系统接口:** 此接口为系统接口。

J
junyi233 已提交
803
**需要权限:** ohos.permission.INSTALL_BUNDLE 或 ohos.permission.UNINSTALL_BUNDLE
S
shilei 已提交
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**参数:**

| 参数名         | 类型                                | 必填 | 说明                         |
| -------------- | ----------------------------------- | ---- | ---------------------------- |
| uninstallParam | [UninstallParam](#uninstallparam10) | 是   | 共享包卸载需指定的参数信息。 |

**返回值:**

| 类型          | 说明                                   |
| ------------- | -------------------------------------- |
| Promise\<void\> | Promise对象。无返回结果的Promise对象。 |

**错误码:**

以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

| 错误码ID | 错误信息                                                     |
| -------- | ------------------------------------------------------------ |
| 17700020 | The specified bundle is pre-installed bundle which cannot be uninstalled. |
| 17700037 | The version of shared bundle is dependent on other applications. |
| 17700038 | The specified shared bundle does not exist.                  |

**示例:**

```ts
import installer from '@ohos.bundle.installer';
833 834 835 836
import { BusinessError } from '@ohos.base';

let uninstallParam: installer.UninstallParam = {
    bundleName: "com.ohos.demo",
S
shilei 已提交
837 838 839
};

try {
840 841
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.uninstall(uninstallParam, (err: BusinessError) => {
S
shilei 已提交
842 843 844 845 846 847
            if (err) {
                console.error('uninstall failed:' + err.message);
            } else {
                console.info('uninstall successfully.');
            }
        });
848
    }).catch((error: BusinessError) => {
S
shilei 已提交
849 850 851
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
852 853
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
S
shilei 已提交
854 855
}
```
W
wangtiantian 已提交
856

X
xsz233 已提交
857 858
## BundleInstaller.updateBundleForSelf<sup>10+</sup>

J
junyi233 已提交
859
updateBundleForSelf(hapFilePaths: Array\<string\>, installParam: InstallParam, callback: AsyncCallback\<void\>): void;
X
xsz233 已提交
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874

以异步方法更新当前应用,仅限企业设备上的企业MDM应用调用,且传入的hapFilePaths中的hap必须都属于当前应用,使用callback形式返回结果。

**系统接口:** 此接口为系统接口。

**需要权限:** ohos.permission.INSTALL_SELF_BUNDLE

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**参数:**

| 参数名           | 类型                                                 | 必填 | 说明                                                         |
| --------------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ |
| hapFilePaths | Array&lt;string&gt;                                  | 是   | 存储应用程序包的路径。路径应该是当前应用程序中存放HAP的数据目录。当传入的路径是一个目录时, 该目录下只能放同一个应用的HAP,且这些HAP的签名需要保持一致。 |
| installParam           | [InstallParam](#installparam)                        | 是   | 指定安装所需的其他参数。                                     |
J
junyi233 已提交
875
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,安装应用成功,err为null,否则为错误对象。 |
X
xsz233 已提交
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905

**错误码:**

以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

| 错误码ID | 错误信息                                                     |
| -------- | ------------------------------------------------------------ |
| 17700004 | The specified user ID is not found.                          |
| 17700010 | Failed to install the HAP because the HAP fails to be parsed. |
| 17700011 | Failed to install the HAP because the HAP signature fails to be verified. |
| 17700012 | Failed to install the HAP because the HAP path is invalid or the HAP is too large. |
| 17700015 | Failed to install the HAPs because they have different configuration information. |
| 17700016 | Failed to install the HAP because of insufficient system disk space. |
| 17700017 | Failed to install the HAP since the version of the HAP to install is too early. |
| 17700018 | Failed to install because the dependent module does not exist. |
| 17700039 | Failed to install because disallow install a shared bundle by hapFilePaths. |
| 17700041 | Failed to install because enterprise device management disallow install. |
| 17700042 | Failed to install the HAP because of incorrect URI in the data proxy. |
| 17700043 | Failed to install the HAP because of low APL in the non-system data proxy (required APL: system_basic or system_core). |
| 17700044 | Failed to install the HAP because the isolationMode configured is not supported. |
| 17700047 | Failed to install the HAP because the VersionCode to be updated is not greater than the current VersionCode. |
| 17700048 | Failed to install the HAP because the code signature verification is failed. |
| 17700049 | Failed to install the HAP because the bundleName is different from the bundleName of the caller application. |
| 17700050 | Failed to install the HAP because enterprise normal/MDM bundle cannot be installed on non-enterprise device. |
| 17700051 | Failed to install the HAP because the distribution type of caller application is not enterprise_mdm. |

**示例:**

```ts
import installer from '@ohos.bundle.installer';
906 907
import { BusinessError } from '@ohos.base';

X
xsz233 已提交
908
let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];
909
let installParam: installer.InstallParam = {
X
xsz233 已提交
910 911 912 913 914 915
    userId: 100,
    isKeepData: false,
    installFlag: 1,
};

try {
916 917
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.updateBundleForSelf(hapFilePaths, installParam, (err: BusinessError) => {
X
xsz233 已提交
918 919 920 921 922 923
            if (err) {
                console.error('updateBundleForSelf failed:' + err.message);
            } else {
                console.info('updateBundleForSelf successfully.');
            }
        });
924
    }).catch((error: BusinessError) => {
X
xsz233 已提交
925 926 927
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
928 929
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
X
xsz233 已提交
930 931 932 933 934
}
```

## BundleInstaller.updateBundleForSelf<sup>10+</sup>

J
junyi233 已提交
935
updateBundleForSelf(hapFilePaths: Array\<string\>, callback: AsyncCallback\<void\>): void;
X
xsz233 已提交
936 937 938 939 940 941 942 943 944 945 946 947 948 949

以异步方法更新当前应用,仅限企业设备上的企业MDM应用调用,且传入的hapFilePaths中的hap必须都属于当前应用,使用callback形式返回结果。

**系统接口:** 此接口为系统接口。

**需要权限:** ohos.permission.INSTALL_SELF_BUNDLE

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**参数:**

| 参数名           | 类型                                                 | 必填 | 说明                                                         |
| --------------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ |
| hapFilePaths | Array&lt;string&gt;                                  | 是   | 存储应用程序包的路径。路径应该是当前应用程序中存放HAP的数据目录。当传入的路径是一个目录时, 该目录下只能放同一个应用的HAP,且这些HAP的签名需要保持一致。 |
J
junyi233 已提交
950
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,安装应用成功,err为null,否则为错误对象。 |
X
xsz233 已提交
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979

**错误码:**

以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

| 错误码ID | 错误信息                                                     |
| -------- | ------------------------------------------------------------ |
| 17700010 | Failed to install the HAP because the HAP fails to be parsed. |
| 17700011 | Failed to install the HAP because the HAP signature fails to be verified. |
| 17700012 | Failed to install the HAP because the HAP path is invalid or the HAP is too large. |
| 17700015 | Failed to install the HAPs because they have different configuration information. |
| 17700016 | Failed to install the HAP because of insufficient system disk space. |
| 17700017 | Failed to install the HAP since the version of the HAP to install is too early. |
| 17700018 | Failed to install because the dependent module does not exist. |
| 17700039 | Failed to install because disallow install a shared bundle by hapFilePaths. |
| 17700041 | Failed to install because enterprise device management disallow install. |
| 17700042 | Failed to install the HAP because of incorrect URI in the data proxy. |
| 17700043 | Failed to install the HAP because of low APL in the non-system data proxy (required APL: system_basic or system_core). |
| 17700044 | Failed to install the HAP because the isolationMode configured is not supported. |
| 17700047 | Failed to install the HAP because the VersionCode to be updated is not greater than the current VersionCode. |
| 17700048 | Failed to install the HAP because the code signature verification is failed. |
| 17700049 | Failed to install the HAP because the bundleName is different from the bundleName of the caller application. |
| 17700050 | Failed to install the HAP because enterprise normal/MDM bundle cannot be installed on non-enterprise device. |
| 17700051 | Failed to install the HAP because the distribution type of caller application is not enterprise_mdm. |

**示例:**

```ts
import installer from '@ohos.bundle.installer';
980 981
import { BusinessError } from '@ohos.base';

X
xsz233 已提交
982 983 984
let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];

try {
985 986
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.updateBundleForSelf(hapFilePaths, (err: BusinessError) => {
X
xsz233 已提交
987 988 989 990 991 992
            if (err) {
                console.error('updateBundleForSelf failed:' + err.message);
            } else {
                console.info('updateBundleForSelf successfully.');
            }
        });
993
    }).catch((error: BusinessError) => {
X
xsz233 已提交
994 995 996
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
997 998
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
X
xsz233 已提交
999 1000 1001 1002 1003
}
```

## BundleInstaller.updateBundleForSelf<sup>10+</sup>

J
junyi233 已提交
1004
updateBundleForSelf(hapFilePaths: Array\<string\>, installParam?: InstallParam): Promise\<void\>;
X
xsz233 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049

以异步方法更新当前应用,仅限企业设备上的企业MDM应用调用,且传入的hapFilePaths中的hap必须都属于当前应用,使用promise形式返回结果。

**系统接口:** 此接口为系统接口。

**需要权限:** ohos.permission.INSTALL_SELF_BUNDLE

**系统能力:** SystemCapability.BundleManager.BundleFramework.Core

**参数:**

| 参数名           | 类型                                                 | 必填 | 说明                                                         |
| --------------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ |
| hapFilePaths | Array&lt;string&gt;                                  | 是   | 存储应用程序包的路径。路径应该是当前应用程序中存放HAP的数据目录。当传入的路径是一个目录时, 该目录下只能放同一个应用的HAP,且这些HAP的签名需要保持一致。 |
| installParam | [InstallParam](#installparam) | 否   | 指定安装所需的其他参数,默认值:参照[InstallParam](#installparam)的默认值。                                     |

**错误码:**

以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)

| 错误码ID | 错误信息                                                     |
| -------- | ------------------------------------------------------------ |
| 17700004 | The specified user ID is not found.                          |
| 17700010 | Failed to install the HAP because the HAP fails to be parsed. |
| 17700011 | Failed to install the HAP because the HAP signature fails to be verified. |
| 17700012 | Failed to install the HAP because the HAP path is invalid or the HAP is too large. |
| 17700015 | Failed to install the HAPs because they have different configuration information. |
| 17700016 | Failed to install the HAP because of insufficient system disk space. |
| 17700017 | Failed to install the HAP since the version of the HAP to install is too early. |
| 17700018 | Failed to install because the dependent module does not exist. |
| 17700039 | Failed to install because disallow install a shared bundle by hapFilePaths. |
| 17700041 | Failed to install because enterprise device management disallow install. |
| 17700042 | Failed to install the HAP because of incorrect URI in the data proxy. |
| 17700043 | Failed to install the HAP because of low APL in the non-system data proxy (required APL: system_basic or system_core). |
| 17700044 | Failed to install the HAP because the isolationMode configured is not supported. |
| 17700047 | Failed to install the HAP because the VersionCode to be updated is not greater than the current VersionCode. |
| 17700048 | Failed to install the HAP because the code signature verification is failed. |
| 17700049 | Failed to install the HAP because the bundleName is different from the bundleName of the caller application. |
| 17700050 | Failed to install the HAP because enterprise normal/MDM bundle cannot be installed on non-enterprise device. |
| 17700051 | Failed to install the HAP because the distribution type of caller application is not enterprise_mdm. |

**示例:**

```ts
import installer from '@ohos.bundle.installer';
1050 1051
import { BusinessError } from '@ohos.base';

X
xsz233 已提交
1052
let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];
1053
let installParam: installer.InstallParam = {
X
xsz233 已提交
1054 1055 1056 1057 1058 1059
    userId: 100,
    isKeepData: false,
    installFlag: 1,
};

try {
1060
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
X
xsz233 已提交
1061
        data.updateBundleForSelf(hapFilePaths, installParam)
1062
            .then((data: void) => {
X
xsz233 已提交
1063
                console.info('updateBundleForSelf successfully: ' + JSON.stringify(data));
1064
        }).catch((error: BusinessError) => {
X
xsz233 已提交
1065 1066
            console.error('updateBundleForSelf failed:' + error.message);
        });
1067
    }).catch((error: BusinessError) => {
X
xsz233 已提交
1068 1069 1070
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
1071 1072
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
X
xsz233 已提交
1073 1074 1075
}
```

W
wanghang 已提交
1076 1077
## HashParam

Z
zhaogan 已提交
1078
应用程序安装卸载哈希参数信息。
W
wanghang 已提交
1079 1080 1081

 **系统能力:** SystemCapability.BundleManager.BundleFramework.Core

J
junyi233 已提交
1082
 **系统接口:** 此接口为系统接口。
W
wanghang 已提交
1083

J
junyi233 已提交
1084 1085 1086 1087
| 名称     | 类型   | 必填 | 说明             |
| ---------- | ------ | ---------------- | ---------------- |
| moduleName | string | 是 | 应用程序模块名称。 |
| hashValue  | string | 是 | 哈希值。           |
W
wanghang 已提交
1088 1089 1090

## InstallParam

Z
zhaogan 已提交
1091
应用程序安装、卸载或恢复需指定的参数信息。
W
wanghang 已提交
1092 1093 1094

 **系统能力:** SystemCapability.BundleManager.BundleFramework.Core

J
junyi233 已提交
1095
 **系统接口:** 此接口为系统接口。
W
wanghang 已提交
1096

J
junyi233 已提交
1097 1098
| 名称                        | 类型                           | 必填                         | 说明               |
| ------------------------------ | ------------------------------ | ------------------ | ------------------ |
1099
| userId                         | number                         | 否                        | 指示用户id,默认值:调用方所在用户,取值范围:大于等于0,可使用[queryOsAccountLocalIdFromProcess](js-apis-osAccount.md#getOsAccountLocalId)获取当前进程所在用户。 |
J
junyi233 已提交
1100
| installFlag                    | number                         | 否                        | 指示安装标志,枚举值:0x00:应用初次安装,0x01:应用覆盖安装,0x10:应用免安装,默认值为应用初次安装。 |
1101 1102
| isKeepData                     | boolean                        | 否                       | 卸载时是否保留数据目录,默认值为false。 |
| hashParams        | Array<[HashParam](#hashparam)> | 否 | 哈希值参数,默认值为空。         |
时睿 已提交
1103
| crowdtestDeadline| number                         | 否                        | 众测活动的截止日期,默认值为-1,表示无截止日期约束。 |
1104
| sharedBundleDirPaths<sup>10+</sup> | Array\<String> | 否 |共享包文件所在路径,默认值为空。 |
W
wangtiantian 已提交
1105 1106
| specifiedDistributionType<sup>10+</sup> | string | 否 |应用安装时指定的分发类型,默认值为空,最大长度为128字节。该字段通常由操作系统运营方的应用市场指定。 |
| additionalInfo<sup>10+</sup> | string | 否 |应用安装时的额外信息,默认值为空,最大长度为3000字节。该字段通常由操作系统运营方的应用市场在安装企业应用时指定,用于保存应用的额外信息。 |
S
shilei 已提交
1107
| verifyCodeParams<sup>10+</sup> | Array<[VerifyCodeParam](#verifycodeparam10)> | 否 | 代码签名文件参数,默认值为空。         |
1108 1109 1110 1111 1112 1113 1114

## UninstallParam<sup>10+</sup>

共享包卸载需指定的参数信息。

 **系统能力:** SystemCapability.BundleManager.BundleFramework.Core

J
junyi233 已提交
1115
 **系统接口:** 此接口为系统接口。
1116 1117 1118 1119

| 名称        | 类型   | 必填 | 说明                                                         |
| ----------- | ------ | ---- | ------------------------------------------------------------ |
| bundleName  | string | 是   | 共享包包名。                                                 |
S
shilei 已提交
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
| versionCode | number | 否   | 指示共享包的版本号。默认值:如果不填写versionCode,则卸载该包名的所有共享包。 |

## VerifyCodeParam<sup>10+</sup>

应用程序代码签名文件信息。

 **系统能力:** SystemCapability.BundleManager.BundleFramework.Core

 **系统接口:** 此接口为系统接口。

| 名称     | 类型   | 必填 | 说明             |
| ---------- | ------ | ---------------- | ---------------- |
| moduleName | string | 是 | 应用程序模块名称。 |
| signatureFilePath  | string | 是 | 代码签名文件路径。           |