开天辟地 HarmonyOS(鸿蒙) - 工具: Targets, Products

源码 https://github.com/webabcd/HarmonyDemo
作者 webabcd

开天辟地 HarmonyOS(鸿蒙) - 工具: Targets, Products

示例如下:

pages\tool\TargetsProducts.ets

/*
 * Targets, Products
 *
 * 一个 hap, hsp, har 可以定制出多个 target,每个 target 都是一个定制的 hap, hsp, har
 * 一个 app 可以定制出多个 product,每个 product 都是一个定制的 app
 *
 * 步骤:
 * 1、在 entry/build-profile.json5 中做 targets 配置
 * 2、在 hsp1/build-profile.json5 中做 targets 配置
 * 3、在 app 级的 build-profile.json5 中做 products 配置和 modules 配置
 * 4、点击右上角工具栏“选择模块”框的左侧的“圆形”按钮,可以选择编译时的 product 和 target,也可以指定是 debug 模式还是 release 模式
 * 5、如需构建 hap 则:Build -> Build Hap(s)/APP(s) -> Build Hap(s)
 * 6、如需构建 app 则:Build -> Build Hap(s)/APP(s) -> Build APP(s)
 */

// 不同的 target 定制了不同的 MyConfig.ets(地址分别为:entry/src/default/MyConfig.ets, entry/src/target1/MyConfig.ets, entry/src/target2/MyConfig.ets)
// 通过类似 import { getTargetName } from 'entry/MyConfig'; 的方式导入,则不同的 target 会导入对应的不同的代码
import { getTargetName } from 'entry/MyConfig';
import { TitleBar } from '../TitleBar';

@Entry
@Component
struct TargetsProducts {

  @State message: string = ""

  aboutToAppear(): void {
    // 不同的 target 使用不同的代码
    this.message = getTargetName()
  }

  build() {
    Column({space:10}) {
      TitleBar()

      Text(this.message)

      // 不同的 target 使用不同的字符串资源
      Text($r('app.string.mytarget_name'))

      // 不同的 target 使用不同的图片资源
      Image($r('app.media.mytarget_logo')).width(100).height(100)
    }
  }
}

\entry\build-profile.json5

{
  "apiType": "stageMode",
  "buildOption": {
    "sourceOption": {
      "workers": [
        './src/main/ets/workers/myworker.ets',
        './src/main/ets/workers/transcodeworker.ets'
      ]
    }
  },
  "buildOptionSet": [
    {
      "name": "release",
      "arkOptions": {
        "obfuscation": {
          "ruleOptions": {
            "enable": false,
            "files": [
              "./obfuscation-rules.txt"
            ]
          }
        }
      }
    },
  ],
  // 配置 targets
  // 一个 hap 可以定制出多个 target,每个 target 都是一个定制的 hap
  // 注:
  // 1、如果 hap 引用了 hsp 则需要在 hsp 模块的 build-profile.json5 中配置相关的 target,否则会报错 Unable to find target 'target1' in module 'hsp1'. Make sure module 'hsp1' has the same target as module 'entry'.
  // 2、哪个 target 需要打包到哪个 product 中,是在 app 级的 build-profile.json5 中配置的
  "targets": [
    {
      "name": "default", // 此 target 的名称
      "source": {
        "sourceRoots": ["./src/default"], // 此 target 的定制代码的目录
        "abilities": [
          {
            "name": "com.webabcd.harmonydemo.EntryAbility", // 此 target 的入口 ability 的名称
            "icon":"$media:mytarget_logo", // 此 target 的入口 ability 的图标(即定制 app 的图标)
            "label":"$string:mytarget_name", // 此 target 的入口 ability 的标题(即定制 app 的标题)
          }
        ]
      },
      "resource": { // 此 target 的定制资源的目录
        "directories": [
          "./src/main/resources",
          "./src/default/resources"
        ]
      },
      "config": {
        "deviceType": [ // 此 target 支持的设备类型
          "phone",
          "tablet",
          "2in1"
        ]
      }
    },
    {
      "name": "target1",
      "source": {
        "sourceRoots": ["./src/target1"],
        "abilities": [
          {
            "name": "com.webabcd.harmonydemo.EntryAbility",
            "icon":"$media:mytarget_logo",
            "label":"$string:mytarget_name",
          }
        ]
      },
      "resource": {
        "directories": [
          "./src/main/resources",
          "./src/target1/resources"
        ]
      },
      "config": {
        "deviceType": [
          "phone",
          "tablet",
          "2in1"
        ]
      }
    },
    {
      "name": "target2",
      "source": {
        "sourceRoots": ["./src/target2"],
        "abilities": [
          {
            "name": "com.webabcd.harmonydemo.EntryAbility",
            "icon":"$media:mytarget_logo",
            "label":"$string:mytarget_name",
          }
        ]
      },
      "resource": {
        "directories": [
          "./src/main/resources",
          "./src/target2/resources"
        ]
      },
      "config": {
        "deviceType": [
          "phone",
          "tablet",
          "2in1"
        ]
      }
    }
  ]
}

\hsp1\build-profile.json5

{
  "apiType": "stageMode",
  "buildOption": {
  },
  "buildOptionSet": [
    {
      "name": "release",
      "arkOptions": {
        "obfuscation": {
          "ruleOptions": {
            "enable": false,
            "files": [
              "./obfuscation-rules.txt"
            ]
          }
        }
      },
    },
  ],
  // 配置 targets
  // 注:
  // 1、如果 hap 配置了 target,则 hap 依赖的 hsp 也需要做相关的 target 配置,否则会报错 Unable to find target 'target1' in module 'hsp1'. Make sure module 'hsp1' has the same target as module 'entry'.
  // 2、哪个 target 需要打包到哪个 product 中,是在 app 级的 build-profile.json5 中配置的
  "targets": [
    {
      "name": "default"
    },
    {
      "name": "target1"
    },
    {
      "name": "target2"
    }
  ]
}

\build-profile.json5

{
  "app": {
    "signingConfigs": [],
    // 配置 products
    // 一个 app 可以定制出多个 product,每个 product 都是一个定制的 app
    // 注:必须要存在名为 default 的 product
    "products": [
      {
        "name": "default", // 此 product 的名称
        "signingConfig": "default",
        "compatibleSdkVersion": "5.0.1(13)",
        "runtimeOS": "HarmonyOS",
        "buildOption": {
          "strictMode": {
            "caseSensitiveCheck": true,
            "useNormalizedOHMUrl": true
          }
        },
        "bundleName": "com.webabcd.harmonydemo", // 此定制 app 的 bundleName(会覆盖 AppScope/app.json5 中的相关配置)
        "icon": "$media:app_icon", // 此定制 app 的图标(会覆盖 AppScope/app.json5 中的相关配置),但是会优先使用 entry 的 target 的入口 ability 的 icon
        "label": "$string:app_name" // 此定制 app 的标题(会覆盖 AppScope/app.json5 中的相关配置),但是会优先使用 entry 的 target 的入口 ability 的 label
      },
      {
        "name": "product1",
        "signingConfig": "default",
        "compatibleSdkVersion": "5.0.1(13)",
        "runtimeOS": "HarmonyOS",
        "buildOption": {
          "strictMode": {
            "caseSensitiveCheck": true,
            "useNormalizedOHMUrl": true
          }
        },
        "bundleName": "com.webabcd.harmonydemo1",
        "icon": "$media:app_icon",
        "label": "$string:app_name"
      },
      {
        "name": "product2",
        "signingConfig": "default",
        "compatibleSdkVersion": "5.0.1(13)",
        "runtimeOS": "HarmonyOS",
        "buildOption": {
          "strictMode": {
            "caseSensitiveCheck": true,
            "useNormalizedOHMUrl": true
          }
        },
        "bundleName": "com.webabcd.harmonydemo2",
        "icon": "$media:app_icon",
        "label": "$string:app_name"
      }
    ],
    "buildModeSet": [
      {
        "name": "debug",
      },
      {
        "name": "release"
      }
    ]
  },
  // 配置 modules
  // 当使用了 targets, products 时,则需要在此处配置 target 需要打包到哪个 product 中
  "modules": [
    {
      "name": "entry",
      "srcPath": "./entry",
      "targets": [
        {
          // 将 entry 模块的名为 default 的 target 打包到名为 default 的 product 中
          "name": "default",
          "applyToProducts": [
            "default"
          ]
        },
        {
          "name": "target1",
          "applyToProducts": [
            "product1"
          ]
        },
        {
          "name": "target2",
          "applyToProducts": [
            "product2"
          ]
        }
      ]
    },
    {
      "name": "hsp1",
      "srcPath": "./hsp1",
      "targets": [
        {
          // 将 hsp1 模块的名为 default 的 target 打包到名为 default 的 product 中
          "name": "default",
          "applyToProducts": [
            "default"
          ]
        },
        {
          "name": "target1",
          "applyToProducts": [
            "product1"
          ]
        },
        {
          "name": "target2",
          "applyToProducts": [
            "product2"
          ]
        }
      ]
    },
    {
      "name": "feature1",
      "srcPath": "./feature1",
      "targets": [
        {
          "name": "default",
          "applyToProducts": [
            "default"
          ]
        }
      ]
    },
    {
      "name": "har1",
      "srcPath": "./har1",
    },
    {
      "name": "har2",
      "srcPath": "./har2",
    },
    {
      "name": "ndk1",
      "srcPath": "./ndk1",
      "targets": [
        {
          "name": "default",
          "applyToProducts": [
            "default"
          ]
        }
      ]
    }
  ]
}

\entry\src\default\MyConfig.ets

export const getTargetName = () => "default"

\entry\src\default\resources\base\element\string.json

{
  "string": [
    {
      "name": "mytarget_name",
      "value": "default"
    }
  ]
}

\entry\src\target1\MyConfig.ets

export const getTargetName = () => "target1"

\entry\src\target1\resources\base\element\string.json

{
  "string": [
    {
      "name": "mytarget_name",
      "value": "target1"
    }
  ]
}

\entry\src\target2\MyConfig.ets

export const getTargetName = () => "target2"

\entry\src\target2\resources\base\element\string.json

{
  "string": [
    {
      "name": "mytarget_name",
      "value": "target2"
    }
  ]
}

源码 https://github.com/webabcd/HarmonyDemo
作者 webabcd

posted @ 2025-06-13 13:34  webabcd  阅读(11)  评论(0)    收藏  举报