1.同步方式获取
首先需要导包:
import bundleManager from '@ohos.bundle.bundleManager'
工具类:
public static async getVersionName(): Promise<string> {
try {
let bundleInfo =
await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
return bundleInfo.versionName;
} catch (error) {
LogUtils.e(JSON.stringify(error))
}
return "";
}
public static async getVersionCode(): Promise<string> {
try {
let bundleInfo =
await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
return bundleInfo.versionCode.toString();
} catch (error) {
LogUtils.e(JSON.stringify(error))
}
return "";
}
public static async getAppVersion() {
return `v${await this.getVersio