惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
N
Netflix TechBlog - Medium
V
Visual Studio Blog
博客园 - Franky
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 三生石上(FineUI控件)
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
量子位
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
V
V2EX
The Cloudflare Blog
月光博客
月光博客
Last Week in AI
Last Week in AI
雷峰网
雷峰网
WordPress大学
WordPress大学
博客园 - 【当耐特】
博客园 - 聂微东
IT之家
IT之家
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - webabcd

开天辟地 HarmonyOS(鸿蒙) - 测试: 单元测试 开天辟地 HarmonyOS(鸿蒙) - 优化: HiDebug(用于获取 cpu, 内存等信息) 开天辟地 HarmonyOS(鸿蒙) - 优化: errorManager(捕获未处理异常) 开天辟地 HarmonyOS(鸿蒙) - 优化: HiAppEvent(事件日志) 开天辟地 HarmonyOS(鸿蒙) - 优化: hilog 日志 开天辟地 HarmonyOS(鸿蒙) - 安全: 用户认证 开天辟地 HarmonyOS(鸿蒙) - 安全: 申请权限 开天辟地 HarmonyOS(鸿蒙) - 安全: 关键资产 开天辟地 HarmonyOS(鸿蒙) - 安全: 加密解密 开天辟地 HarmonyOS(鸿蒙) - 信息: CommonEventManager(用于进程间通信,以及订阅系统事件) 开天辟地 HarmonyOS(鸿蒙) - 信息: emitter(用于同一进程内不同线程间或同一线程内发送和订阅事件) 开天辟地 HarmonyOS(鸿蒙) - 信息: 各种信息 开天辟地 HarmonyOS(鸿蒙) - 华为账号: 华为账号的默认登录 开天辟地 HarmonyOS(鸿蒙) - 元服务: 元服务 开天辟地 HarmonyOS(鸿蒙) - 媒体: AVImageGenerator(提取视频指定时间点的图像) 开天辟地 HarmonyOS(鸿蒙) - 媒体: AVMetadataExtractor(提取视频或音频的元数据信息) 开天辟地 HarmonyOS(鸿蒙) - 媒体: AVTranscoder(视频转码) 开天辟地 HarmonyOS(鸿蒙) - 媒体: SoundPool(音效播放器) 开天辟地 HarmonyOS(鸿蒙) - 媒体: AVPlayer(播放器,用于播放视频或音频)
开天辟地 HarmonyOS(鸿蒙) - 工具: Targets, Products
webabcd · 2025-06-13 · via 博客园 - webabcd

源码 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