DEV Community

HarmonyOS
HarmonyOS

Posted on

How to Handle Dynamic bundleName in shortcuts_config.json

Read the original article:How to Handle Dynamic bundleName in shortcuts_config.json

Question

I want to configure bundleName dynamically in shortcuts_config.json using a value from strings.json, but it doesn't work. How can I achieve dynamic bundleName configuration for shortcuts?

Short Answer

shortcuts_config.json does not support dynamic values for bundleName. To handle dynamic bundleName, you need to modify the module.json5 file programmatically during the build process, for example using Hvigor’s plugin hooks. You can fins the related example below;

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

const entryNode = getNode(__filename);

entryNode.afterNodeEvaluate(node => {
  const hapContext = node.getContext(OhosPluginId.OHOS_HAP_PLUGIN) as OhosHapContext;
  const moduleJsonOpt = hapContext.getModuleJsonOpt();

  // Modify metadata to point to shortcuts config resource
  moduleJsonOpt.module.abilities[0].metadata[1] = {
    "name": "ohos.ability.shortcuts",
    "resource": "$profile:shortcuts_config"
  };

  hapContext.setModuleJsonOpt(moduleJsonOpt);
});

export default {
  system: hapTasks,
  plugins: []
}

Enter fullscreen mode Exit fullscreen mode

Applicable Scenarios

When your app’s bundleName changes per build/profile and you want your shortcut configuration to reflect that without hardcoding the value.

Reference Link

https://developer.huawei.com/consumer/en/doc/harmonyos-guides/ide-build-expanding-context

Written by Emincan Ozcan

Top comments (0)