先通過一個示例,整體認識一下module.json5配置文件。
{
"module": {
"name": "entry",
"type": "entry",
"description": "$string:module_desc",
"mainElement": "EntryAbility",
"deviceTypes": [
"default",
"tablet"
],
"deliveryWithInstall": true,
"installationFree": false,
"pages": "$profile:main_pages",
"virtualMachine": "ark",
"metadata": [
{
"name": "string",
"value": "string",
"resource": "$profile:distributionFilter_config"
}
],
"abilities": [
{
"name": "EntryAbility",
"srcEntry": "./ets/entryability/EntryAbility.ts",
"description": "$string:EntryAbility_desc",
"icon": "$media:icon",
"label": "$string:EntryAbility_label",
"startWindowIcon": "$media:icon",
"startWindowBackground": "$color:start_window_background",
"exported": true,
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"ohos.want.action.home"
]
}
]
}
],
"requestPermissions": [
{
"name": "ohos.abilitydemo.permission.PROVIDER",
"reason": "$string:reason",
"usedScene": {
"abilities": [
"FormAbility"
],
"when": "inuse"
}
}
]
}
}
module.json5配置文件包含以下標簽。
表1 module.json5配置文件配置標簽說明
deviceTypes標簽
表2 deviceType標簽配置說明
deviceTypes示例:
{
"module": {
"name": "myHapName",
"type": "feature",
"deviceTypes" : [
"tablet"
]
}
}
pages標簽
該標簽是一個profile文件資源,用于指定描述頁面信息的配置文件。
{
"module": {
// ...
"pages": "$profile:main_pages", // 通過profile下的資源文件配置
}
}
在開發視圖的resources/base/profile下面定義配置文件main_pages.json,其中文件名(main_pages)可自定義,需要和前文中pages標簽指定的信息對應,配置文件中列舉了當前應用組件中的頁面信息。
表3 pages配置文件標簽說明
{
"src": [
"pages/index/mainPage",
"pages/second/payment",
"pages/third/shopping_cart",
"pages/four/owner"
]
}
metadata標簽
該標簽標識HAP的自定義元信息,標簽值為數組類型,包含name,value,resource三個子標簽。
表5 metadata標簽說明
{
"module": {
"metadata": [{
"name": "module_metadata",
"value": "a test demo for module metadata",
"resource": "$profile:shortcuts_config",
}],
"abilities": [{
"metadata": [{
"name": "ability_metadata",
"value": "a test demo for ability",
"resource": "$profile:config_file"
},
{
"name": "ability_metadata_2",
"value": "a string test",
"resource": "$profile:config_file"
}],
}],
"extensionAbilities": [{
"metadata": [{
"name": "extensionAbility_metadata",
"value": "a test for extensionAbility",
"resource": "$profile:config_file"
},
{
"name": "extensionAbility_metadata_2",
"value": "a string test",
"resource": "$profile:config_file"
}],
}]
}
}
abilities標簽
abilities標簽描述UIAbility組件的配置信息,標簽值為數組類型,該標簽下的配置只對當前UIAbility生效。
表6 abilities標簽說明
abilities示例:
{
"abilities": [{
"name": "EntryAbility",
"srcEntry": "./ets/entryability/EntryAbility.ts",
"launchType":"singleton",
"description": "$string:description_main_ability",
"icon": "$media:icon",
"label": "Login",
"permissions": [],
"metadata": [],
"exported": true,
"continuable": true,
"skills": [{
"actions": ["ohos.want.action.home"],
"entities": ["entity.system.home"],
"uris": []
}],
"backgroundModes": [
"dataTransfer",
"audioPlayback",
"audioRecording",
"location",
"bluetoothInteraction",
"multiDeviceConnection",
"wifiInteraction",
"voip",
"taskKeeping"
],
"startWindowIcon": "$media:icon",
"startWindowBackground": "$color:red",
"removeMissionAfterTerminate": true,
"orientation": " ",
"supportWindowMode": ["fullscreen", "split", "floating"],
"maxWindowRatio": 3.5,
"minWindowRatio": 0.5,
"maxWindowWidth": 2560,
"minWindowWidth": 1400,
"maxWindowHeight": 300,
"minWindowHeight": 200,
"excludeFromMissions": false
}]
}
skills標簽
該標簽標識UIAbility組件或者ExtensionAbility組件能夠接收的Want的特征。
skills示例:
{
"abilities": [
{
"skills": [
{
"actions": [
"ohos.want.action.home"
],
"entities": [
"entity.system.home"
],
"uris": [
{
"scheme":"http",
"host":"example.com",
"port":"80",
"path":"path",
"type": "text/*"
}
]
}
]
}
]
}
extensionAbilities標簽
描述extensionAbilities的配置信息,標簽值為數組類型,該標簽下的配置只對當前extensionAbilities生效。
表9 extensionAbilities標簽說明
extensionAbilities示例:
{
"extensionAbilities": [
{
"name": "FormName",
"srcEntry": "./form/MyForm.ts",
"icon": "$media:icon",
"label" : "$string:extension_name",
"description": "$string:form_description",
"type": "form",
"permissions": ["ohos.abilitydemo.permission.PROVIDER"],
"readPermission": "",
"writePermission": "",
"exported": true,
"uri":"scheme://authority/path/query",
"skills": [{
"actions": [],
"entities": [],
"uris": []
}],
"metadata": [
{
"name": "ohos.extension.form",
"resource": "$profile:form_config",
}
]
}
]
}
requestPermissions標簽
該標簽標識應用運行時需向系統申請的權限集合。
說明
在requestPermissions標簽中配置的權限項將在應用級別生效,即該權限適用于整個應用程序。
如果應用需要訂閱自己發布的事件,而且應用在extensionAbilities標簽中的permissions字段中設置了訪問該應用所需要的權限,那么應用也需要在requestPermissions標簽中注冊相關權限才能收到該事件。
requestPermissions示例:
{
"module" : {
"requestPermissions": [
{
"name": "ohos.abilitydemo.permission.PROVIDER",
"reason": "$string:reason",
"usedScene": {
"abilities": [
"EntryFormAbility"
],
"when": "inuse"
}
}
]
}
}
shortcuts標簽
shortcuts標識應用的快捷方式信息。標簽值為數組,最多可以配置四個快捷方式。其包含四個子標簽shortcutId、label、icon、wants。
metadata中指定shortcut信息,其中:
name:指定shortcuts的名稱。使用ohos.ability.shortcuts作為shortcuts信息的標識。
resource:指定shortcuts信息的資源位置。
表11 shortcuts標簽說明
在/resource/base/profile/目錄下配置shortcuts_config.json配置文件。
{
"shortcuts": [
{
"shortcutId": "id_test1",
"label": "$string:shortcut",
"icon": "$media:aa_icon",
"wants": [
{
"bundleName": "com.ohos.hello",
"abilityName": "EntryAbility"
}
]
}
]
}
在module.json5配置文件的abilities標簽中,針對需要添加快捷方式的UIAbility進行配置metadata標簽,使shortcut配置文件對該UIAbility生效。
{
"module": {
// ...
"abilities": [
{
"name": "EntryAbility",
"srcEntry": "./ets/entryability/EntryAbility.ts",
// ...
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"ohos.want.action.home"
]
}
],
"metadata": [
{
"name": "ohos.ability.shortcuts",
"resource": "$profile:shortcuts_config"
}
]
}
]
}
}
distributionFilter標簽
該標簽下的子標簽均為可選字段,用于定義HAP對應的細分設備規格的分發策略,以便應用市場在云端分發HAP時做精準匹配。該標簽需要配置在/resource/profile資源目錄下;在進行分發時,通過deviceType與下表屬性的匹配關系,唯一確定一個用于分發到設備的HAP。
表12 distributionFilter標簽說明
在開發視圖的resources/base/profile下面定義配置文件distro_filter_config.json,文件名可以自定義。
{
"distributionFilter": {
"screenShape": {
"policy": "include",
"value": [
"circle",
"rect"
]
},
"screenWindow": {
"policy": "include",
"value": [
"454*454",
"466*466"
]
},
"screenDensity": {
"policy": "exclude",
"value": [
"ldpi",
"xldpi"
]
},
"countryCode": { // 支持中國和香港地區分發
"policy": "include",
"value": [
"CN",
"HK"
]
}
}
}
在module.json5配置文件的module標簽中定義metadata信息。
{
"module": {
// ...
"metadata": [
{
"name": "ohos.module.distro",
"resource": "$profile:distro_filter_config",
}
]
}
}
testRunner標簽
此標簽用于支持對測試框架的配置。
表17 testRunner標簽說明
testRunner標簽示例:
{
"module": {
// ...
"testRunner": {
"name": "myTestRunnerName",
"srcPath": "etc/test/TestRunner.ts"
}
}
}