• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bundle.installer (installer模块)
2
3> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
4> 本模块首批接口从API version 9 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
5
6在设备上安装、升级和卸载应用
7
8## 导入模块
9
10```js
11import installer from '@ohos.bundle.installer';
12```
13
14## 权限列表
15
16| 权限                           | 权限等级    | 描述             |
17| ------------------------------ | ----------- | ---------------- |
18| ohos.permission.INSTALL_BUNDLE | system_core | 可安装、卸载应用。 |
19
20权限等级参考[权限等级说明](../../security/accesstoken-overview.md#权限等级说明)
21
22## BundleInstaller.getBundleInstaller
23
24getBundleInstaller(callback: AsyncCallback\<BundleInstaller>): void;
25
26获取BundleInstaller对象,使用callback形式返回结果。
27
28**系统接口:** 此接口为系统接口,三方应用不支持调用
29
30**系统能力:** SystemCapability.BundleManager.BundleFramework.Core
31
32**参数:**
33
34| 参数名   | 类型                                                         | 必填 | 说明                                                         |
35| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
36| callback | AsyncCallback\<[BundleInstaller](js-apis-installer.md#BundleInstaller)> | 是   | 回调函数,获取BundleInstaller对象,err为undefined,data为获取到的BundleInstaller对象;否则为错误对象。 |
37
38**示例:**
39
40```ts
41import installer from '@ohos.bundle.installer';
42
43try {
44    installer.getBundleInstaller((err, data) => {
45        if (err) {
46            console.error('getBundleInstaller failed:' + err.message);
47        } else {
48            console.info('getBundleInstaller successfully');
49        }
50    });
51} catch (error) {
52    console.error('getBundleInstaller failed:' + error.message);
53}
54```
55
56## BundleInstaller.getBundleInstaller
57
58getBundleInstaller(): Promise\<BundleInstaller>;
59
60获取BundleInstaller对象,使用callback形式返回结果。
61
62**系统接口:** 此接口为系统接口,三方应用不支持调用
63
64**系统能力:** SystemCapability.BundleManager.BundleFramework.Core
65
66**返回值:**
67| 类型                                                         | 说明                                 |
68| ------------------------------------------------------------ | ------------------------------------ |
69| Promise\<[BundleInstaller](js-apis-installer.md#BundleInstaller)> | Promise对象,返回BundleInstaller对象。 |
70
71**示例:**
72
73```ts
74import installer from '@ohos.bundle.installer';
75
76try {
77    installer.getBundleInstaller().then((data) => {
78        console.info('getBundleInstaller successfully.');
79    }).catch((error) => {
80        console.error('getBundleInstaller failed. Cause: ' + error.message);
81    });
82} catch (error) {
83    console.error('getBundleInstaller failed. Cause: ' + error.message);
84}
85```
86
87## BundleInstaller.install
88install(hapFilePaths: Array&lt;string&gt;, installParam: InstallParam, callback: AsyncCallback&lt;void&gt;): void;
89
90以异步方法安装应用,使用callback形式返回结果。
91
92**系统接口:** 此接口为系统接口,三方应用不支持调用
93
94**需要权限:** ohos.permission.INSTALL_BUNDLE
95
96**系统能力:** SystemCapability.BundleManager.BundleFramework.Core
97
98**参数:**
99
100| 参数名           | 类型                                                 | 必填 | 说明                                                         |
101| --------------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ |
102| hapFilePaths | Array&lt;string&gt;                                  | 是   | 存储应用程序包的路径。路径应该是当前应用程序中存放HAP包的数据目录。当传入的路径是一个目录时, 该目录下只能放同一个应用的HAP包,且这些HAP包的签名需要保持一致。 |
103| installParam           | [InstallParam](#installparam)                        | 是   | 指定安装所需的其他参数。                                     |
104| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,安装应用成功,err为undefined,否则为错误对象。 |
105
106**错误码:**
107
108以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)。
109
110| 错误码ID | 错误信息                                                     |
111| -------- | ------------------------------------------------------------ |
112| 17700004 | The specified user ID is not found.                          |
113| 17700010 | Failed to install the HAP because the HAP fails to be parsed. |
114| 17700011 | Failed to install the HAP because the HAP signature fails to be verified. |
115| 17700012 | Failed to install the HAP because the HAP path is invalid or the HAP is too large. |
116| 17700015 | Failed to install the HAPs because they have different configuration information. |
117| 17700016 | Failed to install the HAP because of insufficient system disk space. |
118| 17700017 | Failed to install the HAP since the version of the HAP to install is too early. |
119| 17700018 | Failed to install because the dependent module does not exist. |
120
121**示例:**
122
123```ts
124import installer from '@ohos.bundle.installer';
125let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];
126let installParam = {
127    userId: 100,
128    isKeepData: false,
129    installFlag: 1,
130};
131
132try {
133    installer.getBundleInstaller().then(data => {
134        data.install(hapFilePaths, installParam, err => {
135            if (err) {
136                console.error('install failed:' + err.message);
137            } else {
138                console.info('install successfully.');
139            }
140        });
141    }).catch(error => {
142        console.error('getBundleInstaller failed. Cause: ' + error.message);
143    });
144} catch (error) {
145    console.error('getBundleInstaller failed. Cause: ' + error.message);
146}
147```
148
149## BundleInstaller.uninstall
150
151uninstall(bundleName: string, installParam: InstallParam, callback: AsyncCallback&lt;void&gt;): void;
152
153以异步方法卸载应用,使用callback形式返回结果。
154
155**系统接口:** 此接口为系统接口,三方应用不支持调用
156
157**需要权限:** ohos.permission.INSTALL_BUNDLE
158
159**系统能力:** SystemCapability.BundleManager.BundleFramework.Core
160
161**参数:**
162
163| 参数名      | 类型                                                 | 必填 | 说明                                           |
164| ---------- | ---------------------------------------------------- | ---- | ---------------------------------------------- |
165| bundleName | string                                               | 是   | 待卸载应用的包名。                                           |
166| installParam      | [InstallParam](#installparam)                        | 是   | 指定安装所需的其他参数。                       |
167| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,卸载应用成功,err为undefined,否则为错误对象。 |
168
169**错误码:**
170
171以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)。
172
173| 错误码ID | 错误信息                                                     |
174| -------- | ------------------------------------------------------------ |
175| 17700004 | The specified user ID is not found.                          |
176| 17700020 | The specified bundle is pre-installed bundle which cannot be uninstalled. |
177
178**示例:**
179
180```ts
181import installer from '@ohos.bundle.installer';
182let bundleName = 'com.ohos.demo';
183let installParam = {
184    userId: 100,
185    isKeepData: false,
186    installFlag: 1
187};
188
189try {
190    installer.getBundleInstaller().then(data => {
191        data.uninstall(bundleName, installParam, err => {
192            if (err) {
193                console.error('uninstall failed:' + err.message);
194            } else {
195                console.info('uninstall successfully.');
196            }
197        });
198    }).catch(error => {
199        console.error('getBundleInstaller failed. Cause: ' + error.message);
200    });
201} catch (error) {
202    console.error('getBundleInstaller failed. Cause: ' + error.message);
203}
204```
205
206## BundleInstaller.recover
207
208recover(bundleName: string, installParam: InstallParam, callback: AsyncCallback&lt;void&gt;): void;
209
210以异步方法回滚应用,使用callback形式返回结果。
211
212**系统接口:** 此接口为系统接口,三方应用不支持调用
213
214**需要权限:** ohos.permission.INSTALL_BUNDLE
215
216**系统能力:** SystemCapability.BundleManager.BundleFramework.Core
217
218**参数:**
219
220| 参数名      | 类型                                                 | 必填 | 说明                                           |
221| ---------- | ---------------------------------------------------- | ---- | ---------------------------------------------- |
222| bundleName | string                                               | 是   | 待恢复应用的包名。                                           |
223| installParam      | [InstallParam](#installparam)                        | 是   | 指定安装所需的其他参数。                       |
224| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,回滚应用成功,err为undefined,否则为错误对象。 |
225
226**错误码:**
227
228以下错误码的详细介绍请参见[ohos.bundle错误码](../errorcodes/errorcode-bundle.md)。
229
230| 错误码ID | 错误信息                            |
231| -------- | ----------------------------------- |
232| 17700004 | The specified user ID is not found. |
233
234**示例:**
235
236```ts
237import installer from '@ohos.bundle.installer';
238let bundleName = 'com.ohos.demo';
239let installParam = {
240    userId: 100,
241    isKeepData: false,
242    installFlag: 1
243};
244
245try {
246    installer.getBundleInstaller().then(data => {
247        data.recover(bundleName, installParam, err => {
248            if (err) {
249                console.error('recover failed:' + err.message);
250            } else {
251                console.info('recover successfully.');
252            }
253        });
254    }).catch(error => {
255        console.error('getBundleInstaller failed. Cause: ' + error.message);
256    });
257} catch (error) {
258    console.error('getBundleInstaller failed. Cause: ' + error.message);
259}
260```
261
262## HashParam
263
264应用程序安装卸载哈希参数信息。
265
266 **系统能力:** SystemCapability.BundleManager.BundleFramework.Core
267
268 **系统接口:** 此接口为系统接口,三方应用不支持调用
269
270| 名称     | 类型   | 必填 | 说明             |
271| ---------- | ------ | ---------------- | ---------------- |
272| moduleName | string | 是 | 应用程序模块名称。 |
273| hashValue  | string | 是 | 哈希值。           |
274
275## InstallParam
276
277应用程序安装、卸载或恢复需指定的参数信息。
278
279 **系统能力:** SystemCapability.BundleManager.BundleFramework.Core
280
281 **系统接口:** 此接口为系统接口,三方应用不支持调用
282
283| 名称                        | 类型                           | 必填                         | 说明               |
284| ------------------------------ | ------------------------------ | ------------------ | ------------------ |
285| userId                         | number                         | 否                        | 指示用户id,可使用[queryOsAccountLocalIdFromProcess](js-apis-osAccount.md#getOsAccountLocalId)获取当前进程所在用户。 |
286| installFlag                    | number                         | 否                        | 指示安装标志,枚举值:0:应用初次安装,1:应用覆盖安装。 |
287| isKeepData                     | boolean                        | 否                       | 卸载时是否保留数据目录。 |
288| hashParams        | Array<[HashParam](#hashparam)> | 否 | 哈希值参数。         |
289| crowdtestDeadline| number                         | 否                        |[众测](https://developer.huawei.com/consumer/cn/agconnect/crowd-test/)截止日期。 |