【关键字】
编译构建 / 配置APP多目标构建产物 / deviceType不唯一
【问题描述】
在DevEco Studio中设置product配置,实际不生效,如何解决?
应用的工程为一个Entry模块。
根目录的build-profile配置了default project中支持两个target, 根目录build-profile的配置项如下:
"modules": [
{
"name": "entry",
"srcPath": "./entry",
"targets": [
{
"name": "default",
"applyToProducts": [
"default"
]
},
{
"name": "free",
//将free target打包到productA APP中
"applyToProducts": [
"default",
]
},
]
}
Entry模块配置下的build-profile
"targets": [
{
"name": "default",
"runtimeOS": "HarmonyOS",
"output": {
"artifactName": "defaultTarget" //产物名称为customizedTargetOutputName
},
"config": {
"deviceType": [ //定义free支持的设备类型为phone
"tablet"
]
},
"source": { //定义Stage模型中默认版target的pages源码文件
"pages": [
"pages/Index"
]
}
},
{
"name": "free", //免费版target名称
"runtimeOS": "HarmonyOS",
"output": {
"artifactName": "freeTarget" //产物名称为customizedTargetOutputName
},
"config": {
"deviceType": [ //定义free支持的设备类型为phone
"tablet"
]
},
"source": { //定义Stage模型中默认版target的pages源码文件
"pages": [
"pages/Index",
"pages/Page2"
]
}
},
]
在DevEco Studio中点击右上角的图标,配置了需要打包的product为default,module Target为default,如下图所示。
选择Build > Build Hap(s)/APP(s) > Build APP(s),进行编译打包如下图所示。
编译报错如下:
2024/01/02 10:03:26.754 - Ohos BundleTool [Error]: Module: (entry) and Module: (entry) have the same moduleName, please check deviceType or distroFilter of the module.
2024/01/02 10:03:26.754 - Ohos BundleTool [Error]: Module: entry has deviceType [tablet].
2024/01/02 10:03:26.754 - Ohos BundleTool [Error]: Another Module: entry has deviceType [tablet].2024/01/02 10:03:26.754 - Ohos BundleTool [Error]: Solution: Make sure the module name is valid and unique.
报错提示,根目录的build-profile文件targets配置中两个target对应的deviceType都是tablet,不唯一。
所以怀疑通过DevEco Studio中点击右上角配置了所选的product为default,module Target为default,实际没生效,导致在选择Build > Build Hap(s)/APP(s) > Build APP(s)进行编译打包时,将free的包错误打包了。
【解决方案】
根据上面配置文件判断只有一个product,即默认的default。在对应的Entry/Feature模块下配置target后,会在编译后生成不同target的HAP。如果此时通过Build App的方式打包,App中会包含不同目标的HAP产物,不同目标的产物会分发到不同的目标设备上。
例如:Entry下free的deviceType为phone,default为tablet,在应用市场分发的时候,手机设备phone只会分发到App中的free对应的HAP包,平板设备tablet只会分发到default对应的HAP包。
如果需要通过Build App的方式打包多目标产物,即构建不同的目标的App文件,需要在项目级build-profile文件中设置不同product,在IDE中选择Product后就只会生成对应目标的App。
具体配置APP多目标构建产物的详细介绍请参见:
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/ide-customized-multi-targets-and-products-guides-0000001731595144#section680552311459
每个target对应一个HAP,每个product对应一个APP包,APP包由多个目标的HAP组成,因此在执行Build > Build Hap(s)/APP(s) > Build APP(s)时,会把所有的target都编译打包。因为编译的APP包里只有一个product,这个product包含2个HAP包,所以会把这2个HAP包都编译。