实践说明
====

示例：配置工程级和模块级的自定义参数并通过切换product来展示不同的message。

#### 新建工程并创建一个har模块

在工程级别的build-profile.json5使用以下配置，目的是为了实现在所有模块中都可以使用到productMessage自定义参数。

通过切换不同的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": \[
            // 关联到多个product
            "default",
            "product",
            "mirror"
          \]
        }
      \]
    },
    {
      "name": "har",
      "srcPath": "./har"
    }
  \]
}

在har模块的build-profile.json5使用以下配置。

{
  "apiType": "stageMode",
  "buildOption": {
    "arkOptions": {
      // har模块的自定义参数
      "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"
    }
  \]
}

在har模块的MainPage.ets中添加以下代码。

import BuildProfile from "../../../../BuildProfile"

@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})
      }
    }
  }
}

在hap的oh-package.json5中引用本地的har模块。

{
  "name": "entry",
  "version": "1.0.0",
  "description": "Please describe the basic information.",
  "main": "",
  "author": "",
  "license": "",
  "dependencies": {
    "har": "file:../har"
  }
}

在hap的Index.ets文件中引用该har包并且使用MainPage方法。

import { MainPage } from "har"

@Entry
@Component
struct Index {
  build() {
    Row() {
      MainPage()
    }
  }
}

#### 执行预览或签名后推包到设备调试

点击har模块执行以下按钮。

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/a8/v3/AkVHT21AS_SJbJSwoh5IOQ/zh-cn_image_0000002594475090.png?HW-CC-KV=V1&HW-CC-Date=20260709T132918Z&HW-CC-Expire=86400&HW-CC-Sign=1F39989B64E78D48F2588F88DFFF652FF0EE6FFCFD7B7787647415276935B326)

default模式下初始化的message为defaultMessage。

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/52/v3/PHtue06xRRqvc0uUY_CsMQ/zh-cn_image_0000002625074583.png?HW-CC-KV=V1&HW-CC-Date=20260709T132918Z&HW-CC-Expire=86400&HW-CC-Sign=E753FC0897750A8D9128C248DDC51D6711D2A2BFAC2F698040603248D52F2A9B)

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/a4/v3/8SOufSsLSvKxZ8KCYVTP3Q/zh-cn_image_0000002625074585.png?HW-CC-KV=V1&HW-CC-Date=20260709T132918Z&HW-CC-Expire=86400&HW-CC-Sign=93EF4DF7ADE0FCDEEAF08DCF4842D53073AEC900D808E85CC1CC1B3A1BCA3A30)

通过切换不同的product，可以使用不同的自定义参数来初始化message。

切换product为mirror。

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/a2/v3/sd2HiY4vTNCQ59pLO7OfoQ/zh-cn_image_0000002624994439.png?HW-CC-KV=V1&HW-CC-Date=20260709T132918Z&HW-CC-Expire=86400&HW-CC-Sign=866D509B50B601753252B6ED5071E3EB1FC66DD92CD7076045436EF3643976C9)

可以观察到初始化参数为mirrorMessage：

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/1b/v3/xA_WnQtvR7CrFpdfRCK80g/zh-cn_image_0000002594635004.png?HW-CC-KV=V1&HW-CC-Date=20260709T132918Z&HW-CC-Expire=86400&HW-CC-Sign=BFE6CC0BE28B9404F759CD87E4B41669740CBCD9D145E7D866EB709F3EA64CA3)

点击不同的Button可以改变message为对应的自定义参数：

**图1** 点击Button1  
![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/54/v3/T6uO_2xtQOyPx5-_cWZ-cA/zh-cn_image_0000002594475082.png?HW-CC-KV=V1&HW-CC-Date=20260709T132918Z&HW-CC-Expire=86400&HW-CC-Sign=B3F56FB0CFD504128A0D775834A6B4FAC9E47E0BBD7AB32BA662AF2799E1FD8B)

**图2** 点击Button2  
![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/fe/v3/DeiOeS5jSrmXdLsNYnsnZQ/zh-cn_image_0000002594635008.png?HW-CC-KV=V1&HW-CC-Date=20260709T132918Z&HW-CC-Expire=86400&HW-CC-Sign=F05E8A978B80BA46F7CC2E7606900FB813FC159BD0D8BA13D892700EFE8A947B)