js-apis-installer.md 32.4 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
| 权限                           | 权限等级    | 描述             |
| ------------------------------ | ----------- | ---------------- |
Z
zhaogan 已提交
18
| ohos.permission.INSTALL_BUNDLE | system_core | 可安装、卸载应用。 |
W
wanghang 已提交
19

J
junyi233 已提交
20
权限等级参考[权限等级说明](../../security/accesstoken-overview.md#权限等级说明)
W
wanghang 已提交
21 22 23 24 25 26 27

## BundleInstaller.getBundleInstaller

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

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

J
junyi233 已提交
28
**系统接口:** 此接口为系统接口。
W
wanghang 已提交
29 30 31 32 33

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

**参数:**

J
junyi233 已提交
34
| 参数名   | 类型                                                         | 必填 | 说明                                                         |
W
wanghang 已提交
35
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
Z
zhaogan 已提交
36
| callback | AsyncCallback\<[BundleInstaller](js-apis-installer.md#BundleInstaller)> | 是   | 回调函数,获取BundleInstaller对象,err为undefined,data为获取到的BundleInstaller对象;否则为错误对象。 |
W
wanghang 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

**示例:**

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

try {
    installer.getBundleInstaller((err, data) => {
        if (err) {
            console.error('getBundleInstaller failed:' + err.message);
        } else {
            console.info('getBundleInstaller successfully');
        }
    });
} catch (error) {
    console.error('getBundleInstaller failed:' + error.message);
}
```

## BundleInstaller.getBundleInstaller

getBundleInstaller(): Promise\<BundleInstaller>;

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

J
junyi233 已提交
62
**系统接口:** 此接口为系统接口。
W
wanghang 已提交
63 64 65 66 67 68

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

**返回值:**
| 类型                                                         | 说明                                 |
| ------------------------------------------------------------ | ------------------------------------ |
Z
zhaogan 已提交
69
| Promise\<[BundleInstaller](js-apis-installer.md#BundleInstaller)> | Promise对象,返回BundleInstaller对象。 |
W
wanghang 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

**示例:**

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

try {
    installer.getBundleInstaller().then((data) => {
        console.info('getBundleInstaller successfully.');
    }).catch((error) => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    console.error('getBundleInstaller failed. Cause: ' + error.message);
}
```

## BundleInstaller.install
install(hapFilePaths: Array&lt;string&gt;, installParam: InstallParam, callback: AsyncCallback&lt;void&gt;): void;

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

J
junyi233 已提交
92
**系统接口:** 此接口为系统接口。
W
wanghang 已提交
93 94 95 96 97 98 99

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

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

**参数:**

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

**错误码:**

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

J
junyi233 已提交
110 111 112 113 114 115 116 117 118
| 错误码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 已提交
119
| 17700018 | Failed to install because the dependent module does not exist. |
S
shilei 已提交
120
| 17700031 | Failed to install the HAP because the overlay check of the HAP is failed. |
121 122
| 17700036 | Failed to install the HSP because lacks appropriate permissions. |
| 17700039 | Failed to install because disallow install a shared bundle by hapFilePaths. |
J
junyi233 已提交
123
| 17700041 | Failed to install because enterprise device management disallow install. |
X
xsz233 已提交
124 125
| 17700042 | Failed to install because the uri in data proxy is wrong. |
| 17700043 | Failed to install because the apl of permission in non-system data proxy should be system_basic or system_core. |
H
Handsome_Afraican_Man 已提交
126
| 17700044 | Failed to install because the isolationMode does not match the system. |
W
wanghang 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

**示例:**

```ts
import installer from '@ohos.bundle.installer';
let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];
let installParam = {
    userId: 100,
    isKeepData: false,
    installFlag: 1,
};

try {
    installer.getBundleInstaller().then(data => {
        data.install(hapFilePaths, installParam, err => {
            if (err) {
                console.error('install failed:' + err.message);
            } else {
                console.info('install successfully.');
            }
        });
    }).catch(error => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    console.error('getBundleInstaller failed. Cause: ' + error.message);
}
```
S
shilei 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
## BundleInstaller.install
install(hapFilePaths: Array&lt;string&gt;, callback: AsyncCallback&lt;void&gt;): void;

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

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

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

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

**参数:**

| 参数名           | 类型                                                 | 必填 | 说明                                                         |
| --------------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ |
| hapFilePaths | Array&lt;string&gt;                                  | 是   | 存储应用程序包的路径。路径应该是当前应用程序中存放HAP的数据目录。当传入的路径是一个目录时, 该目录下只能放同一个应用的HAP,且这些HAP的签名需要保持一致。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,安装应用成功,err为undefined,否则为错误对象。 |

**错误码:**

以下错误码的详细介绍请参见[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 已提交
190 191
| 17700042 | Failed to install because the uri in data proxy is wrong. |
| 17700043 | Failed to install because the apl of permission in non-system data proxy should be system_basic or system_core. |
H
Handsome_Afraican_Man 已提交
192
| 17700044 | Failed to install because the isolationMode does not match the system. |
S
shilei 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

**示例:**

```ts
import installer from '@ohos.bundle.installer';
let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];

try {
    installer.getBundleInstaller().then(data => {
        data.install(hapFilePaths, err => {
            if (err) {
                console.error('install failed:' + err.message);
            } else {
                console.info('install successfully.');
            }
        });
    }).catch(error => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    console.error('getBundleInstaller failed. Cause: ' + error.message);
}
```
W
wanghang 已提交
216

J
junyi233 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
## BundleInstaller.install

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

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

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

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

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

**参数:**

| 参数名       | 类型                          | 必填 | 说明                                                         |
| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ |
| hapFilePaths | Array\<string\>               | 是   | 存储应用程序包的路径。路径应该是当前应用程序中存放HAP的数据目录。当传入的路径是一个目录时, 该目录下只能放同一个应用的HAP,且这些HAP的签名需要保持一致。 |
234
| installParam | [InstallParam](#installparam) | 否   | 指定安装所需的其他参数,默认值:参照[InstallParam](#installparam)的默认值。                                     |
J
junyi233 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

**返回值:**

| 类型            | 说明                                   |
| --------------- | -------------------------------------- |
| 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 已提交
260 261
| 17700042 | Failed to install because the uri in data proxy is wrong. |
| 17700043 | Failed to install because the apl of permission in non-system data proxy should be system_basic or system_core. |
H
Handsome_Afraican_Man 已提交
262
| 17700044 | Failed to install because the isolationMode does not match the system. |
J
junyi233 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

**示例:**

```ts
import installer from '@ohos.bundle.installer';
let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];
let installParam = {
    userId: 100,
    isKeepData: false,
    installFlag: 1,
};

try {
    installer.getBundleInstaller().then(data => {
        data.install(hapFilePaths, installParam)
            .then((data) => {
S
shilei 已提交
279
                console.info('install successfully: ' + JSON.stringify(data));
J
junyi233 已提交
280
        }).catch((error) => {
S
shilei 已提交
281
            console.error('install failed:' + error.message);
J
junyi233 已提交
282 283 284 285 286 287 288 289 290
        });
    }).catch(error => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    console.error('getBundleInstaller failed. Cause: ' + error.message);
}
```

W
wanghang 已提交
291 292 293 294 295 296
## BundleInstaller.uninstall

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

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

J
junyi233 已提交
297
**系统接口:** 此接口为系统接口。
W
wanghang 已提交
298 299 300 301 302 303 304

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

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

**参数:**

J
junyi233 已提交
305
| 参数名      | 类型                                                 | 必填 | 说明                                           |
W
wanghang 已提交
306
| ---------- | ---------------------------------------------------- | ---- | ---------------------------------------------- |
Z
zhaogan 已提交
307 308 309
| bundleName | string                                               | 是   | 待卸载应用的包名。                                           |
| installParam      | [InstallParam](#installparam)                        | 是   | 指定安装所需的其他参数。                       |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,卸载应用成功,err为undefined,否则为错误对象。 |
W
wanghang 已提交
310 311 312

**错误码:**

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

J
junyi233 已提交
315 316
| 错误码ID | 错误信息                                                     |
| -------- | ------------------------------------------------------------ |
S
shilei 已提交
317 318
| 17700001 | The specified bundle name is not found. |
| 17700004 | The specified user ID is not found. |
Z
zhaogan 已提交
319
| 17700020 | The specified bundle is pre-installed bundle which cannot be uninstalled. |
320
| 17700040 | The specified bundle is a shared bundle which cannot be uninstalled. |
W
wanghang 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

**示例:**

```ts
import installer from '@ohos.bundle.installer';
let bundleName = 'com.ohos.demo';
let installParam = {
    userId: 100,
    isKeepData: false,
    installFlag: 1
};

try {
    installer.getBundleInstaller().then(data => {
        data.uninstall(bundleName, installParam, err => {
            if (err) {
                console.error('uninstall failed:' + err.message);
            } else {
                console.info('uninstall successfully.');
            }
        });
    }).catch(error => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    console.error('getBundleInstaller failed. Cause: ' + error.message);
}
```

S
shilei 已提交
350
## BundleInstaller.uninstall
351

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

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

J
junyi233 已提交
356
**系统接口:** 此接口为系统接口。
357 358 359 360 361 362 363

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

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

**参数:**

S
shilei 已提交
364 365 366 367
| 参数名      | 类型                                                 | 必填 | 说明                                           |
| ---------- | ---------------------------------------------------- | ---- | ---------------------------------------------- |
| bundleName | string                                               | 是   | 待卸载应用的包名。                                           |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,卸载应用成功,err为undefined,否则为错误对象。 |
368 369 370 371 372 373 374

**错误码:**

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

| 错误码ID | 错误信息                                                     |
| -------- | ------------------------------------------------------------ |
S
shilei 已提交
375
| 17700001 | The specified bundle name is not found. |
376
| 17700020 | The specified bundle is pre-installed bundle which cannot be uninstalled. |
S
shilei 已提交
377
| 17700040 | The specified bundle is a shared bundle which cannot be uninstalled. |
378 379 380 381 382

**示例:**

```ts
import installer from '@ohos.bundle.installer';
S
shilei 已提交
383
let bundleName = 'com.ohos.demo';
384 385 386

try {
    installer.getBundleInstaller().then(data => {
S
shilei 已提交
387
        data.uninstall(bundleName, err => {
388 389 390 391 392 393 394 395 396 397 398 399 400
            if (err) {
                console.error('uninstall failed:' + err.message);
            } else {
                console.info('uninstall successfully.');
            }
        });
    }).catch(error => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    console.error('getBundleInstaller failed. Cause: ' + error.message);
}
```
S
shilei 已提交
401
## BundleInstaller.uninstall
402

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

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

J
junyi233 已提交
407
**系统接口:** 此接口为系统接口。
408 409 410 411 412 413 414

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

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

**参数:**

S
shilei 已提交
415 416 417
| 参数名       | 类型                          | 必填 | 说明                                                         |
| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ |
| bundleName | string                          | 是   | 待卸载应用的包名。                                           |
418
| installParam | [InstallParam](#installparam) | 否   | 指定安装所需的其他参数,默认值:参照[InstallParam](#installparam)的默认值。                                     |
419 420 421

**返回值:**

S
shilei 已提交
422 423
| 类型            | 说明                                   |
| --------------- | -------------------------------------- |
424
| Promise\<void\> | Promise对象。无返回结果的Promise对象。 |
425 426 427 428 429 430 431

**错误码:**

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

| 错误码ID | 错误信息                                                     |
| -------- | ------------------------------------------------------------ |
S
shilei 已提交
432
| 17700001 | The specified bundle name is not found. |
J
jiangminsen 已提交
433
| 17700004 | The specified user ID is not found. |
434
| 17700020 | The specified bundle is pre-installed bundle which cannot be uninstalled. |
S
shilei 已提交
435
| 17700040 | The specified bundle is a shared bundle which cannot be uninstalled. |
436 437 438 439

**示例:**
```ts
import installer from '@ohos.bundle.installer';
S
shilei 已提交
440 441 442 443 444
let bundleName = 'com.ohos.demo';
let installParam = {
    userId: 100,
    isKeepData: false,
    installFlag: 1,
445 446 447 448
};

try {
    installer.getBundleInstaller().then(data => {
S
shilei 已提交
449 450 451 452 453
        data.uninstall(bundleName, installParam)
            .then((data) => {
                console.info('uninstall successfully: ' + JSON.stringify(data));
        }).catch((error) => {
            console.error('uninstall failed:' + error.message);
454 455 456 457 458 459 460 461 462
        });
    }).catch(error => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    console.error('getBundleInstaller failed. Cause: ' + error.message);
}
```

W
wanghang 已提交
463 464 465 466
## BundleInstaller.recover

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

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

J
junyi233 已提交
469
**系统接口:** 此接口为系统接口。
W
wanghang 已提交
470 471 472 473 474 475 476

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

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

**参数:**

J
junyi233 已提交
477
| 参数名      | 类型                                                 | 必填 | 说明                                           |
W
wanghang 已提交
478
| ---------- | ---------------------------------------------------- | ---- | ---------------------------------------------- |
Z
zhaogan 已提交
479 480 481
| bundleName | string                                               | 是   | 待恢复应用的包名。                                           |
| installParam      | [InstallParam](#installparam)                        | 是   | 指定安装所需的其他参数。                       |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,回滚应用成功,err为undefined,否则为错误对象。 |
W
wanghang 已提交
482 483 484

**错误码:**

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

J
junyi233 已提交
487 488
| 错误码ID | 错误信息                            |
| -------- | ----------------------------------- |
S
shilei 已提交
489
| 17700001 | The specified bundle name is not found. |
J
junyi233 已提交
490
| 17700004 | The specified user ID is not found. |
W
wanghang 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519

**示例:**

```ts
import installer from '@ohos.bundle.installer';
let bundleName = 'com.ohos.demo';
let installParam = {
    userId: 100,
    isKeepData: false,
    installFlag: 1
};

try {
    installer.getBundleInstaller().then(data => {
        data.recover(bundleName, installParam, err => {
            if (err) {
                console.error('recover failed:' + err.message);
            } else {
                console.info('recover successfully.');
            }
        });
    }).catch(error => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    console.error('getBundleInstaller failed. Cause: ' + error.message);
}
```

S
shilei 已提交
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587

## BundleInstaller.recover

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

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

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

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

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

**参数:**

| 参数名      | 类型                                                 | 必填 | 说明                                           |
| ---------- | ---------------------------------------------------- | ---- | ---------------------------------------------- |
| bundleName | string                                               | 是   | 待恢复应用的包名。                               |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,回滚应用成功,err为undefined,否则为错误对象。 |

**错误码:**

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

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

**示例:**

```ts
import installer from '@ohos.bundle.installer';
let bundleName = 'com.ohos.demo';

try {
    installer.getBundleInstaller().then(data => {
        data.recover(bundleName, err => {
            if (err) {
                console.error('recover failed:' + err.message);
            } else {
                console.info('recover successfully.');
            }
        });
    }).catch(error => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    console.error('getBundleInstaller failed. Cause: ' + error.message);
}
```

## BundleInstaller.recover

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

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

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

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

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

**参数:**

| 参数名       | 类型                          | 必填 | 说明                                                         |
| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ |
| bundleName | string                          | 是   | 待卸载应用的包名。                                           |
588
| installParam | [InstallParam](#installparam) | 否   | 指定安装所需的其他参数,默认值:参照[InstallParam](#installparam)的默认值。                                     |
S
shilei 已提交
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742

**返回值:**

| 类型            | 说明                                   |
| --------------- | -------------------------------------- |
| 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';
let bundleName = 'com.ohos.demo';
let installParam = {
    userId: 100,
    isKeepData: false,
    installFlag: 1,
};

try {
    installer.getBundleInstaller().then(data => {
        data.recover(bundleName, installParam)
            .then((data) => {
                console.info('recover successfully: ' + JSON.stringify(data));
        }).catch((error) => {
            console.error('recover failed:' + error.message);
        });
    }).catch(error => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    console.error('getBundleInstaller failed. Cause: ' + error.message);
}
```

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

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

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

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

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

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

**参数:**

| 参数名         | 类型                                | 必填 | 说明                                                     |
| -------------- | ----------------------------------- | ---- | -------------------------------------------------------- |
| uninstallParam | [UninstallParam](#uninstallparam10) | 是   | 共享包卸载需指定的参数信息。                             |
| callback       | AsyncCallback&lt;void&gt;           | 是   | 回调函数,卸载应用成功,err为undefined,否则为错误对象。 |

**错误码:**

以下错误码的详细介绍请参见[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';
let uninstallParam = {
    bundleName : "com.ohos.demo",
};

try {
    installer.getBundleInstaller().then(data => {
        data.uninstall(uninstallParam, err => {
            if (err) {
                console.error('uninstall failed:' + err.message);
            } else {
                console.info('uninstall successfully.');
            }
        });
    }).catch(error => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    console.error('getBundleInstaller failed. Cause: ' + error.message);
}
```

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

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

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

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

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

**系统能力:** 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';
let uninstallParam = {
    bundleName : "com.ohos.demo",
};

try {
    installer.getBundleInstaller().then(data => {
        data.uninstall(uninstallParam, err => {
            if (err) {
                console.error('uninstall failed:' + err.message);
            } else {
                console.info('uninstall successfully.');
            }
        });
    }).catch(error => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    console.error('getBundleInstaller failed. Cause: ' + error.message);
}
```
W
wanghang 已提交
743 744
## HashParam

Z
zhaogan 已提交
745
应用程序安装卸载哈希参数信息。
W
wanghang 已提交
746 747 748

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

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

J
junyi233 已提交
751 752 753 754
| 名称     | 类型   | 必填 | 说明             |
| ---------- | ------ | ---------------- | ---------------- |
| moduleName | string | 是 | 应用程序模块名称。 |
| hashValue  | string | 是 | 哈希值。           |
W
wanghang 已提交
755 756 757

## InstallParam

Z
zhaogan 已提交
758
应用程序安装、卸载或恢复需指定的参数信息。
W
wanghang 已提交
759 760 761

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

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

J
junyi233 已提交
764 765
| 名称                        | 类型                           | 必填                         | 说明               |
| ------------------------------ | ------------------------------ | ------------------ | ------------------ |
766
| userId                         | number                         | 否                        | 指示用户id,默认值:调用方所在用户,取值范围:大于等于0,可使用[queryOsAccountLocalIdFromProcess](js-apis-osAccount.md#getOsAccountLocalId)获取当前进程所在用户。 |
L
Lotol 已提交
767
| installFlag                    | number                         | 否                        | 指示安装标志,枚举值:0:应用初次安装,1:应用覆盖安装,默认值为应用初次安装 |
768 769 770 771
| isKeepData                     | boolean                        | 否                       | 卸载时是否保留数据目录,默认值为false。 |
| hashParams        | Array<[HashParam](#hashparam)> | 否 | 哈希值参数,默认值为空。         |
| crowdtestDeadline| number                         | 否                        |[众测](https://developer.huawei.com/consumer/cn/agconnect/crowd-test/)截止日期,默认值为-1。 |
| sharedBundleDirPaths<sup>10+</sup> | Array\<String> | 否 |共享包文件所在路径,默认值为空。 |
772 773 774 775 776 777 778

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

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

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

J
junyi233 已提交
779
 **系统接口:** 此接口为系统接口。
780 781 782 783

| 名称        | 类型   | 必填 | 说明                                                         |
| ----------- | ------ | ---- | ------------------------------------------------------------ |
| bundleName  | string | 是   | 共享包包名。                                                 |
L
Lotol 已提交
784
| versionCode | number | 否   | 指示共享包的版本号。默认值:如果不填写versionCode,则卸载该包名的所有共享包。 |