赞
踩
在Android或iOS开发时经常会有打“马甲”包的场景,就是一套代码打出不同主题的包,一个公司的产品可能针对不同用户提供不同的应用,比如抖音有国内版也有国外版,滴滴有个人版还有企业版,同样的在鸿蒙平台也有类似的诉求,本文我们讨论鸿蒙平台的多产物构建。
下面是一个最简单的鸿蒙工程截图:
build-profile.json5
在一个标准的Android工程中,工程下的模块使用setting.gradle
来声明,对应的鸿蒙应用的模块配置在build-profile.json5
中:
{ "app": { "signingConfigs": [], "products": [ { "name": "default", "signingConfig": "default", "compatibleSdkVersion": "5.0.0(12)", "runtimeOS": "HarmonyOS", } ], "buildModeSet": [ { "name": "debug", }, { "name": "release" } ] }, "modules": [ { "name": "entry", "srcPath": "./entry", "targets": [ { "name": "default", "applyToProducts": [ "default" ] } ] } ] }
modules列表中声明模块,其中:
注意: 这里跟Android有两个不同:
build-profile.json5
{ "apiType": "stageMode", "buildOption": { }, "buildOptionSet": [ { "name": "release", "arkOptions": { "obfuscation": { "ruleOptions": { "enable": true, "files": [ "./obfuscation-rules.txt" ] } } } }, ], "targets": [ { "name": "default" }, { "name": "ohosTest", } ] }
module.json5
在模块/main/
下的module.json5
示例:
{ "module": { "name": "entry", "type": "entry", "description": "$string:module_desc", "mainElement": "EntryAbility", "deviceTypes": [ "phone", "tablet", "2in1" ], "deliveryWithInstall": true, "installationFree": false, "pages": "$profile:main_pages", "abilities": [ { "name": "EntryAbility", "srcEntry": "./ets/entryability/EntryAbility.ets", "description": "$string:EntryAbility_desc", "icon": "$media:layered_image", "label": "$string:EntryAbility_label", "startWindowIcon": "$media:startIcon", "startWindowBackground": "$color:start_window_background", "exported": true, "skills": [ { "entities": [ "entity.system.home" ], "actions": [ "action.system.home" ] } ] } ], "extensionAbilities": [ { "name": "EntryBackupAbility", "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", "type": "backup", "exported": false, "metadata": [ { "name": "ohos.extension.backup", "resource": "$profile:backup_config" } ], } ] } }
主要作用类似于Android中的AndroidManifest,声明模块信息、deviceTypes等。
由于平级目录进行模块管理有两个缺陷不利于开发及后期维护:
官方推荐common、features、product三层工程结构:
/application
├── common # 公共特性目录
│
├── features # 功能模块目录
│ ├── feature1 # 子功能
│ ├── feature2 # 子功能2
│ └── ... # 子功能n
│
└── product # 产品层目录
├── wearable # 智能穿戴泛类目录
├── default # 默认设备泛类目录
└── ...
在介绍多目标产物构建前先介绍下target和product的概念:
要开发一个SDK,这个SDK上面又封装了两个SDK,这两个SDK分别额外实现了ToB和ToC业务的功能,这三个SDK在一个工程中,开发调试时最方便的方式就是可以有三个Demo分别调试这三个不同的SDK。
由于HarmonyOS工程中只能有一个可运行的entry模块,所以创建三个demo分别运行的方式无法跑通。
"modules": [ { "name": "app", "srcPath": "./app", "targets": [ { "name": "default", "applyToProducts": [ "default" ] } ] }, { "name": "app_c", "srcPath": "./app_c", "targets": [ { "name": "default", "applyToProducts": [ "default" ] } ] }, { "name": "app_b", "srcPath": "./app_b", "targets": [ { "name": "default", "applyToProducts": [ "default" ] } ] } ]
虽然在build_profile中声明了三个带targets模块,但是只有第一个可以运行。
可以通过把其他两个配置注释掉,使用运行哪个打开哪个的方式调试,这样的缺点就是每次调试不同模块都需要修改代码。
Biz2B和Biz2C模块合成一个模块,定义连个target,用不同代码路径区分2B或者2C,但是Biz2B和Biz2C是两个对外独立的SDK,合成一个模块无法对外差异化提供。
Biz2B和Biz2C模块各自创建两个target,分别是2B和2C,配置源码模块分别制定不同路径,Biz2B模块的2Ctarget指向空路径,这样demo工程虽然依赖了2B模块,但是调试2C的target不会有任何Biz2B的代码。可是使用模块级build_profile.json5中的sourceRoots属性,制定源码路径。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。