在上一篇文章中介绍了BuildProfile的理论。本文通过一个示例,展示如何在工程级和模块级配置自定义构建参数,并通过切换product来展示不同的message。

一、示例说明

新建工程并创建一个har模块,实现以下效果:

配置层级 自定义参数 用途
工程级 productMessage 根据切换的product显示不同初始消息
HAR模块级 targetMessage1targetMessage2 点击不同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 定义三个product:default、mirror、product
buildProfileFields.productMessage 每个product配置不同的值
modules.entry.targets.applyToProducts entry模块关联到多个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中,通过相对路径导入BuildProfile:

import 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模块引用HAR

5.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()
    }
  }
}

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐