js-apis-installer.md 33.1 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 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 已提交
126
| 17700044 | Failed to install the HAP because the isolationMode configured is not supported. |
H
Handsome_Afraican_Man 已提交
127
| 17700047 | Failed to install the HAP because the VersionCode to be updated is not greater than the current VersionCode. |
W
wanghang 已提交
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 155

**示例:**

```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 已提交
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 190
## 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 已提交
191 192
| 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 已提交
193
| 17700044 | Failed to install the HAP because the isolationMode configured is not supported. |
H
Handsome_Afraican_Man 已提交
194
| 17700047 | Failed to install the HAP because the VersionCode to be updated is not greater than the current VersionCode. |
S
shilei 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

**示例:**

```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 已提交
218

J
junyi233 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
## 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的签名需要保持一致。 |
236
| installParam | [InstallParam](#installparam) | 否   | 指定安装所需的其他参数,默认值:参照[InstallParam](#installparam)的默认值。                                     |
J
junyi233 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261

**返回值:**

| 类型            | 说明                                   |
| --------------- | -------------------------------------- |
| 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 已提交
262 263
| 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 已提交
264
| 17700044 | Failed to install the HAP because the isolationMode configured is not supported. |
H
Handsome_Afraican_Man 已提交
265
| 17700047 | Failed to install the HAP because the VersionCode to be updated is not greater than the current VersionCode. |
J
junyi233 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

**示例:**

```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 已提交
282
                console.info('install successfully: ' + JSON.stringify(data));
J
junyi233 已提交
283
        }).catch((error) => {
S
shilei 已提交
284
            console.error('install failed:' + error.message);
J
junyi233 已提交
285 286 287 288 289 290 291 292 293
        });
    }).catch(error => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    console.error('getBundleInstaller failed. Cause: ' + error.message);
}
```

W
wanghang 已提交
294 295 296 297 298 299
## BundleInstaller.uninstall

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

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

J
junyi233 已提交
300
**系统接口:** 此接口为系统接口。
W
wanghang 已提交
301 302 303 304 305 306 307

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

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

**参数:**

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

**错误码:**

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

J
junyi233 已提交
318 319
| 错误码ID | 错误信息                                                     |
| -------- | ------------------------------------------------------------ |
S
shilei 已提交
320 321
| 17700001 | The specified bundle name is not found. |
| 17700004 | The specified user ID is not found. |
Z
zhaogan 已提交
322
| 17700020 | The specified bundle is pre-installed bundle which cannot be uninstalled. |
323
| 17700040 | The specified bundle is a shared bundle which cannot be uninstalled. |
324
| 17700045 | Failed to uninstall because enterprise device management disallow uninstall. |
W
wanghang 已提交
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 350 351 352 353

**示例:**

```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 已提交
354
## BundleInstaller.uninstall
355

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

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

J
junyi233 已提交
360
**系统接口:** 此接口为系统接口。
361 362 363 364 365 366 367

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

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

**参数:**

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

**错误码:**

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

| 错误码ID | 错误信息                                                     |
| -------- | ------------------------------------------------------------ |
S
shilei 已提交
379
| 17700001 | The specified bundle name is not found. |
380
| 17700020 | The specified bundle is pre-installed bundle which cannot be uninstalled. |
S
shilei 已提交
381
| 17700040 | The specified bundle is a shared bundle which cannot be uninstalled. |
382
| 17700045 | Failed to uninstall because enterprise device management disallow uninstall. |
383 384 385 386 387

**示例:**

```ts
import installer from '@ohos.bundle.installer';
S
shilei 已提交
388
let bundleName = 'com.ohos.demo';
389 390 391

try {
    installer.getBundleInstaller().then(data => {
S
shilei 已提交
392
        data.uninstall(bundleName, err => {
393 394 395 396 397 398 399 400 401 402 403 404 405
            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 已提交
406
## BundleInstaller.uninstall
407

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

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

J
junyi233 已提交
412
**系统接口:** 此接口为系统接口。
413 414 415 416 417 418 419

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

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

**参数:**

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

**返回值:**

S
shilei 已提交
427 428
| 类型            | 说明                                   |
| --------------- | -------------------------------------- |
429
| Promise\<void\> | Promise对象。无返回结果的Promise对象。 |
430 431 432 433 434 435 436

**错误码:**

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

| 错误码ID | 错误信息                                                     |
| -------- | ------------------------------------------------------------ |
S
shilei 已提交
437
| 17700001 | The specified bundle name is not found. |
J
jiangminsen 已提交
438
| 17700004 | The specified user ID is not found. |
439
| 17700020 | The specified bundle is pre-installed bundle which cannot be uninstalled. |
S
shilei 已提交
440
| 17700040 | The specified bundle is a shared bundle which cannot be uninstalled. |
441
| 17700045 | Failed to uninstall because enterprise device management disallow uninstall. |
442 443 444 445

**示例:**
```ts
import installer from '@ohos.bundle.installer';
S
shilei 已提交
446 447 448 449 450
let bundleName = 'com.ohos.demo';
let installParam = {
    userId: 100,
    isKeepData: false,
    installFlag: 1,
451 452 453 454
};

try {
    installer.getBundleInstaller().then(data => {
S
shilei 已提交
455 456 457 458 459
        data.uninstall(bundleName, installParam)
            .then((data) => {
                console.info('uninstall successfully: ' + JSON.stringify(data));
        }).catch((error) => {
            console.error('uninstall failed:' + error.message);
460 461 462 463 464 465 466 467 468
        });
    }).catch(error => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    console.error('getBundleInstaller failed. Cause: ' + error.message);
}
```

W
wanghang 已提交
469 470 471 472
## BundleInstaller.recover

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

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

J
junyi233 已提交
475
**系统接口:** 此接口为系统接口。
W
wanghang 已提交
476 477 478 479 480 481 482

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

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

**参数:**

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

**错误码:**

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

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

**示例:**

```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 已提交
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 588 589 590 591 592 593

## 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                          | 是   | 待卸载应用的包名。                                           |
594
| installParam | [InstallParam](#installparam) | 否   | 指定安装所需的其他参数,默认值:参照[InstallParam](#installparam)的默认值。                                     |
S
shilei 已提交
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 743 744 745 746 747 748

**返回值:**

| 类型            | 说明                                   |
| --------------- | -------------------------------------- |
| 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 已提交
749 750
## HashParam

Z
zhaogan 已提交
751
应用程序安装卸载哈希参数信息。
W
wanghang 已提交
752 753 754

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

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

J
junyi233 已提交
757 758 759 760
| 名称     | 类型   | 必填 | 说明             |
| ---------- | ------ | ---------------- | ---------------- |
| moduleName | string | 是 | 应用程序模块名称。 |
| hashValue  | string | 是 | 哈希值。           |
W
wanghang 已提交
761 762 763

## InstallParam

Z
zhaogan 已提交
764
应用程序安装、卸载或恢复需指定的参数信息。
W
wanghang 已提交
765 766 767

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

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

J
junyi233 已提交
770 771
| 名称                        | 类型                           | 必填                         | 说明               |
| ------------------------------ | ------------------------------ | ------------------ | ------------------ |
772
| userId                         | number                         | 否                        | 指示用户id,默认值:调用方所在用户,取值范围:大于等于0,可使用[queryOsAccountLocalIdFromProcess](js-apis-osAccount.md#getOsAccountLocalId)获取当前进程所在用户。 |
L
Lotol 已提交
773
| installFlag                    | number                         | 否                        | 指示安装标志,枚举值:0:应用初次安装,1:应用覆盖安装,默认值为应用初次安装 |
774 775 776 777
| 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> | 否 |共享包文件所在路径,默认值为空。 |
778 779 780 781 782 783 784

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

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

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

J
junyi233 已提交
785
 **系统接口:** 此接口为系统接口。
786 787 788 789

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