鸿蒙 获取定义构建参数(二)

发布时间:2026/7/22 19:07:09
鸿蒙 获取定义构建参数(二) 在上一篇文章中介绍了BuildProfile的理论。本文通过一个示例展示如何在工程级和模块级配置自定义构建参数并通过切换product来展示不同的message。一、示例说明新建工程并创建一个har模块实现以下效果配置层级自定义参数用途工程级productMessage根据切换的product显示不同初始消息HAR模块级targetMessage1、targetMessage2点击不同Button显示不同消息二、工程级build-profile.json5配置在工程级配置中为不同的product定义不同的productMessage值实现在所有模块中都可以使用该自定义参数。{ app: { products: [ { name: default, signingConfig: default, compatibleSdkVersion: 26.0.0, runtimeOS: HarmonyOS, buildOption: { arkOptions: { buildProfileFields: { productMessage: defaultMessage } } } }, { name: mirror, signingConfig: default, compatibleSdkVersion: 26.0.0, runtimeOS: HarmonyOS, buildOption: { arkOptions: { buildProfileFields: { productMessage: mirrorMessage } } } }, { name: product, signingConfig: default, compatibleSdkVersion: 26.0.0, runtimeOS: HarmonyOS, buildOption: { arkOptions: { buildProfileFields: { productMessage: productMessage } } } } ], buildModeSet: [ { name: debug }, { name: release } ] }, modules: [ { name: entry, srcPath: ./entry, targets: [ { name: default, applyToProducts: [ default, product, mirror ] } ] }, { name: har, srcPath: ./har } ] }备注配置项说明products定义三个productdefault、mirror、productbuildProfileFields.productMessage每个product配置不同的值modules.entry.targets.applyToProductsentry模块关联到多个product三、HAR模块级build-profile.json5配置在har模块中配置模块级自定义参数。{ apiType: stageMode, buildOption: { arkOptions: { buildProfileFields: { targetMessage1: this is target buildProfileValue1, targetMessage2: this is target buildProfileValue2 } } }, buildOptionSet: [ { name: release, arkOptions: { obfuscation: { ruleOptions: { enable: true, files: [./obfuscation-rules.txt] }, consumerFiles: [./consumer-rules.txt] } } } ], targets: [ { name: default } ] }备注配置项说明buildProfileFields.targetMessage1/2模块级自定义参数四、HAR模块实现4.1 导入BuildProfile在har模块的MainPage.ets中通过相对路径导入BuildProfileimport BuildProfile from ../../../../BuildProfile注意HAR模块的BuildProfile文件生成在模块根目录使用相对路径导入。4.2 MainPage组件Preview Component export struct MainPage { // 默认赋值为工程级BuildProfile自定义参数配置的productMessage State message: string BuildProfile.productMessage build() { Row() { Column() { Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceAround }) { Button(Button 1).width(40%) .onClick(() { // 点击展示自定义字段targetMessage1 this.message BuildProfile.targetMessage1; }) Button(Button 2).width(40%) .onClick(() { // 点击展示自定义字段targetMessage2 this.message BuildProfile.targetMessage2; }) } .margin(20) .width(315) Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) { Text(this.message) .textAlign(TextAlign.Start) .fontSize(12) .border({ width: 1 }) .padding(10) .width(100%) } .height(600) .width(350) .padding({ left: 35, right: 35 }) } } } }说明操作显示内容初始加载显示BuildProfile.productMessage取决于当前product点击Button1显示BuildProfile.targetMessage1点击Button2显示BuildProfile.targetMessage2五、HAP模块引用HAR5.1 配置依赖在hap的oh-package.json5中引用本地har模块{ name: entry, version: 1.0.0, dependencies: { har: file:../har } }5.2 使用HAR组件在hap的Index.ets中引用har包并使用MainPage组件import { MainPage } from har Entry Component struct Index { build() { Row() { MainPage() } } }