• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# BundleInstaller
2
3The **BundleInstaller** 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 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## BundleInstaller.install<sup>(deprecated)<sup>
10
11> This API is deprecated since API version 9. You are advised to use [@ohos.bundle.installer.install](js-apis-installer.md) instead.
12
13install(bundleFilePaths: Array&lt;string&gt;, param: InstallParam, callback: AsyncCallback&lt;InstallStatus&gt;): void;
14
15Installs a bundle. Multiple HAP files can be installed. This API uses an asynchronous callback to return the result.
16
17**Required permissions**
18
19ohos.permission.INSTALL_BUNDLE
20
21**System capability**
22
23SystemCapability.BundleManager.BundleFramework
24
25**System API**: This is a system API and cannot be called by third-party applications.
26
27**Parameters**
28
29| Name         | Type                                                        | Mandatory| Description                                                        |
30| --------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
31| bundleFilePaths | Array&lt;string&gt;                                          | Yes  | Sandbox path where the HAP files of the bundle are stored. For details about how to obtain the sandbox path, see [Obtaining the Sandbox Path](#obtaining-the-sandbox-path).|
32| param           | [InstallParam](#installparamdeprecated)                      | Yes  | Parameters required for bundle installation.                                    |
33| callback        | AsyncCallback&lt;[InstallStatus](#installstatusdeprecated)&gt; | Yes  | Callback used to return the installation status.              |
34
35**Example**
36
37```ts
38import bundle from '@ohos.bundle';
39let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];
40let installParam = {
41    userId: 100,
42    isKeepData: false,
43    installFlag: 1,
44};
45
46bundle.getBundleInstaller().then(installer => {
47    installer.install(hapFilePaths, installParam, err => {
48        if (err) {
49            console.error('install failed:' + JSON.stringify(err));
50        } else {
51            console.info('install successfully.');
52        }
53    });
54}).catch(error => {
55    console.error('getBundleInstaller failed. Cause: ' + error.message);
56});
57```
58
59## BundleInstaller.uninstall<sup>(deprecated)<sup>
60
61> This API is deprecated since API version 9. You are advised to use [uninstall](js-apis-installer.md) instead.
62
63uninstall(bundleName: string, param: InstallParam, callback: AsyncCallback&lt;InstallStatus&gt;): void;
64
65Uninstalls a bundle. This API uses an asynchronous callback to return the result.
66
67**Required permissions**
68
69ohos.permission.INSTALL_BUNDLE
70
71**System capability**
72
73SystemCapability.BundleManager.BundleFramework
74
75**System API**: This is a system API and cannot be called by third-party applications.
76
77**Parameters**
78
79| Name    | Type                                                        | Mandatory| Description                                          |
80| ---------- | ------------------------------------------------------------ | ---- | ---------------------------------------------- |
81| bundleName | string                                                       | Yes  | Bundle name.                              |
82| param      | [InstallParam](#installparamdeprecated)                      | Yes  | Parameters required for bundle uninstall.                      |
83| callback   | AsyncCallback&lt;[InstallStatus](#installstatusdeprecated)&gt; | Yes  | Callback used to return the installation status.|
84
85**Example**
86
87```ts
88import bundle from '@ohos.bundle';
89let bundleName = 'com.example.myapplication';
90let installParam = {
91    userId: 100,
92    isKeepData: false,
93    installFlag: 1,
94};
95
96bundle.getBundleInstaller().then(installer => {
97    installer.uninstall(bundleName, installParam, err => {
98        if (err) {
99            console.error('uninstall failed:' + JSON.stringify(err));
100        } else {
101            console.info('uninstall successfully.');
102        }
103    });
104}).catch(error => {
105    console.error('getBundleInstaller failed. Cause: ' + error.message);
106});
107```
108## BundleInstaller.recover<sup>(deprecated)<sup>
109
110> This API is deprecated since API version 9. You are advised to use [recover](js-apis-installer.md) instead.
111
112recover(bundleName: string, param: InstallParam, callback: AsyncCallback&lt;InstallStatus&gt;): void;
113
114Recovers a bundle. This API uses an asynchronous callback to return the result. After a pre-installed bundle is uninstalled, you can call this API to recover it.
115
116**Required permissions**
117
118ohos.permission.INSTALL_BUNDLE
119
120**System capability**
121
122SystemCapability.BundleManager.BundleFramework
123
124**System API**: This is a system API and cannot be called by third-party applications.
125
126**Parameters**
127
128| Name    | Type                                                        | Mandatory| Description                                              |
129| ---------- | ------------------------------------------------------------ | ---- | -------------------------------------------------- |
130| bundleName | string                                                       | Yes  | Bundle name.                                  |
131| param      | [InstallParam](#installparamdeprecated)                      | Yes  | Parameters required for bundle recovery.                      |
132| callback   | AsyncCallback&lt;[InstallStatus](#installstatusdeprecated)&gt; | Yes  | Callback used to return the recovery status.|
133
134**Example**
135
136```ts
137import bundle from '@ohos.bundle';
138
139let bundleName = 'com.example.myapplication';
140let installParam = {
141    userId: 100,
142    isKeepData: false,
143    installFlag: 1,
144};
145
146bundle.getBundleInstaller().then(installer => {
147    installer.recover(bundleName, installParam, err => {
148        if (err) {
149            console.error('recover failed:' + JSON.stringify(err));
150        } else {
151            console.info('recover successfully.');
152        }
153    });
154}).catch(error => {
155    console.error('getBundleInstaller failed. Cause: ' + error.message);
156});
157```
158
159## InstallParam<sup>(deprecated)<sup>
160
161Describes the parameters required for bundle installation, recovery, or uninstall.
162
163**System capability**: SystemCapability.BundleManager.BundleFramework
164
165**System API**: This is a system API and cannot be called by third-party applications.
166
167| Name       | Type   | Readable| Writable| Description              |
168| ----------- | ------- | ---- | ---- | ------------------ |
169| userId      | number  | Yes  | Yes  | User ID. The default value is the user ID of the caller.|
170| installFlag | number  | Yes  | Yes  | Installation flag.<br>**1** (default): overwrite installation.<br>**16**: installation-free.|
171| isKeepData  | boolean | Yes  | Yes  | Whether data is kept. The default value is **false**.|
172
173## InstallStatus<sup>(deprecated)<sup>
174
175Describes the bundle installation or uninstall status.
176
177**System capability**: SystemCapability.BundleManager.BundleFramework
178
179**System API**: This is a system API and cannot be called by third-party applications.
180
181| Name         | Type                                                        | Readable| Writable| Description                          |
182| ------------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------ |
183| status        | bundle.[InstallErrorCode](js-apis-Bundle.md#installerrorcode) | Yes  | No  | Installation or uninstall error code. The value must be defined in [InstallErrorCode](js-apis-Bundle.md#installerrorcode)|
184| statusMessage | string                                                       | Yes  | No  | Installation or uninstall status message.  <br>**SUCCESS**: install_succeed<br>**STATUS_INSTALL_FAILURE**: Installation failed (no installation file exists).<br>**STATUS_INSTALL_FAILURE_ABORTED**: Installation aborted.<br>**STATUS_INSTALL_FAILURE_INVALID**: Invalid installation parameter.<br>**STATUS_INSTALL_FAILURE_CONFLICT**: Installation conflict. (The basic information of the application to update is inconsistent with that of the existing application.)<br>**STATUS_INSTALL_FAILURE_STORAGE**: Failed to store the bundle information.<br>**STATUS_INSTALL_FAILURE_INCOMPATIBLE**: Installation incompatibility. (A downgrade occurs or the signature information is incorrect.)<br>**STATUS_UNINSTALL_FAILURE**: Uninstallation failed. (The application to be uninstalled is not found.)<br>**STATUS_UNINSTALL_FAILURE_ABORTED**: Uninstallation aborted. (This error code is not in use.)<br>**STATUS_UNINSTALL_FAILURE_ABORTED**: Uninstallation conflict. (Failed to uninstall a system application or end the application process.)<br>**STATUS_INSTALL_FAILURE_DOWNLOAD_TIMEOUT**: Installation failed. (Download timed out.)<br>**STATUS_INSTALL_FAILURE_DOWNLOAD_FAILED**: Installation failed. (Download failed.)<br>**STATUS_RECOVER_FAILURE_INVALID**: Failed to restore the pre-installed application.<br>**STATUS_ABILITY_NOT_FOUND**: Ability not found.<br>**STATUS_BMS_SERVICE_ERROR**: BMS service error.<br>**STATUS_FAILED_NO_SPACE_LEFT**: Insufficient device space.<br>**STATUS_GRANT_REQUEST_PERMISSIONS_FAILED**: Application authorization failed.<br>**STATUS_INSTALL_PERMISSION_DENIED**: No installation permission.<br>**STATUS_UNINSTALL_PERMISSION_DENIED**: No uninstallation permission. |
185
186## Obtaining the Sandbox Path
187For the FA model, the sandbox path of a bundle can be obtained using the APIs in [Context](js-apis-inner-app-context.md). For the stage model, the sandbox path can be obtained using the attribute in [Context](js-apis-inner-application-uiAbilityContext.md#abilitycontext). The following describes how to obtain the sandbox path.
188
189**Example**
190``` ts
191// Stage model
192import UIAbility from '@ohos.app.ability.UIAbility';
193export default class EntryAbility extends UIAbility {
194    onWindowStageCreate(windowStage) {
195        let context = this.context;
196        let pathDir = context.filesDir;
197        console.info('sandbox path is ' + pathDir);
198    }
199}
200
201// FA model
202import featureAbility from '@ohos.ability.featureAbility';
203let context = featureAbility.getContext();
204context.getFilesDir().then((data) => {
205    let pathDir = data;
206    console.info('sandbox path is ' + pathDir);
207});
208```
209