HarmonyOS鸿蒙Next中如何根据Product不同来控制rawfile的文件

HarmonyOS鸿蒙Next中如何根据Product不同来控制rawfile的文件 HarmonyOS 项目页面是H5,我们放在 rawfile下的prod或test来区分生产和测试 H5,然后通过不同的 Product来打生产和测试的APP,是否有办法可以根据Product不同来控制哪些rawfile的文件将会被编译导入/隔离排除进目标包?

3 回复

参考文档:https://developer.huawei.com/consumer/cn/blog/topic/03165766457401077

【背景知识】

【修改建议】

若想对不同的外发版本进行差异化定制,在进行多目标产物配置时,可以在工程级build-profile.json5->app{}->products[]中进行相关配置。下述方案提供了针对normalvip版本的icon和label两个标签的差异化定制,具体方案参考如下:

(1)在工程级build-profile.json5中配置好products,此处除默认default之外,增加vip_product和normal_product配置;

"products": [{
  "name": "default",
  "signingConfig": "default",
  "compatibleSdkVersion": "5.0.0(12)",
  "runtimeOS": "HarmonyOS",
      "buildOption": {
        "strictMode": {
        "caseSensitiveCheck": true,
        "useNormalizedOHMUrl": true
        }
      }
    },
    {
      "name": "vip_product",
      "signingConfig": "vipSign",
      "compatibleSdkVersion": "5.0.0(12)",
      "runtimeOS": "HarmonyOS",
      "buildOption": {
        "strictMode": {
          "caseSensitiveCheck": true,
          "useNormalizedOHMUrl": true
        }
      }
    },
    {
      "name": "normal_product",
      "signingConfig": "normalSign",
      "compatibleSdkVersion": "5.0.0(12)",
      "runtimeOS": "HarmonyOS",
      "buildOption": {
        "strictMode": {
        "caseSensitiveCheck": true,
        "useNormalizedOHMUrl": true
        }
      }
    }
]

(2)在工程级build-profile.json5中的modules标签下配置targets,并且限制每个target可以在哪个product下使用。以下限制vip的配置可以被product为default和vip_product使用,normal的配置可以被product为default和normal_product使用;

"modules": [
     {
       "name": "entry",
       "srcPath": "./entry",
       "targets": [
         {
           "name": "default",
           "applyToProducts": [
             "default",
             "normal_product",
             "vip_product"
           ]
         },
         {
           "name": "vip", // target为vip时可以选择的product为default和vip_product
           "applyToProducts": [
             "default",
             "vip_product"
           ]
         },
         {
           "name": "normal", // target为normal时可以选择的product为default和normal_product
           "applyToProducts": [
             "default",
             "normal_product"
           ]
         }
       ]
     }
 ]

(3)对于模块级的build-profile.json5,在target标签下的normal、vip字段中增加abilities配置如下:

"targets": [
    {
      "name": "default"
    },
    {
      "name": "normal", // 当target为normal时,使用normal作为label
      "source": {
        "abilities": [
          {
            "name": "EntryAbility",
            "icon":"$media:layered_image",
            "label":"normal", // 实际配置中请用变量形式定义在string.json文件中
            "launchType": "singleton"
          }
        ]
      }
    },
    {
      "name": "vip", // 当target为vip时,使用vip作为label
      "source": {
        "abilities": [
          {
            "name": "EntryAbility",
            "icon":"$media:layered_image", 
            "label":"vip", // 实际配置中请用变量形式定义在string.json文件中
            "launchType": "singleton"
          }
        ]
      }
    },
    {
      "name": "ohosTest",
    }
  ]

更多关于HarmonyOS鸿蒙Next中如何根据Product不同来控制rawfile的文件的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next中,通过productName区分不同产品,在rawfile目录下按产品名创建子目录存放资源文件。代码中使用ResourceManagergetRawFile方法,通过路径productName/filename动态加载对应产品的文件。

在HarmonyOS Next中,可以通过配置模块级build-profile.json5中的product字段实现rawfile资源按产品类型隔离。具体步骤如下:

  1. rawfile目录下创建子目录(如prod/test/)存放不同环境的H5资源。
  2. build-profile.json5中针对不同产品类型配置资源路径:
{
  "products": [
    {
      "name": "prod",
      "signingConfig": "default",
      "resourceDirectories": ["src/main/resources/rawfile/prod"]
    },
    {
      "name": "test", 
      "resourceDirectories": ["src/main/resources/rawfile/test"]
    }
  ]
}
  1. 编译时通过--product prod--product test参数指定打包资源。

此方式会确保只有对应产品的rawfile资源被编译到最终应用中,实现生产/测试环境的资源隔离。

回到顶部