HarmonyOS的应用软件包以APP Pack(Application Package)形式发布,它是由一个或多个HAP(HarmonyOS Ability Package)以及描述每个HAP属性的pack.info组成。HAP是Ability的部署包,HarmonyOS应用代码围绕Ability组件展开。
一个HAP是由代码、资源、第三方库及应用配置文件组成的模块包,可分为entry和feature两种模块类型,如图1所示。
图1 APP逻辑视图
Ability是应用所具备的能力的抽象,一个应用可以包含一个或多个Ability。Ability分为两种类型:FA(Feature Ability)和PA(Particle Ability)。FA/PA是应用的基本组成单元,能够实现特定的业务功能。FA有UI界面,而PA无UI界面。
库文件是应用依赖的第三方代码形式,存放在libs目录,是.so文件。
应用的资源文件(字符串、图片、音频等)存放于resources目录下,便于开发者使用和维护,详见资源文件分类。
配置文件 (config.json) 是应用的Ability信息,用于声明应用的Ability,以及应用所需权限等信息,详见应用配置文件。
描述应用软件包中每个HAP的属性,由IDE编译生成,应用市场根据该文件进行拆包和HAP的分类存储。HAP的具体属性包括:
应用的每个HAP的根目录下都存在一个“config.json”配置文件,主要涵盖以下三个方面:
配置文件“config.json”采用JSON文件格式,由属性和值两部分构成:
此部分提供“config.json”文件中所有属性的详细解释。
应用的配置文件“config.json”中由“app”、“deviceConfig”和“module”三个部分组成,缺一不可。配置文件的内部结构说明参见表1。
表1 配置文件的内部结构说明
app对象包含应用的全局配置信息,内部结构说明参见表2。
表2 app对象的内部结构说明
app示例:
"app": {
"bundleName": "com.huawei.hiworld.example",
"vendor": "huawei",
"version": {
"code": 2,
"name": "2.0"
} "apiVersion": {
"compatible": 3,
"target": 3
}}
deviceConfig包含在具体设备上的应用配置信息,可以包含default、car、tv、wearable、liteWearable、smartVision等属性。default标签内的配置是适用于所有设备通用,其他设备类型如果有特殊的需求,则需要在该设备类型的标签下进行配置。内部结构说明参见表3。
表3 deviceConfig对象的内部结构说明
default、car、tv、wearable、liteWearable、smartVision等对象的内部结构说明,可参见表4。
表4 default/car/tv/wearable等对象的内部结构说明
表5 network对象的内部结构说明
表6 securityConfig对象的内部结构说明
deviceConfig示例:
"deviceConfig": {
"default": {
"process": "com.huawei.hiworld.example",
"directLaunch": false,
"supportBackup": false,
"network": {
"usesCleartext": true,
"securityConfig": {
"domainSettings": {
"cleartextPermitted": true,
"domains": [
{
"subDomains": true,
"name": "example.ohos.com"
}
]
}
}
}
}
}
module对象包含HAP包的配置信息,内部结构说明参见表7。
表7 module对象的内部结构说明
module示例:
"module": {
"package": "com.example.myapplication.entry",
"name": ".MyOHOSAbilityPackage",
"description": "$string:description_application",
"supportedModes": [
"drive"
], ], "deviceType": [
"car"
], "distro": {
"deliveryWithInstall": true,
"moduleName": "ohos_entry",
"moduleType": "entry"
}, "abilities": [
... ], "shortcuts": [
... ], "js": [
... ], "reqPermissions": [
... ], "defPermissions": [
... ]}
表8 distro对象的内部结构说明
distro示例:
"distro": {
"deliveryWithInstall": true,
"moduleName": "ohos_entry",
"moduleType": "entry"
}
表9 abilities对象的内部结构说明
abilities示例:
"abilities": [
{ "name": ".MainAbility",
"description": "$string:description_main_ability",
"icon": "$media:hiworld.png",
"label": "HiMusic",
"type": "page",
"formEnabled": false,
"launchType": "standard",
"orientation": "unspecified",
"permissions": [
], "visible": false,
"skills": [
{ "actions": [
"action.system.home"
], "entities": [
"entity.system.home"
] } ], "configChanges": [
"locale",
"layout",
"fontSize",
"orientation"
], "directLaunch": false,
"process": "string",
"backgroundModes": [
"dataTransfer",
"audioPlayback",
"audioRecording",
"pictureInPicture",
"voip",
"location",
"bluetoothInteraction",
"wifiInteraction",
"screenFetch"
], }]
表10 skills对象的内部结构说明
skills示例:
"skills": [
{ "actions": [
"action.system.home"
], "entities": [
"entity.system.home"
], "uris": [
{ "scheme": "http",
"host": "www.xxx.com",
"port": "8080",
"path": "query/student/name",
"type": "text"
} ] }]
表11 form对象的内部结构说明
form示例:
"form": {
"formEntity": [
"homeScreen",
"searchbox"
],
"minHeight": 100,
"maxHeight": 200,
"minWidth": 100,
"maxWidth": 200
}
表12 js对象的内部结构说明
js示例:
"js": [
{ "name": "default",
"pages": [
"pages/index/index",
"pages/detail/detail"
], "window": {
"designWidth": 750,
"autoDesignWidth": false
} }]
表13 shortcuts对象的内部结构说明
示例:
"shortcuts": [
{ "shortcutId": "id",
"label": "$string:shortcut",
"intents": [
{ "targetBundle": "com.huawei.hiworld.himusic",
"targetClass": "com.huawei.hiworld.himusic.entry.MainAbility"
} ] }]
以JSON文件为config.json的一个简单示例,该示例的应用声明为三个Ability。
{
"app": {
"bundleName": "com.huawei.hiworld.himusic",
"vendor": "huawei",
"version": {
"code": 2,
"name": "2.0"
} "apiVersion": {
"compatible": 3,
"target": 3
} }, "deviceConfig": {
"default": {
} }, "module": {
"package": "com.huawei.hiworld.himusic.entry",
"name": ".MainApplication",
"supportedModes": [
"drive"
], "distro": {
"moduleType": "entry",
"deliveryWithInstall": true,
"moduleName": "hap-car"
}, "deviceType": [
"car"
], "abilities": [
{ "name": ".MainAbility",
"description": "himusic main ability",
"icon": "$media:ic_launcher",
"label": "HiMusic",
"launchType": "standard",
"orientation": "unspecified",
"visible": true,
"skills": [
{ "actions": [
"action.system.home"
], "entities": [
"entity.system.home"
] } ], "type": "page",
"formEnabled": false
}, { "name": ".PlayService",
"description": "himusic play ability",
"icon": "$media:ic_launcher",
"label": "HiMusic",
"launchType": "standard",
"orientation": "unspecified",
"visible": false,
"skills": [
{ "actions": [
"action.play.music",
"action.stop.music"
], "entities": [
"entity.audio"
] } ], "type": "service",
"formEnabled": false,
"backgroundModes": [
"audioPlayback"
] }, { "name": ".UserADataAbility",
"type": "data",
"uri": "dataability://com.huawei.hiworld.himusic.UserADataAbility",
"visible": true
} ], "reqPermissions": [{
"name": "ohos.permission.DISTRIBUTED_DATASYNC",
"reason": "",
"usedScene": {
"ability": [
"com.huawei.hiworld.himusic.entry.MainAbility",
"com.huawei.hiworld.himusic.entry.PlayService"
], "when": "inuse"
} } ] }}