harmony 鸿蒙@ohos.bundle.installer (installer模块)

2022-12-05 浏览 (1381)

@ohos.bundle.installer (installer模块)

说明: 本模块首批接口从API version 9 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

在设备上安装、升级和卸载应用

导入模块

import installer from '@ohos.bundle.installer';

权限列表

权限权限等级描述
ohos.permission.INSTALL_BUNDLEsystem_core允许应用安装、卸载其他应用(除了企业相关应用,目前有企业InHouse应用,企业MDM应用和企业normal应用)。
ohos.permission.INSTALL_ENTERPRISE_BUNDLEsystem_core允许应用安装企业InHouse应用。
ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLEsystem_core允许在企业设备上安装企业MDM应用包。
ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLEsystem_core允许在企业设备上安装企业NORMAL应用包。
ohos.permission.UNINSTALL_BUNDLEsystem_core允许应用卸载应用。
ohos.permission.RECOVER_BUNDLEsystem_core允许应用恢复预置应用。
ohos.permission.INSTALL_SELF_BUNDLEsystem_core允许企业MDM应用在企业设备上自升级。

权限等级参考权限等级说明

BundleInstaller.getBundleInstaller

getBundleInstaller(callback: AsyncCallback<BundleInstaller>): void;

获取BundleInstaller对象,使用callback形式返回结果。

系统接口: 此接口为系统接口。

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

参数:

参数名类型必填说明
callbackAsyncCallback<BundleInstaller>回调函数,获取BundleInstaller对象,err为null,data为获取到的BundleInstaller对象;否则为错误对象。

示例:

import installer from '@ohos.bundle.installer';
import { BusinessError } from '@ohos.base';

try {
    installer.getBundleInstaller((err: BusinessError, data: installer.BundleInstaller) => {
        if (err) {
            console.error('getBundleInstaller failed:' + err.message);
        } else {
            console.info('getBundleInstaller successfully');
        }
    });
} catch (error) {
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed:' + message);
}

BundleInstaller.getBundleInstaller

getBundleInstaller(): Promise<BundleInstaller>;

获取BundleInstaller对象,使用callback形式返回结果。

系统接口: 此接口为系统接口。

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

返回值:

类型说明
Promise<BundleInstaller>Promise对象,返回BundleInstaller对象。

示例:

import installer from '@ohos.bundle.installer';
import { BusinessError } from '@ohos.base';

try {
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        console.info('getBundleInstaller successfully.');
    }).catch((error: BusinessError) => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
}

BundleInstaller.getBundleInstallerSync10+

getBundleInstallerSync(): BundleInstaller;

获取并返回BundleInstaller对象。

系统接口: 此接口为系统接口。

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

返回值:

类型说明
BundleInstaller返回BundleInstaller对象。

示例:

import installer from '@ohos.bundle.installer';
import { BusinessError } from '@ohos.base';

try {
    installer.getBundleInstallerSync();
    console.info('getBundleInstallerSync successfully.');
} catch (error) {
    let message = (error as BusinessError).message;
    console.error('getBundleInstallerSync failed. Cause: ' + message);
}

BundleInstaller.install

install(hapFilePaths: Array<string>, installParam: InstallParam, callback: AsyncCallback<void>): void;

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

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INSTALL_BUNDLE 或 ohos.permission.INSTALL_ENTERPRISE_BUNDLE10+ 或 ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLE10+ 或 ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE10+

说明: 从API version 10起,可通过ohos.permission.INSTALL_ENTERPRISE_BUNDLE 或 ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLE 或 ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE权限调用此接口。

安装企业应用需要ohos.permission.INSTALL_ENTERPRISE_BUNDLE权限

安装企业NORMAL应用需要ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLE或ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE权限

安装企业MDM应用需要ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE权限

安装普通应用需要ohos.permission.INSTALL_BUNDLE权限

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

参数:

参数名类型必填说明
hapFilePathsArray<string>存储应用程序包的路径。路径应该是当前应用程序中存放HAP的数据目录。当传入的路径是一个目录时, 该目录下只能放同一个应用的HAP,且这些HAP的签名需要保持一致。
installParamInstallParam指定安装所需的其他参数。
callbackAsyncCallback<void>回调函数,安装应用成功,err为null,否则为错误对象。

错误码:

以下错误码的详细介绍请参见ohos.bundle错误码

错误码ID错误信息
17700004The specified user ID is not found.
17700010Failed to install the HAP because the HAP fails to be parsed.
17700011Failed to install the HAP because the HAP signature fails to be verified.
17700012Failed to install the HAP because the HAP path is invalid or the HAP is too large.
17700015Failed to install the HAPs because they have different configuration information.
17700016Failed to install the HAP because of insufficient system disk space.
17700017Failed to install the HAP since the version of the HAP to install is too early.
17700018Failed to install because the dependent module does not exist.
17700031Failed to install the HAP because the overlay check of the HAP is failed.
17700036Failed to install the HSP because lacks appropriate permissions.
17700039Failed to install because disallow install a shared bundle by hapFilePaths.
17700041Failed to install because enterprise device management disallow install.
17700042Failed to install the HAP because of incorrect URI in the data proxy.
17700043Failed to install the HAP because of low APL in the non-system data proxy (required APL: system_basic or system_core).
17700044Failed to install the HAP because the isolationMode configured is not supported.
17700047Failed to install the HAP because the VersionCode to be updated is not greater than the current VersionCode.
17700048Failed to install the HAP because the code signature verification is failed.
17700050Failed to install the HAP because enterprise normal/MDM bundle cannot be installed on non-enterprise device.
17700052Failed to install the HAP because debug bundle cannot be installed under non-developer mode.

示例:

import installer from '@ohos.bundle.installer';
import { BusinessError } from '@ohos.base';

let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];
let installParam: installer.InstallParam = {
    userId: 100,
    isKeepData: false,
    installFlag: 1,
};

try {
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.install(hapFilePaths, installParam, (err: BusinessError) => {
            if (err) {
                console.error('install failed:' + err.message);
            } else {
                console.info('install successfully.');
            }
        });
    }).catch((error: BusinessError) => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
}

BundleInstaller.install

install(hapFilePaths: Array<string>, callback: AsyncCallback<void>): void;

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

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INSTALL_BUNDLE 或 ohos.permission.INSTALL_ENTERPRISE_BUNDLE10+ 或 ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLE10+ 或 ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE10+

说明: 从API version 10起,可通过ohos.permission.INSTALL_ENTERPRISE_BUNDLE 或 ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLE 或 ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE权限调用此接口。

安装企业应用需要ohos.permission.INSTALL_ENTERPRISE_BUNDLE权限

安装企业NORMAL应用需要ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLE或ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE权限

安装企业MDM应用需要ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE权限

安装普通应用需要ohos.permission.INSTALL_BUNDLE权限

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

参数:

参数名类型必填说明
hapFilePathsArray<string>存储应用程序包的路径。路径应该是当前应用程序中存放HAP的数据目录。当传入的路径是一个目录时, 该目录下只能放同一个应用的HAP,且这些HAP的签名需要保持一致。
callbackAsyncCallback<void>回调函数,安装应用成功,err为null,否则为错误对象。

错误码:

以下错误码的详细介绍请参见ohos.bundle错误码

错误码ID错误信息
17700010Failed to install the HAP because the HAP fails to be parsed.
17700011Failed to install the HAP because the HAP signature fails to be verified.
17700012Failed to install the HAP because the HAP path is invalid or the HAP is too large.
17700015Failed to install the HAPs because they have different configuration information.
17700016Failed to install the HAP because of insufficient system disk space.
17700017Failed to install the HAP since the version of the HAP to install is too early.
17700018Failed to install because the dependent module does not exist.
17700031Failed to install the HAP because the overlay check of the HAP is failed.
17700036Failed to install the HSP because lacks appropriate permissions.
17700039Failed to install because disallow install a shared bundle by hapFilePaths.
17700041Failed to install because enterprise device management disallow install.
17700042Failed to install the HAP because of incorrect URI in the data proxy.
17700043Failed to install the HAP because of low APL in the non-system data proxy (required APL: system_basic or system_core).
17700044Failed to install the HAP because the isolationMode configured is not supported.
17700047Failed to install the HAP because the VersionCode to be updated is not greater than the current VersionCode.
17700048Failed to install the HAP because the code signature verification is failed.
17700050Failed to install the HAP because enterprise normal/MDM bundle cannot be installed on non-enterprise device.
17700052Failed to install the HAP because debug bundle cannot be installed under non-developer mode.

示例:

import installer from '@ohos.bundle.installer';
import { BusinessError } from '@ohos.base';

let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];

try {
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.install(hapFilePaths, (err: BusinessError) => {
            if (err) {
                console.error('install failed:' + err.message);
            } else {
                console.info('install successfully.');
            }
        });
    }).catch((error: BusinessError) => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
}

BundleInstaller.install

install(hapFilePaths: Array<string>, installParam?: InstallParam) : Promise<void>;

以异步方法安装应用,使用Promise形式返回结果。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INSTALL_BUNDLE 或 ohos.permission.INSTALL_ENTERPRISE_BUNDLE10+ 或 ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLE10+ 或 ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE10+

说明: 从API version 10起,可通过ohos.permission.INSTALL_ENTERPRISE_BUNDLE 或 ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLE 或 ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE权限调用此接口。

安装企业应用需要ohos.permission.INSTALL_ENTERPRISE_BUNDLE权限

安装企业NORMAL应用需要ohos.permission.INSTALL_ENTERPRISE_NORMAL_BUNDLE或ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE权限

安装企业MDM应用需要ohos.permission.INSTALL_ENTERPRISE_MDM_BUNDLE权限

安装普通应用需要ohos.permission.INSTALL_BUNDLE权限

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

参数:

参数名类型必填说明
hapFilePathsArray<string>存储应用程序包的路径。路径应该是当前应用程序中存放HAP的数据目录。当传入的路径是一个目录时, 该目录下只能放同一个应用的HAP,且这些HAP的签名需要保持一致。
installParamInstallParam指定安装所需的其他参数,默认值:参照InstallParam的默认值。

返回值:

类型说明
Promise<void>Promise对象。无返回结果的Promise对象。

错误码:

以下错误码的详细介绍请参见ohos.bundle错误码

错误码ID错误信息
17700004The specified user ID is not found.
17700010Failed to install the HAP because the HAP fails to be parsed.
17700011Failed to install the HAP because the HAP signature fails to be verified.
17700012Failed to install the HAP because the HAP path is invalid or the HAP is too large.
17700015Failed to install the HAPs because they have different configuration information.
17700016Failed to install the HAP because of insufficient system disk space.
17700017Failed to install the HAP since the version of the HAP to install is too early.
17700018Failed to install because the dependent module does not exist.
17700031Failed to install the HAP because the overlay check of the HAP is failed.
17700036Failed to install the HSP because lacks appropriate permissions.
17700039Failed to install because disallow install a shared bundle by hapFilePaths.
17700041Failed to install because enterprise device management disallow install.
17700042Failed to install the HAP because of incorrect URI in the data proxy.
17700043Failed to install the HAP because of low APL in the non-system data proxy (required APL: system_basic or system_core).
17700044Failed to install the HAP because the isolationMode configured is not supported.
17700047Failed to install the HAP because the VersionCode to be updated is not greater than the current VersionCode.
17700048Failed to install the HAP because the code signature verification is failed.
17700050Failed to install the HAP because enterprise normal/MDM bundle cannot be installed on non-enterprise device.
17700052Failed to install the HAP because debug bundle cannot be installed under non-developer mode.

示例:

import installer from '@ohos.bundle.installer';
import { BusinessError } from '@ohos.base';

let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];
let installParam: installer.InstallParam = {
    userId: 100,
    isKeepData: false,
    installFlag: 1,
};

try {
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.install(hapFilePaths, installParam)
            .then((data: void) => {
                console.info('install successfully: ' + JSON.stringify(data));
        }).catch((error: BusinessError) => {
            console.error('install failed:' + error.message);
        });
    }).catch((error: BusinessError) => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
}

BundleInstaller.uninstall

uninstall(bundleName: string, installParam: InstallParam, callback: AsyncCallback<void>): void;

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

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INSTALL_BUNDLE 或 ohos.permission.UNINSTALL_BUNDLE

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

参数:

参数名类型必填说明
bundleNamestring待卸载应用的包名。
installParamInstallParam指定安装所需的其他参数。
callbackAsyncCallback<void>回调函数,卸载应用成功,err为null,否则为错误对象。

错误码:

以下错误码的详细介绍请参见ohos.bundle错误码

错误码ID错误信息
17700001The specified bundle name is not found.
17700004The specified user ID is not found.
17700020The specified bundle is pre-installed bundle which cannot be uninstalled.
17700040The specified bundle is a shared bundle which cannot be uninstalled.
17700045Failed to uninstall because enterprise device management disallow uninstall.

示例:

import installer from '@ohos.bundle.installer';
import { BusinessError } from '@ohos.base';

let bundleName = 'com.ohos.demo';
let installParam: installer.InstallParam = {
    userId: 100,
    isKeepData: false,
    installFlag: 1
};

try {
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.uninstall(bundleName, installParam, (err: BusinessError) => {
            if (err) {
                console.error('uninstall failed:' + err.message);
            } else {
                console.info('uninstall successfully.');
            }
        });
    }).catch((error: BusinessError) => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
}

BundleInstaller.uninstall

uninstall(bundleName: string, callback: AsyncCallback<void>): void;

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

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INSTALL_BUNDLE 或 ohos.permission.UNINSTALL_BUNDLE

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

参数:

参数名类型必填说明
bundleNamestring待卸载应用的包名。
callbackAsyncCallback<void>回调函数,卸载应用成功,err为null,否则为错误对象。

错误码:

以下错误码的详细介绍请参见ohos.bundle错误码

错误码ID错误信息
17700001The specified bundle name is not found.
17700020The specified bundle is pre-installed bundle which cannot be uninstalled.
17700040The specified bundle is a shared bundle which cannot be uninstalled.
17700045Failed to uninstall because enterprise device management disallow uninstall.

示例:

import installer from '@ohos.bundle.installer';
import { BusinessError } from '@ohos.base';

let bundleName = 'com.ohos.demo';

try {
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.uninstall(bundleName, (err: BusinessError) => {
            if (err) {
                console.error('uninstall failed:' + err.message);
            } else {
                console.info('uninstall successfully.');
            }
        });
    }).catch((error: BusinessError) => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
}

BundleInstaller.uninstall

uninstall(bundleName: string, installParam?: InstallParam) : Promise<void>;

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

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INSTALL_BUNDLE 或 ohos.permission.UNINSTALL_BUNDLE

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

参数:

参数名类型必填说明
bundleNamestring待卸载应用的包名。
installParamInstallParam指定安装所需的其他参数,默认值:参照InstallParam的默认值。

返回值:

类型说明
Promise<void>Promise对象。无返回结果的Promise对象。

错误码:

以下错误码的详细介绍请参见ohos.bundle错误码

错误码ID错误信息
17700001The specified bundle name is not found.
17700004The specified user ID is not found.
17700020The specified bundle is pre-installed bundle which cannot be uninstalled.
17700040The specified bundle is a shared bundle which cannot be uninstalled.
17700045Failed to uninstall because enterprise device management disallow uninstall.

示例:

import installer from '@ohos.bundle.installer';
import { BusinessError } from '@ohos.base';

let bundleName = 'com.ohos.demo';
let installParam: installer.InstallParam = {
    userId: 100,
    isKeepData: false,
    installFlag: 1,
};

try {
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.uninstall(bundleName, installParam)
            .then((data: void) => {
                console.info('uninstall successfully: ' + JSON.stringify(data));
        }).catch((error: BusinessError) => {
            console.error('uninstall failed:' + error.message);
        });
    }).catch((error: BusinessError) => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
}

BundleInstaller.recover

recover(bundleName: string, installParam: InstallParam, callback: AsyncCallback<void>): void;

以异步方法回滚应用到初次安装时的状态,使用callback形式返回结果。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INSTALL_BUNDLE 或 ohos.permission.RECOVER_BUNDLE

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

参数:

参数名类型必填说明
bundleNamestring待恢复应用的包名。
installParamInstallParam指定安装所需的其他参数。
callbackAsyncCallback<void>回调函数,回滚应用成功,err为null,否则为错误对象。

错误码:

以下错误码的详细介绍请参见ohos.bundle错误码

错误码ID错误信息
17700001The specified bundle name is not found.
17700004The specified user ID is not found.

示例:

import installer from '@ohos.bundle.installer';
import { BusinessError } from '@ohos.base';

let bundleName = 'com.ohos.demo';
let installParam: installer.InstallParam = {
    userId: 100,
    isKeepData: false,
    installFlag: 1
};

try {
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.recover(bundleName, installParam, (err: BusinessError) => {
            if (err) {
                console.error('recover failed:' + err.message);
            } else {
                console.info('recover successfully.');
            }
        });
    }).catch((error: BusinessError) => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
}

BundleInstaller.recover

recover(bundleName: string, callback: AsyncCallback<void>): void;

以异步方法回滚应用到初次安装时的状态,使用callback形式返回结果。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INSTALL_BUNDLE 或 ohos.permission.RECOVER_BUNDLE

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

参数:

参数名类型必填说明
bundleNamestring待恢复应用的包名。
callbackAsyncCallback<void>回调函数,回滚应用成功,err为null,否则为错误对象。

错误码:

以下错误码的详细介绍请参见ohos.bundle错误码

错误码ID错误信息
17700001The specified bundle name is not found.

示例:

import installer from '@ohos.bundle.installer';
import { BusinessError } from '@ohos.base';

let bundleName = 'com.ohos.demo';

try {
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.recover(bundleName, (err: BusinessError) => {
            if (err) {
                console.error('recover failed:' + err.message);
            } else {
                console.info('recover successfully.');
            }
        });
    }).catch((error: BusinessError) => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
}

BundleInstaller.recover

recover(bundleName: string, installParam?: InstallParam) : Promise<void>;

以异步方法回滚应用到初次安装时的状态,使用Promise形式返回结果。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INSTALL_BUNDLE 或 ohos.permission.RECOVER_BUNDLE

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

参数:

参数名类型必填说明
bundleNamestring待卸载应用的包名。
installParamInstallParam指定安装所需的其他参数,默认值:参照InstallParam的默认值。

返回值:

类型说明
Promise<void>Promise对象。无返回结果的Promise对象。

错误码:

以下错误码的详细介绍请参见ohos.bundle错误码

错误码ID错误信息
17700001The specified bundle name is not found.
17700004The specified user ID is not found.

示例:

import installer from '@ohos.bundle.installer';
import { BusinessError } from '@ohos.base';

let bundleName = 'com.ohos.demo';
let installParam: installer.InstallParam = {
    userId: 100,
    isKeepData: false,
    installFlag: 1,
};

try {
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.recover(bundleName, installParam)
            .then((data: void) => {
                console.info('recover successfully: ' + JSON.stringify(data));
        }).catch((error: BusinessError) => {
            console.error('recover failed:' + error.message);
        });
    }).catch((error: BusinessError) => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
}

BundleInstaller.uninstall10+

uninstall(uninstallParam: UninstallParam, callback : AsyncCallback<void>) : void ;

以异步方法卸载一个共享包,使用callback形式返回结果。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INSTALL_BUNDLE 或 ohos.permission.UNINSTALL_BUNDLE

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

参数:

参数名类型必填说明
uninstallParamUninstallParam共享包卸载需指定的参数信息。
callbackAsyncCallback<void>回调函数,卸载应用成功,err为null,否则为错误对象。

错误码:

以下错误码的详细介绍请参见ohos.bundle错误码

错误码ID错误信息
17700020The specified bundle is pre-installed bundle which cannot be uninstalled.
17700037The version of shared bundle is dependent on other applications.
17700038The specified shared bundle does not exist.

示例:

import installer from '@ohos.bundle.installer';
import { BusinessError } from '@ohos.base';

let uninstallParam: installer.UninstallParam = {
    bundleName: "com.ohos.demo",
};

try {
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.uninstall(uninstallParam, (err: BusinessError) => {
            if (err) {
                console.error('uninstall failed:' + err.message);
            } else {
                console.info('uninstall successfully.');
            }
        });
    }).catch((error: BusinessError) => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
}

BundleInstaller.uninstall10+

uninstall(uninstallParam: UninstallParam) : Promise<void>;

以异步方法卸载一个共享包,使用Promise形式返回结果。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INSTALL_BUNDLE 或 ohos.permission.UNINSTALL_BUNDLE

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

参数:

参数名类型必填说明
uninstallParamUninstallParam共享包卸载需指定的参数信息。

返回值:

类型说明
Promise<void>Promise对象。无返回结果的Promise对象。

错误码:

以下错误码的详细介绍请参见ohos.bundle错误码

错误码ID错误信息
17700020The specified bundle is pre-installed bundle which cannot be uninstalled.
17700037The version of shared bundle is dependent on other applications.
17700038The specified shared bundle does not exist.

示例:

import installer from '@ohos.bundle.installer';
import { BusinessError } from '@ohos.base';

let uninstallParam: installer.UninstallParam = {
    bundleName: "com.ohos.demo",
};

try {
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.uninstall(uninstallParam, (err: BusinessError) => {
            if (err) {
                console.error('uninstall failed:' + err.message);
            } else {
                console.info('uninstall successfully.');
            }
        });
    }).catch((error: BusinessError) => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
}

BundleInstaller.updateBundleForSelf10+

updateBundleForSelf(hapFilePaths: Array<string>, installParam: InstallParam, callback: AsyncCallback<void>): void;

以异步方法更新当前应用,仅限企业设备上的企业MDM应用调用,且传入的hapFilePaths中的hap必须都属于当前应用,使用callback形式返回结果。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INSTALL_SELF_BUNDLE

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

参数:

参数名类型必填说明
hapFilePathsArray<string>存储应用程序包的路径。路径应该是当前应用程序中存放HAP的数据目录。当传入的路径是一个目录时, 该目录下只能放同一个应用的HAP,且这些HAP的签名需要保持一致。
installParamInstallParam指定安装所需的其他参数。
callbackAsyncCallback<void>回调函数,安装应用成功,err为null,否则为错误对象。

错误码:

以下错误码的详细介绍请参见ohos.bundle错误码

错误码ID错误信息
17700004The specified user ID is not found.
17700010Failed to install the HAP because the HAP fails to be parsed.
17700011Failed to install the HAP because the HAP signature fails to be verified.
17700012Failed to install the HAP because the HAP path is invalid or the HAP is too large.
17700015Failed to install the HAPs because they have different configuration information.
17700016Failed to install the HAP because of insufficient system disk space.
17700017Failed to install the HAP since the version of the HAP to install is too early.
17700018Failed to install because the dependent module does not exist.
17700039Failed to install because disallow install a shared bundle by hapFilePaths.
17700041Failed to install because enterprise device management disallow install.
17700042Failed to install the HAP because of incorrect URI in the data proxy.
17700043Failed to install the HAP because of low APL in the non-system data proxy (required APL: system_basic or system_core).
17700044Failed to install the HAP because the isolationMode configured is not supported.
17700047Failed to install the HAP because the VersionCode to be updated is not greater than the current VersionCode.
17700048Failed to install the HAP because the code signature verification is failed.
17700049Failed to install the HAP because the bundleName is different from the bundleName of the caller application.
17700050Failed to install the HAP because enterprise normal/MDM bundle cannot be installed on non-enterprise device.
17700051Failed to install the HAP because the distribution type of caller application is not enterprise_mdm.

示例:

import installer from '@ohos.bundle.installer';
import { BusinessError } from '@ohos.base';

let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];
let installParam: installer.InstallParam = {
    userId: 100,
    isKeepData: false,
    installFlag: 1,
};

try {
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.updateBundleForSelf(hapFilePaths, installParam, (err: BusinessError) => {
            if (err) {
                console.error('updateBundleForSelf failed:' + err.message);
            } else {
                console.info('updateBundleForSelf successfully.');
            }
        });
    }).catch((error: BusinessError) => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
}

BundleInstaller.updateBundleForSelf10+

updateBundleForSelf(hapFilePaths: Array<string>, callback: AsyncCallback<void>): void;

以异步方法更新当前应用,仅限企业设备上的企业MDM应用调用,且传入的hapFilePaths中的hap必须都属于当前应用,使用callback形式返回结果。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INSTALL_SELF_BUNDLE

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

参数:

参数名类型必填说明
hapFilePathsArray<string>存储应用程序包的路径。路径应该是当前应用程序中存放HAP的数据目录。当传入的路径是一个目录时, 该目录下只能放同一个应用的HAP,且这些HAP的签名需要保持一致。
callbackAsyncCallback<void>回调函数,安装应用成功,err为null,否则为错误对象。

错误码:

以下错误码的详细介绍请参见ohos.bundle错误码

错误码ID错误信息
17700010Failed to install the HAP because the HAP fails to be parsed.
17700011Failed to install the HAP because the HAP signature fails to be verified.
17700012Failed to install the HAP because the HAP path is invalid or the HAP is too large.
17700015Failed to install the HAPs because they have different configuration information.
17700016Failed to install the HAP because of insufficient system disk space.
17700017Failed to install the HAP since the version of the HAP to install is too early.
17700018Failed to install because the dependent module does not exist.
17700039Failed to install because disallow install a shared bundle by hapFilePaths.
17700041Failed to install because enterprise device management disallow install.
17700042Failed to install the HAP because of incorrect URI in the data proxy.
17700043Failed to install the HAP because of low APL in the non-system data proxy (required APL: system_basic or system_core).
17700044Failed to install the HAP because the isolationMode configured is not supported.
17700047Failed to install the HAP because the VersionCode to be updated is not greater than the current VersionCode.
17700048Failed to install the HAP because the code signature verification is failed.
17700049Failed to install the HAP because the bundleName is different from the bundleName of the caller application.
17700050Failed to install the HAP because enterprise normal/MDM bundle cannot be installed on non-enterprise device.
17700051Failed to install the HAP because the distribution type of caller application is not enterprise_mdm.

示例:

import installer from '@ohos.bundle.installer';
import { BusinessError } from '@ohos.base';

let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];

try {
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.updateBundleForSelf(hapFilePaths, (err: BusinessError) => {
            if (err) {
                console.error('updateBundleForSelf failed:' + err.message);
            } else {
                console.info('updateBundleForSelf successfully.');
            }
        });
    }).catch((error: BusinessError) => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
}

BundleInstaller.updateBundleForSelf10+

updateBundleForSelf(hapFilePaths: Array<string>, installParam?: InstallParam): Promise<void>;

以异步方法更新当前应用,仅限企业设备上的企业MDM应用调用,且传入的hapFilePaths中的hap必须都属于当前应用,使用promise形式返回结果。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INSTALL_SELF_BUNDLE

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

参数:

参数名类型必填说明
hapFilePathsArray<string>存储应用程序包的路径。路径应该是当前应用程序中存放HAP的数据目录。当传入的路径是一个目录时, 该目录下只能放同一个应用的HAP,且这些HAP的签名需要保持一致。
installParamInstallParam指定安装所需的其他参数,默认值:参照InstallParam的默认值。

错误码:

以下错误码的详细介绍请参见ohos.bundle错误码

错误码ID错误信息
17700004The specified user ID is not found.
17700010Failed to install the HAP because the HAP fails to be parsed.
17700011Failed to install the HAP because the HAP signature fails to be verified.
17700012Failed to install the HAP because the HAP path is invalid or the HAP is too large.
17700015Failed to install the HAPs because they have different configuration information.
17700016Failed to install the HAP because of insufficient system disk space.
17700017Failed to install the HAP since the version of the HAP to install is too early.
17700018Failed to install because the dependent module does not exist.
17700039Failed to install because disallow install a shared bundle by hapFilePaths.
17700041Failed to install because enterprise device management disallow install.
17700042Failed to install the HAP because of incorrect URI in the data proxy.
17700043Failed to install the HAP because of low APL in the non-system data proxy (required APL: system_basic or system_core).
17700044Failed to install the HAP because the isolationMode configured is not supported.
17700047Failed to install the HAP because the VersionCode to be updated is not greater than the current VersionCode.
17700048Failed to install the HAP because the code signature verification is failed.
17700049Failed to install the HAP because the bundleName is different from the bundleName of the caller application.
17700050Failed to install the HAP because enterprise normal/MDM bundle cannot be installed on non-enterprise device.
17700051Failed to install the HAP because the distribution type of caller application is not enterprise_mdm.

示例:

import installer from '@ohos.bundle.installer';
import { BusinessError } from '@ohos.base';

let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/'];
let installParam: installer.InstallParam = {
    userId: 100,
    isKeepData: false,
    installFlag: 1,
};

try {
    installer.getBundleInstaller().then((data: installer.BundleInstaller) => {
        data.updateBundleForSelf(hapFilePaths, installParam)
            .then((data: void) => {
                console.info('updateBundleForSelf successfully: ' + JSON.stringify(data));
        }).catch((error: BusinessError) => {
            console.error('updateBundleForSelf failed:' + error.message);
        });
    }).catch((error: BusinessError) => {
        console.error('getBundleInstaller failed. Cause: ' + error.message);
    });
} catch (error) {
    let message = (error as BusinessError).message;
    console.error('getBundleInstaller failed. Cause: ' + message);
}

HashParam

应用程序安装卸载哈希参数信息。

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

系统接口: 此接口为系统接口。

名称类型必填说明
moduleNamestring应用程序模块名称。
hashValuestring哈希值。

InstallParam

应用程序安装、卸载或恢复需指定的参数信息。

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

系统接口: 此接口为系统接口。

名称类型必填说明
userIdnumber指示用户id,默认值:调用方所在用户,取值范围:大于等于0,可使用queryOsAccountLocalIdFromProcess获取当前进程所在用户。
installFlagnumber指示安装标志,枚举值:0x00:应用初次安装,0x01:应用覆盖安装,0x10:应用免安装,默认值为应用初次安装。
isKeepDataboolean卸载时是否保留数据目录,默认值为false。
hashParamsArray<HashParam>哈希值参数,默认值为空。
crowdtestDeadlinenumber众测活动的截止日期,默认值为-1,表示无截止日期约束。
sharedBundleDirPaths10+Array<String>共享包文件所在路径,默认值为空。
specifiedDistributionType10+string应用安装时指定的分发类型,默认值为空,最大长度为128字节。该字段通常由操作系统运营方的应用市场指定。
additionalInfo10+string应用安装时的额外信息,默认值为空,最大长度为3000字节。该字段通常由操作系统运营方的应用市场在安装企业应用时指定,用于保存应用的额外信息。
verifyCodeParams10+Array<VerifyCodeParam>代码签名文件参数,默认值为空。

UninstallParam10+

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

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

系统接口: 此接口为系统接口。

名称类型必填说明
bundleNamestring共享包包名。
versionCodenumber指示共享包的版本号。默认值:如果不填写versionCode,则卸载该包名的所有共享包。

VerifyCodeParam10+

应用程序代码签名文件信息。

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

系统接口: 此接口为系统接口。

名称类型必填说明
moduleNamestring应用程序模块名称。
signatureFilePathstring代码签名文件路径。

你可能感兴趣的鸿蒙文章

harmony 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager模块)

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/8a07561d82e547e2b1e3fd608bc2b2f2