• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bundle.installer (installer)
2
3The **bundle.installer** module provides APIs for you to install, uninstall, and recover bundles on devices.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```js
12import installer from '@ohos.bundle.installer';
13```
14
15## Required Permissions
16
17| Permission                          | Permission Level   | Description            |
18| ------------------------------ | ----------- | ---------------- |
19| ohos.permission.INSTALL_BUNDLE | system_core | Permission to install or uninstall bundles.|
20
21For details, see [Permission Levels](../../security/accesstoken-overview.md#permission-levels).
22
23## BundleInstaller.getBundleInstaller
24
25getBundleInstaller(callback: AsyncCallback\<BundleInstaller>): void;
26
27Obtains a **BundleInstaller** object. This API uses an asynchronous callback to return the result.
28
29**System API**: This is a system API and cannot be called by third-party applications.
30
31**System capability**: SystemCapability.BundleManager.BundleFramework.Core
32
33**Parameters**
34
35| Name  | Type                                                        | Mandatory| Description                                                        |
36| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
37| callback | AsyncCallback\<[BundleInstaller](js-apis-installer.md#BundleInstaller)> | Yes  | Callback used to return the result. If the operation is successful, **err** is undefined and **data** is the **BundleInstaller** object obtained; otherwise, **err** is an error object.|
38
39**Example**
40
41```ts
42import installer from '@ohos.bundle.installer';
43
44try {
45    installer.getBundleInstaller((err, data) => {
46        if (err) {
47            console.error('getBundleInstaller failed:' + err.message);
48        } else {
49            console.info('getBundleInstaller successfully');
50        }
51    });
52} catch (error) {
53    console.error('getBundleInstaller failed:' + error.message);
54}
55```
56
57## BundleInstaller.getBundleInstaller
58
59getBundleInstaller(): Promise\<BundleInstaller>;
60
61Obtains a **BundleInstaller** object. This API uses an asynchronous callback to return the result.
62
63**System API**: This is a system API and cannot be called by third-party applications.
64
65**System capability**: SystemCapability.BundleManager.BundleFramework.Core
66
67**Return value**
68| Type                                                        | Description                                |
69| ------------------------------------------------------------ | ------------------------------------ |
70| Promise\<[BundleInstaller](js-apis-installer.md#BundleInstaller)> | Promise used to return the **BundleInstaller** object obtained.|
71
72**Example**
73
74```ts
75import installer from '@ohos.bundle.installer';
76
77try {
78    installer.getBundleInstaller().then((data) => {
79        console.info('getBundleInstaller successfully.');
80    }).catch((error) => {
81        console.error('getBundleInstaller failed. Cause: ' + error.message);
82    });
83} catch (error) {
84    console.error('getBundleInstaller failed. Cause: ' + error.message);
85}
86```
87
88## BundleInstaller.install
89install(hapFilePaths: Array&lt;string&gt;, installParam: InstallParam, callback: AsyncCallback&lt;void&gt;): void;
90
91Installs a bundle. This API uses an asynchronous callback to return the result.
92
93**System API**: This is a system API and cannot be called by third-party applications.
94
95**Required permissions**: ohos.permission.INSTALL_BUNDLE
96
97**System capability**: SystemCapability.BundleManager.BundleFramework.Core
98
99**Parameters**
100
101| Name          | Type                                                | Mandatory| Description                                                        |
102| --------------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ |
103| hapFilePaths | Array&lt;string&gt;                                  | Yes  | Paths where the HAP files of the bundle are stored, which are the data directories. If only one directory is passed, the HAP files in the directory must belong to the same bundle and have the same signature.|
104| installParam           | [InstallParam](#installparam)                        | Yes  | Parameters required for the installation.                                    |
105| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the operation is successful, **err** is undefined; otherwise, **err** is an error object.|
106
107**Error codes**
108
109For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md).
110
111| ID| Error Message                                                    |
112| -------- | ------------------------------------------------------------ |
113| 17700004 | The specified user ID is not found.                          |
114| 17700010 | Failed to install the HAP because the HAP fails to be parsed. |
115| 17700011 | Failed to install the HAP because the HAP signature fails to be verified. |
116| 17700012 | Failed to install the HAP because the HAP path is invalid or the HAP is too large. |
117| 17700015 | Failed to install the HAPs because they have different configuration information. |
118| 17700016 | Failed to install the HAP because of insufficient system disk space. |
119| 17700017 | Failed to install the HAP since the version of the HAP to install is too early. |
120| 17700018 | Failed to install because the dependent module does not exist. |
121
122**Example**
123
124```ts
125import installer from '@ohos.bundle.installer';
126let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];
127let installParam = {
128    userId: 100,
129    isKeepData: false,
130    installFlag: 1,
131};
132
133try {
134    installer.getBundleInstaller().then(data => {
135        data.install(hapFilePaths, installParam, err => {
136            if (err) {
137                console.error('install failed:' + err.message);
138            } else {
139                console.info('install successfully.');
140            }
141        });
142    }).catch(error => {
143        console.error('getBundleInstaller failed. Cause: ' + error.message);
144    });
145} catch (error) {
146    console.error('getBundleInstaller failed. Cause: ' + error.message);
147}
148```
149
150## BundleInstaller.install
151
152install(hapFilePaths: Array\<string\>, installParam?: InstallParam) : Promise\<void\>;
153
154Installs a bundle. This API uses a promise to return the result.
155
156**System API**: This is a system API.
157
158**Required permissions**: ohos.permission.INSTALL_BUNDLE
159
160**System capability**: SystemCapability.BundleManager.BundleFramework.Core
161
162**Parameters**
163
164| Name      | Type                         | Mandatory| Description                                                        |
165| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ |
166| hapFilePaths | Array\<string\>               | Yes  | Paths where the HAP files of the bundle are stored, which are the data directories. If only one directory is passed, the HAP files in the directory must belong to the same bundle and have the same signature.|
167| installParam | [InstallParam](#installparam) | No  | Parameters required for the installation.                                    |
168
169**Return value**
170
171| Type           | Description                                  |
172| --------------- | -------------------------------------- |
173| Promise\<void\> | Promise that returns no value.|
174
175**Error codes**
176
177For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md).
178
179| ID| Error Message                                                    |
180| -------- | ------------------------------------------------------------ |
181| 17700004 | The specified user ID is not found.                          |
182| 17700010 | Failed to install the HAP because the HAP fails to be parsed. |
183| 17700011 | Failed to install the HAP because the HAP signature fails to be verified. |
184| 17700012 | Failed to install the HAP because the HAP path is invalid or the HAP is too large. |
185| 17700015 | Failed to install the HAPs because they have different configuration information. |
186| 17700016 | Failed to install the HAP because of insufficient system disk space. |
187| 17700017 | Failed to install the HAP since the version of the HAP to install is too early. |
188| 17700018 | Failed to install because the dependent module does not exist. |
189
190**Example**
191
192```ts
193import installer from '@ohos.bundle.installer';
194let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];
195let installParam = {
196    userId: 100,
197    isKeepData: false,
198    installFlag: 1,
199};
200
201try {
202    installer.getBundleInstaller().then(data => {
203        data.install(hapFilePaths, installParam)
204            .then((data) => {
205                console.info('install successfully: ' + JSON.stringify(data));
206        }).catch((error) => {
207            console.error('install failed:' + error.message);
208        });
209    }).catch(error => {
210        console.error('getBundleInstaller failed. Cause: ' + error.message);
211    });
212} catch (error) {
213    console.error('getBundleInstaller failed. Cause: ' + error.message);
214}
215```
216
217## BundleInstaller.uninstall
218
219uninstall(bundleName: string, installParam: InstallParam, callback: AsyncCallback&lt;void&gt;): void;
220
221Uninstalls a bundle. This API uses an asynchronous callback to return the result.
222
223**System API**: This is a system API and cannot be called by third-party applications.
224
225**Required permissions**: ohos.permission.INSTALL_BUNDLE
226
227**System capability**: SystemCapability.BundleManager.BundleFramework.Core
228
229**Parameters**
230
231| Name     | Type                                                | Mandatory| Description                                          |
232| ---------- | ---------------------------------------------------- | ---- | ---------------------------------------------- |
233| bundleName | string                                               | Yes  | Name of the target bundle.                                          |
234| installParam      | [InstallParam](#installparam)                        | Yes  | Parameters required for the uninstall.                      |
235| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the operation is successful, **err** is undefined; otherwise, **err** is an error object.|
236
237**Error codes**
238
239For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md).
240
241| ID| Error Message                                                    |
242| -------- | ------------------------------------------------------------ |
243| 17700001 | The specified bundle name is not found. |
244| 17700004 | The specified user ID is not found.                          |
245| 17700020 | The specified bundle is pre-installed bundle which cannot be uninstalled. |
246
247**Example**
248
249```ts
250import installer from '@ohos.bundle.installer';
251let bundleName = 'com.ohos.demo';
252let installParam = {
253    userId: 100,
254    isKeepData: false,
255    installFlag: 1
256};
257
258try {
259    installer.getBundleInstaller().then(data => {
260        data.uninstall(bundleName, installParam, err => {
261            if (err) {
262                console.error('uninstall failed:' + err.message);
263            } else {
264                console.info('uninstall successfully.');
265            }
266        });
267    }).catch(error => {
268        console.error('getBundleInstaller failed. Cause: ' + error.message);
269    });
270} catch (error) {
271    console.error('getBundleInstaller failed. Cause: ' + error.message);
272}
273```
274## BundleInstaller.uninstall
275
276uninstall(bundleName: string, installParam?: InstallParam) : Promise\<void\>;
277
278Uninstalls a bundle. This API uses a promise to return the result.
279
280**System API**: This is a system API.
281
282**Required permissions**: ohos.permission.INSTALL_BUNDLE
283
284**System capability**: SystemCapability.BundleManager.BundleFramework.Core
285
286**Parameters**
287
288| Name      | Type                         | Mandatory| Description                                                        |
289| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ |
290| bundleName | string                          | Yes  | Name of the target bundle.                                          |
291| installParam | [InstallParam](#installparam) | No  | Parameters required for the uninstall.                                    |
292
293**Return value**
294
295| Type           | Description                                  |
296| --------------- | -------------------------------------- |
297| Promise\<void\> | Promise that returns no value.|
298
299**Error codes**
300
301For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md).
302
303| ID| Error Message                                                    |
304| -------- | ------------------------------------------------------------ |
305| 17700001 | The specified bundle name is not found. |
306| 17700004 | The specified user ID is not found.                          |
307| 17700020 | The specified bundle is pre-installed bundle which cannot be uninstalled. |
308
309**Example**
310```ts
311import installer from '@ohos.bundle.installer';
312let bundleName = 'com.ohos.demo';
313let installParam = {
314    userId: 100,
315    isKeepData: false,
316    installFlag: 1,
317};
318
319try {
320    installer.getBundleInstaller().then(data => {
321        data.uninstall(bundleName, installParam)
322            .then((data) => {
323                console.info('uninstall successfully: ' + JSON.stringify(data));
324        }).catch((error) => {
325            console.error('uninstall failed:' + error.message);
326        });
327    }).catch(error => {
328        console.error('getBundleInstaller failed. Cause: ' + error.message);
329    });
330} catch (error) {
331    console.error('getBundleInstaller failed. Cause: ' + error.message);
332}
333```
334
335## BundleInstaller.recover
336
337recover(bundleName: string, installParam: InstallParam, callback: AsyncCallback&lt;void&gt;): void;
338
339Rolls back a bundle to the initial installation state. This API uses an asynchronous callback to return the result.
340
341**System API**: This is a system API and cannot be called by third-party applications.
342
343**Required permissions**: ohos.permission.INSTALL_BUNDLE
344
345**System capability**: SystemCapability.BundleManager.BundleFramework.Core
346
347**Parameters**
348
349| Name     | Type                                                | Mandatory| Description                                          |
350| ---------- | ---------------------------------------------------- | ---- | ---------------------------------------------- |
351| bundleName | string                                               | Yes  | Name of the target bundle.                                          |
352| installParam      | [InstallParam](#installparam)                        | Yes  | Parameters required for the recovery.                      |
353| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the operation is successful, **err** is undefined; otherwise, **err** is an error object.|
354
355**Error codes**
356
357For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md).
358
359| ID| Error Message                           |
360| -------- | ----------------------------------- |
361| 17700001 | The specified bundle name is not found. |
362| 17700004 | The specified user ID is not found. |
363
364**Example**
365
366```ts
367import installer from '@ohos.bundle.installer';
368let bundleName = 'com.ohos.demo';
369let installParam = {
370    userId: 100,
371    isKeepData: false,
372    installFlag: 1
373};
374
375try {
376    installer.getBundleInstaller().then(data => {
377        data.recover(bundleName, installParam, err => {
378            if (err) {
379                console.error('recover failed:' + err.message);
380            } else {
381                console.info('recover successfully.');
382            }
383        });
384    }).catch(error => {
385        console.error('getBundleInstaller failed. Cause: ' + error.message);
386    });
387} catch (error) {
388    console.error('getBundleInstaller failed. Cause: ' + error.message);
389}
390```
391## BundleInstaller.recover
392
393recover(bundleName: string, installParam?: InstallParam) : Promise\<void\>;
394
395Rolls back a bundle to the initial installation state. This API uses a promise to return the result.
396
397**System API**: This is a system API.
398
399**Required permissions**: ohos.permission.INSTALL_BUNDLE
400
401**System capability**: SystemCapability.BundleManager.BundleFramework.Core
402
403**Parameters**
404
405| Name      | Type                         | Mandatory| Description                                                        |
406| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ |
407| bundleName | string                          | Yes  | Name of the target bundle.                                          |
408| installParam | [InstallParam](#installparam) | No  | Parameters required for the recovery.                                    |
409
410**Return value**
411
412| Type           | Description                                  |
413| --------------- | -------------------------------------- |
414| Promise\<void\> | Promise that returns no value.|
415
416**Error codes**
417
418For details about the error codes, see [Bundle Error Codes](../errorcodes/errorcode-bundle.md).
419
420| ID| Error Message                           |
421| -------- | ----------------------------------- |
422| 17700001 | The specified bundle name is not found. |
423| 17700004 | The specified user ID is not found. |
424
425**Example**
426```ts
427import installer from '@ohos.bundle.installer';
428let bundleName = 'com.ohos.demo';
429let installParam = {
430    userId: 100,
431    isKeepData: false,
432    installFlag: 1,
433};
434
435try {
436    installer.getBundleInstaller().then(data => {
437        data.recover(bundleName, installParam)
438            .then((data) => {
439                console.info('recover successfully: ' + JSON.stringify(data));
440        }).catch((error) => {
441            console.error('recover failed:' + error.message);
442        });
443    }).catch(error => {
444        console.error('getBundleInstaller failed. Cause: ' + error.message);
445    });
446} catch (error) {
447    console.error('getBundleInstaller failed. Cause: ' + error.message);
448}
449```
450## HashParam
451
452Defines the hash parameters for bundle installation and uninstall.
453
454 **System capability**: SystemCapability.BundleManager.BundleFramework.Core
455
456 **System API**: This is a system API and cannot be called by third-party applications.
457
458| Name    | Type  | Mandatory| Description            |
459| ---------- | ------ | ---------------- | ---------------- |
460| moduleName | string | Yes| Module name of the bundle.|
461| hashValue  | string | Yes| Hash value.          |
462
463## InstallParam
464
465Defines the parameters that need to be specified for bundle installation, uninstall, or recovering.
466
467 **System capability**: SystemCapability.BundleManager.BundleFramework.Core
468
469 **System API**: This is a system API and cannot be called by third-party applications.
470
471| Name                       | Type                          | Mandatory                        | Description              |
472| ------------------------------ | ------------------------------ | ------------------ | ------------------ |
473| userId                         | number                         | No                       | User ID. You can use [queryOsAccountLocalIdFromProcess](js-apis-osAccount.md#getOsAccountLocalId) to obtain the user of the current process.|
474| installFlag                    | number                         | No                       | Installation flag. The value **0** means initial installation and **1** means overwrite installation.|
475| isKeepData                     | boolean                        | No                      | Whether to retain the data directory during bundle uninstall.|
476| hashParams        | Array<[HashParam](#hashparam)> | No| Hash parameters.        |
477| crowdtestDeadline| number                         | No                       |End date of crowdtesting.|
478