IT评测·应用市场-qidao123.com技术社区

标题: HarmonyOS 多目标产物构建最佳实践 [打印本页]

作者: 九天猎人    时间: 2024-11-12 10:14
标题: HarmonyOS 多目标产物构建最佳实践
背景

在Android或iOS开辟时经常会有打“马甲”包的场景,就是一套代码打出差别主题的包,一个公司的产品大概针对差别用户提供差别的应用,比如抖音有国内版也有国外版,滴滴有个人版另有企业版,同样的在鸿蒙平台也有类似的诉求,本文我们讨论鸿蒙平台的多产物构建。
HarmonyOS 工程设置文件阐明

下面是一个最简单的鸿蒙工程截图:

工程 build-profile.json5

在一个标准的Android工程中,工程下的模块利用setting.gradle来声明,对应的鸿蒙应用的模块设置在build-profile.json5中:
  1. {  
  2.   "app": {  
  3.     "signingConfigs": [],  
  4.     "products": [  
  5.       {  
  6.         "name": "default",  
  7.         "signingConfig": "default",  
  8.         "compatibleSdkVersion": "5.0.0(12)",  
  9.         "runtimeOS": "HarmonyOS",  
  10.       }  
  11.     ],  
  12.     "buildModeSet": [  
  13.       {  
  14.         "name": "debug",  
  15.       },  
  16.       {  
  17.         "name": "release"  
  18.       }  
  19.     ]  
  20.   },  
  21.   "modules": [  
  22.     {  
  23.       "name": "entry",  
  24.       "srcPath": "./entry",  
  25.       "targets": [  
  26.         {  
  27.           "name": "default",  
  28.           "applyToProducts": [  
  29.             "default"  
  30.           ]  
  31.         }  
  32.       ]  
  33.     }  
  34.   ]  
  35. }
复制代码
modules列表中声明模块,此中:

注意: 这里跟Android有两个差别:

模块 build-profile.json5

  1. {  
  2.   "apiType": "stageMode",  
  3.   "buildOption": {  
  4.   },  
  5.   "buildOptionSet": [  
  6.     {  
  7.       "name": "release",  
  8.       "arkOptions": {  
  9.         "obfuscation": {  
  10.           "ruleOptions": {  
  11.             "enable": true,  
  12.             "files": [  
  13.               "./obfuscation-rules.txt"  
  14.             ]  
  15.           }  
  16.         }  
  17.       }  
  18.     },  
  19.   ],  
  20.   "targets": [  
  21.     {  
  22.       "name": "default"  
  23.     },  
  24.     {  
  25.       "name": "ohosTest",  
  26.     }  
  27.   ]  
  28. }
复制代码

module.json5

在模块/main/下的module.json5示例:
  1. {  
  2.   "module": {  
  3.     "name": "entry",  
  4.     "type": "entry",  
  5.     "description": "$string:module_desc",  
  6.     "mainElement": "EntryAbility",  
  7.     "deviceTypes": [  
  8.       "phone",  
  9.       "tablet",  
  10.       "2in1"  
  11.     ],  
  12.     "deliveryWithInstall": true,  
  13.     "installationFree": false,  
  14.     "pages": "$profile:main_pages",  
  15.     "abilities": [  
  16.       {  
  17.         "name": "EntryAbility",  
  18.         "srcEntry": "./ets/entryability/EntryAbility.ets",  
  19.         "description": "$string:EntryAbility_desc",  
  20.         "icon": "$media:layered_image",  
  21.         "label": "$string:EntryAbility_label",  
  22.         "startWindowIcon": "$media:startIcon",  
  23.         "startWindowBackground": "$color:start_window_background",  
  24.         "exported": true,  
  25.         "skills": [  
  26.           {  
  27.             "entities": [  
  28.               "entity.system.home"  
  29.             ],  
  30.             "actions": [  
  31.               "action.system.home"  
  32.             ]  
  33.           }  
  34.         ]  
  35.       }  
  36.     ],  
  37.     "extensionAbilities": [  
  38.       {  
  39.         "name": "EntryBackupAbility",  
  40.         "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets",  
  41.         "type": "backup",  
  42.         "exported": false,  
  43.         "metadata": [  
  44.           {  
  45.             "name": "ohos.extension.backup",  
  46.             "resource": "$profile:backup_config"  
  47.           }  
  48.         ],  
  49.       }  
  50.     ]  
  51.   }  
  52. }
复制代码
主要作用类似于Android中的AndroidManifest,声明模块信息、deviceTypes等。

官方保举模块结构

由于平级目录进行模块管理有两个缺陷倒霉于开辟及后期维护:

官方保举common、features、product三层工程结构:
  1. /application
  2. ├── common                  # 公共特性目录
  3. ├── features                # 功能模块目录
  4. │   ├── feature1            # 子功能
  5. │   ├── feature2            # 子功能2
  6. │   └── ...                 # 子功能n
  7. └── product                 # 产品层目录
  8.      ├── wearable            # 智能穿戴泛类目录
  9.      ├── default             # 默认设备泛类目录
  10.      └── ...
复制代码
product与target介绍

在介绍多目标产物构建前先介绍下target和product的概念:

最佳实践

目标

要开辟一个SDK,这个SDK上面又封装了两个SDK,这两个SDK分别额外实现了ToB和ToC业务的功能,这三个SDK在一个工程中,开辟调试时最方便的方式就是可以有三个Demo分别调试这三个差别的SDK。

方案

由于HarmonyOS工程中只能有一个可运行的entry模块,以是创建三个demo分别运行的方式无法跑通。
  1. "modules": [  
  2.   {  
  3.     "name": "app",  
  4.     "srcPath": "./app",  
  5.     "targets": [  
  6.       {  
  7.         "name": "default",  
  8.         "applyToProducts": [  
  9.           "default"  
  10.         ]  
  11.       }
  12.     ]  
  13.   },  
  14. {  
  15.     "name": "app_c",  
  16.     "srcPath": "./app_c",  
  17.     "targets": [  
  18.       {  
  19.         "name": "default",  
  20.         "applyToProducts": [  
  21.           "default"  
  22.         ]  
  23.       }  
  24.     ]  
  25.   },  
  26.   {  
  27.     "name": "app_b",  
  28.     "srcPath": "./app_b",  
  29.     "targets": [  
  30.       {  
  31.         "name": "default",  
  32.         "applyToProducts": [  
  33.           "default"  
  34.         ]  
  35.       }  
  36.     ]  
  37.   }
  38. ]
复制代码
虽然在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属性,制定源码路径。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4