1# BundleInstaller (System API) 2<!--Kit: Ability Kit--> 3<!--Subsystem: BundleManager--> 4<!--Owner: @wanghang904--> 5<!--Designer: @hanfeng6--> 6<!--Tester: @kongjing2--> 7<!--Adviser: @Brilliantry_Rui--> 8 9The module provides APIs for you to install, uninstall, and recover bundles on devices. 10 11> **NOTE** 12> 13> 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. 14> 15> The APIs of this module are deprecated since API version 9. You are advised to use [@ohos.bundle.installer.install](js-apis-installer-sys.md) instead. 16> 17> The APIs provided by this module are system APIs. 18 19## BundleInstaller.install<sup>(deprecated)<sup> 20 21This API is deprecated since API version 9. You are advised to use [@ohos.bundle.installer.install](js-apis-installer-sys.md#bundleinstallerinstall) instead. 22 23install(bundleFilePaths: Array<string>, param: InstallParam, callback: AsyncCallback<InstallStatus>): void; 24 25Installs a bundle. Multiple HAP files can be installed. This API uses an asynchronous callback to return the result. 26 27**Required permissions** 28 29ohos.permission.INSTALL_BUNDLE 30 31**System capability** 32 33SystemCapability.BundleManager.BundleFramework 34 35**System API**: This is a system API. 36 37**Parameters** 38 39| Name | Type | Mandatory| Description | 40| --------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 41| bundleFilePaths | Array<string> | 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).| 42| param | [InstallParam](#installparamdeprecated) | Yes | Parameters required for bundle installation. | 43| callback | AsyncCallback<[InstallStatus](#installstatusdeprecated)> | Yes | Callback used to return the installation status. | 44 45**Example** 46 47```ts 48import bundleInstall from '@ohos.bundle.installer'; 49import { BusinessError } from '@ohos.base'; 50 51let hapFilePaths: Array<string> = ['/data/storage/el2/base/haps/entry/files/']; 52let installParam: bundleInstall.InstallParam = { 53 userId: 100, 54 isKeepData: false, 55 installFlag: 1, 56}; 57 58bundleInstall.getBundleInstaller().then(installer => { 59 installer.install(hapFilePaths, installParam, err => { 60 if (err) { 61 console.error('install failed:' + JSON.stringify(err)); 62 } else { 63 console.info('install successfully.'); 64 } 65 }); 66}).catch((error: BusinessError)=> { 67 let message = (error as BusinessError).message; 68 console.error('getBundleInstaller failed. Cause: ' + message); 69}); 70``` 71 72## BundleInstaller.uninstall<sup>(deprecated)<sup> 73 74This API is deprecated since API version 9. You are advised to use [uninstall](js-apis-installer-sys.md#bundleinstalleruninstall) instead. 75 76uninstall(bundleName: string, param: InstallParam, callback: AsyncCallback<InstallStatus>): void; 77 78Uninstalls a bundle. This API uses an asynchronous callback to return the result. 79 80**Required permissions** 81 82ohos.permission.INSTALL_BUNDLE 83 84**System capability** 85 86SystemCapability.BundleManager.BundleFramework 87 88**System API**: This is a system API. 89 90**Parameters** 91 92| Name | Type | Mandatory| Description | 93| ---------- | ------------------------------------------------------------ | ---- | ---------------------------------------------- | 94| bundleName | string | Yes | Bundle name. | 95| param | [InstallParam](#installparamdeprecated) | Yes | Parameters required for bundle uninstall. | 96| callback | AsyncCallback<[InstallStatus](#installstatusdeprecated)> | Yes | Callback used to return the installation status.| 97 98**Example** 99 100```ts 101import bundleInstall from '@ohos.bundle.installer'; 102import { BusinessError } from '@ohos.base'; 103 104let bundleName: string = 'com.example.myapplication'; 105let installParam: bundleInstall.InstallParam = { 106 userId: 100, 107 isKeepData: false, 108 installFlag: 1, 109}; 110 111bundleInstall.getBundleInstaller().then(installer => { 112 installer.uninstall(bundleName, installParam, err => { 113 if (err) { 114 console.error('uninstall failed:' + JSON.stringify(err)); 115 } else { 116 console.info('uninstall successfully.'); 117 } 118 }); 119}).catch((error: BusinessError) => { 120 let message = (error as BusinessError).message; 121 console.error('getBundleInstaller failed. Cause: ' + message); 122}); 123``` 124## BundleInstaller.recover<sup>(deprecated)<sup> 125 126This API is deprecated since API version 9. You are advised to use [recover](js-apis-installer-sys.md#bundleinstallerrecover) instead. 127 128recover(bundleName: string, param: InstallParam, callback: AsyncCallback<InstallStatus>): void; 129 130Recovers 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. 131 132**Required permissions** 133 134ohos.permission.INSTALL_BUNDLE 135 136**System capability** 137 138SystemCapability.BundleManager.BundleFramework 139 140**System API**: This is a system API. 141 142**Parameters** 143 144| Name | Type | Mandatory| Description | 145| ---------- | ------------------------------------------------------------ | ---- | -------------------------------------------------- | 146| bundleName | string | Yes | Bundle name. | 147| param | [InstallParam](#installparamdeprecated) | Yes | Parameters required for bundle recovery. | 148| callback | AsyncCallback<[InstallStatus](#installstatusdeprecated)> | Yes | Callback used to return the recovery status.| 149 150**Example** 151 152```ts 153import bundleInstall from '@ohos.bundle.installer'; 154import { BusinessError } from '@ohos.base'; 155 156let bundleName: string = 'com.example.myapplication'; 157let installParam: bundleInstall.InstallParam = { 158 userId: 100, 159 isKeepData: false, 160 installFlag: 1, 161}; 162 163bundleInstall.getBundleInstaller().then(installer => { 164 installer.uninstall(bundleName, installParam, err => { 165 if (err) { 166 console.error('uninstall failed:' + JSON.stringify(err)); 167 } else { 168 console.info('uninstall successfully.'); 169 } 170 }); 171}).catch((error: BusinessError) => { 172 let message = (error as BusinessError).message; 173 console.error('getBundleInstaller failed. Cause: ' + message); 174}); 175``` 176 177## InstallParam<sup>(deprecated)<sup> 178 179This API is deprecated since API version 9. You are advised to use [InstallParam](js-apis-installer-sys.md#installparam) instead. 180 181Describes the parameters required for bundle installation, recovery, or uninstall. 182 183**System capability**: SystemCapability.BundleManager.BundleFramework 184 185**System API**: This is a system API. 186 187| Name | Type | Read-Only| Optional| Description | 188| ----------- | ------- | ---- | ---- | ------------------ | 189| userId | number | No | No | User ID. The default value is the user ID of the caller.| 190| installFlag | number | No | No | Installation flag.<br>The value can be:<br>**1** (default): overwrite installation.<br>**16**: installation-free.| 191| isKeepData | boolean | No | No | Whether to retain the bundle data when the application is uninstalled. The default value is **false**. **true** to retain, **false** otherwise.| 192 193## InstallStatus<sup>(deprecated)<sup> 194 195Describes the bundle installation or uninstall status. 196 197**System capability**: SystemCapability.BundleManager.BundleFramework 198 199**System API**: This is a system API. 200 201| Name | Type | Read-Only| Optional| Description | 202| ------------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ | 203| status | bundle.[InstallErrorCode](js-apis-Bundle.md#installerrorcodedeprecated) | No | No | Installation or uninstall error code. The value must be defined in [InstallErrorCode](js-apis-Bundle.md#installerrorcodedeprecated).| 204| statusMessage | string | No | No | Installation or uninstall status message. <br>**SUCCESS**: Installation succeeded.<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**: Uninstall failed. (The application to be uninstalled is not found.)<br>**STATUS_UNINSTALL_FAILURE_ABORTED**: Uninstall aborted. (This error code is not in use.)<br>**STATUS_UNINSTALL_FAILURE_ABORTED**: Uninstall 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 uninstall permission.| 205 206## Obtaining the Sandbox Path 207For 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 property in [Context](js-apis-inner-application-uiAbilityContext-sys.md#uiabilitycontext). The following describes how to obtain the sandbox path. 208 209**Example** 210``` ts 211// Stage model 212import UIAbility from '@ohos.app.ability.UIAbility'; 213import window from '@ohos.window'; 214export default class EntryAbility extends UIAbility { 215 onWindowStageCreate(windowStage: window.WindowStage) { 216 let context = this.context; 217 let pathDir = context.filesDir; 218 console.info('sandbox path is ' + pathDir); 219 } 220} 221``` 222 223<!--code_no_check_fa--> 224``` ts 225// FA model 226import featureAbility from '@ohos.ability.featureAbility'; 227let context = featureAbility.getContext(); 228context.getFilesDir().then((data: string) => { 229 let pathDir = data; 230 console.info('sandbox path is ' + pathDir); 231}); 232``` 233