DEV Community

HarmonyOS
HarmonyOS

Posted on

Multi-Target Product Build in HarmonyOS Next (ArkUI/ArkTS)

Read the original article:Multi-Target Product Build in HarmonyOS Next (ArkUI/ArkTS)

Introduction

Multi-target (product flavor) build lets you:

· Define multiple app “variants” (products) in your project,

· Set different bundleName, icon, signing config, and even package output directories for each variant,

· Manage everything from a single codebase.

This is similar to “flavors” in Android, but natively supported by HarmonyOS Next’s build system via build-profile.json5 and module.json5.

Project-level: build-profile.json5 (root folder)

Define your products and connect them with module targets.

{
  "app": {
    "products": [
      { "name": "default", "signingConfig": "default", "runtimeOS": "HarmonyOS" }
    ]
  },
  "modules": [
    {
      "name": "entry",
      "srcPath": "./entry",
      "targets": [
        { "name": "free", "applyToProducts": ["default"] },
        { "name": "paid", "applyToProducts": ["default"] }
      ]
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Module-level: entry/build-profile.json5

Define variant-specific config using buildProfileFields for each target.

{
  "apiType": "stageMode",
  "buildOptionSet": [
    {
      "name": "free",
      "arkOptions": {
        "buildProfileFields": { "IS_PREMIUM": "false" }
      }
    },
    {
      "name": "paid",
      "arkOptions": {
        "buildProfileFields": { "IS_PREMIUM": "true" }
      }
    }
  ],
  "targets": [
    { "name": "free" },
    { "name": "paid" }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Use in Your ArkTS Code

import BuildProfile from 'BuildProfile';

if (BuildProfile.IS_PREMIUM === "true") {
  // Show premium features
} else {
  // Show free features
}
Enter fullscreen mode Exit fullscreen mode

Build & Test

In DevEco Studio, select your target (free or paid) before building/running the app.

Or use the terminal:

hvigor build --target paid
hvigor build --target free
Enter fullscreen mode Exit fullscreen mode

Conclusion

HarmonyOS Next’s multi-target system offers clean separation between app products and environment-specific configurations. Defining buildProfileFields under module targets ensures that environment details are injected at build-time, making your workflow scalable, secure, and easy to automate.

Written by Ahmet Furkan Sevim

Top comments (0)