HarmonyOS 怎么给构建的产物hap重命名,名字里面带着构建类型、构建时间等信息?

如题:HarmonyOS 怎么给构建的产物hap重命名,名字里面带着构建类型、构建时间等信息?

阅读 477
1 个回答

在模块级hvigorfile.ts里:使用下述代码可以实现构建的产物hap重命名。

import { hapTasks, OhosHapContext, OhosPluginId } from '@ohos/hvigor-ohos-plugin'
import { getNode } from '@ohos/hvigor'
import { hvigor } from '@ohos/hvigor'

const entryNode = getNode(__filename)
const appContext = hvigor.getRootNode().getContext(OhosPluginId.OHOS_APP_PLUGIN) as OhosAppContext
const appJsonOpt = appContext.getAppJsonOpt()
const versionName = appJsonOpt['app']['versionName']
entryNode.afterNodeEvaluate(node => {
  const hapContext = node.getContext(OhosPluginId.OHOS_HAP_PLUGIN) as OhosHapContext
  if (hapContext && hapContext.getBuildProfileOpt) {
    const buildProfile = hapContext.getBuildProfileOpt()
    const product = buildProfile.targets[0]
    product['output'] = {
      "artifactName": "xxx-" + versionName + '_' + getTime() + '_' + appContext.getBuildMode(),
    }
    hapContext.setBuildProfileOpt(buildProfile)
  }
})
function getTime(): string {
  let date = new Date()
  let year = date.getFullYear()
  let month = (date.getMonth() + 1).toString().padStart(2, '0')
  let day = date.getDate().toString().padStart(2, '0')
  let hours = date.getHours().toString().padStart(2, '0')
  let minutes = date.getMinutes().toString().padStart(2, '0')
  return `${year}-${month}-${day}_${hours}_${minutes}`
}
export default {
  system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
  plugins: [] /* Custom plugin to extend the functionality of Hvigor. */
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进