首先通过@ohos.bundle.bundleManager 模块 bundleManager.getBundleInfoForSelf()接口获取包信息 BundleInfo,然后分别通过 BundleInfo.versionCode、BundleInfo.versionName 获取所需信息。示例代码如下:import bundleManager from '@ohos.bundle.bundleManager'; import hilog from '@ohos.hilog'; let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT; try { bundleManager.getBundleInfoForSelf(bundleFlags).then((data) => { const versionCode = data.versionCode; const versionName = data.versionName; hilog.info(0x0000, 'testTag','successfully. versionCode: ${versionCode}, versionName: ${versionName}'); }).catch(err => { hilog.error(0x0000, 'testTag', 'failed. Cause: %{public}s', err.message); }); } catch (err) { hilog.error(0x0000, 'testTag', 'failed: %{public}s', err.message); }
首先通过@ohos.bundle.bundleManager 模块 bundleManager.getBundleInfoForSelf()接口获取包信息 BundleInfo,然后分别通过 BundleInfo.versionCode、BundleInfo.versionName 获取所需信息。示例代码如下: