Introduction
In daily development, manually switching signature files and package names when dealing with multi-signature and multi-product build outputs is error-prone and time-consuming. HarmonyOS provides custom hvigor plugins and multi-target product building capabilities. We can use hvigor plugins to dynamically modify project configurations, ensuring that a single codebase can switch between different package names while maintaining core functionality. This allows us to generate customized build products through multi-target product building.
Simply put, multi-target products refer to highly customized output modules. Developers can build different HAP, HAR, HSP, APP, etc., by defining different build configurations to achieve differentiation between products. For detailed customization options, see: https://developer.huawei.com/consumer/cn/doc/best-practices/bpta-multi-target#section19846433183815
(Source code reference branch: feature/multiTargetProduct)
Multi-target product building requires modifying configuration files like build-profile.json5
and module.json5
to define different product
and target
entries. Developers can specify device types, source sets, resources, and assign different targets to products. The build tool generates targets based on these configurations and combines them into customized products.
Define default
, demo_debug
, and demo_release
signing configurations for debugging and release builds:
"signingConfigs": [
{
"name": "default", // Default certificate
"type": "HarmonyOS",
"material": { /* ... default certificate details ... */ }
},
{
"name": "demo_debug", // Debugging certificate
"type": "HarmonyOS",
"material": { /* ... debug certificate details ... */ }
},
{
"name": "demo_release", // Release certificate
"type": "HarmonyOS",
"material": { /* ... release certificate details ... */ }
}
]
Each product uses a specific signing configuration to generate 差异化 outputs:
"products": [
{
"name": "default",
"signingConfig": "default", // Default product uses default certificate
"compatibleSdkVersion": "5.0.1(13)",
"runtimeOS": "HarmonyOS",
"buildOption": { /* ... build options ... */ }
},
{
"name": "products_debug",
"signingConfig": "demo_debug", // Debug product uses debug certificate
"compatibleSdkVersion": "5.0.1(13)",
"runtimeOS": "HarmonyOS",
"buildOption": { /* ... build options ... */ }
},
{
"name": "products_release",
"signingConfig": "demo_release", // Release product uses release certificate
"compatibleSdkVersion": "5.0.1(13)",
"runtimeOS": "HarmonyOS",
"buildOption": { /* ... build options ... */ }
}
]
Configure targets in modules
to associate products with build outputs:
"modules": [
{
"name": "entry",
"srcPath": "./entry",
"targets": [
{
"name": "default",
"applyToProducts": [
"default",
"products_debug",
"products_release"
]
}
]
}
]
(Source code reference branch: feature/differentPackageConfigurations)
"signingConfigs": [
// ... existing configs ...
{
"name": "demo_debug_test2", // Signing for the second app
"type": "HarmonyOS",
"material": { /* ... testDemo2 certificate details ... */ }
}
]
Configure label
, icon
, bundleName
, and output
for differentiation. Use buildProfileFields
for custom parameters:
"products": [
// ... existing products ...
{
"name": "products_debug_test2",
"signingConfig": "demo_debug_test2",
"compatibleSdkVersion": "5.0.1(13)",
"runtimeOS": "HarmonyOS",
"label": "$string:app_name_test2", // Second app's name
"icon": "$media:app_icon_test2", // Second app's icon
"bundleName": "com.atomicservice.6917571239128090930", // Second app's package name
"buildOption": { /* ... build options ... */ },
"output": { "artifactName": "products_debug_test2" } // Unique output directory
}
]
Desktop icon and application name after it takes effect:
Use buildProfileFields
to define product-specific parameters for code differentiation.
"products": [
{
"name": "default",
"buildOption": {
"arkOptions": {
"buildProfileFields": {
"isStartNet": false,
"isDebug": true,
"productsName": "default"
// ... other custom parameters ...
}
}
}
},
// ... repeat for other products with unique values ...
]
Import BuildProfile
and use parameters in UI:
import BuildProfile from 'BuildProfile';
Column() {
Text(`productsName:${BuildProfile.productsName}`)
// ... other Text components for custom fields ...
}
Multi-target product building allows rapid switching between different build configurations and solves package name differentiation for scenarios like multi-entity app submissions (e.g., domestic vs. foreign entities on AG). While this covers basic customization, advanced needs (e.g., dynamic client_id
/app_id
in module.json5
) require integration with hvigor plugins. Future articles will explore using custom scripts to modify hard-coded configurations during builds.
Multi-Target Product Building Practice - Huawei HarmonyOS Developers HarmonyOS Multi-Environment Building Guide - Juejin