实践说明
====

示例：配置工程级和模块级的自定义参数并通过切换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/16/v3/JxUxDDrnRfaq-nfB5PVc7w/zh-cn_image_0000002594475090.png?HW-CC-KV=V1&HW-CC-Date=20260612T073908Z&HW-CC-Expire=86400&HW-CC-Sign=0766A2274DDCE15A25860760F51A564510336E80FE6FD9EF646FC1DCD0187F5B)

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

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/27/v3/8Pksdpg8Qx2rX7xMSmrtDw/zh-cn_image_0000002625074583.png?HW-CC-KV=V1&HW-CC-Date=20260612T073908Z&HW-CC-Expire=86400&HW-CC-Sign=893B2FABA6F1E3E40534B0A7841F1B59E512A1B1CE97C9ED2DAE1984490CC93C)

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/c7/v3/7cJHo-QEQKqPfwOxoW4HKg/zh-cn_image_0000002625074585.png?HW-CC-KV=V1&HW-CC-Date=20260612T073908Z&HW-CC-Expire=86400&HW-CC-Sign=40C85A08ECB64F9480E6DE3D28CEB20534B9C4E783AB0BA10C5F55C596FF9372)

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

切换product为mirror。

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/cf/v3/lt30-diLQm-QJCw7sMH6hw/zh-cn_image_0000002624994439.png?HW-CC-KV=V1&HW-CC-Date=20260612T073908Z&HW-CC-Expire=86400&HW-CC-Sign=88C99CE60B031FB8DBCFBB97E9F6C26C5995AB654B34B43907CE71A3F4A06E48)

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

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/59/v3/CeEGh1p_Ts6D4vQf4dIsrQ/zh-cn_image_0000002594635004.png?HW-CC-KV=V1&HW-CC-Date=20260612T073908Z&HW-CC-Expire=86400&HW-CC-Sign=D84B481682112B95DDA1553339E5DEF043F059EDC83625AB514639F1AAFD598E)

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

**图1** 点击Button1  
![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/7e/v3/wRDQdx4CTPSh7UiyYNQ_6g/zh-cn_image_0000002594475082.png?HW-CC-KV=V1&HW-CC-Date=20260612T073908Z&HW-CC-Expire=86400&HW-CC-Sign=6CC728D54EF46C777E8109D338B1A78D850219138668DFAF53FAEA8F9D1F00F9)

**图2** 点击Button2  
![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/a9/v3/-93C9xpOR3Ozu7b-Z_GjCQ/zh-cn_image_0000002594635008.png?HW-CC-KV=V1&HW-CC-Date=20260612T073908Z&HW-CC-Expire=86400&HW-CC-Sign=ECF64082CEF865B9729D525C755E4A74471FE2D582E38966D708A5995FBEBCE9)