如何获取应用配置的versionCode和versionName?

如何获取应用配置的versionCode和versionName

阅读 415
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); 
}

参考链接

getBundleInfoForSelf

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进