在HarmonyOS 开发中, 如何获取APP 的versionCode 和 versionName ?

在HarmonyOS 开发中,如何获取APP 的versionCode 和 versionName ?

阅读 643
1 个回答

首先通过@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); 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进