DEV Community

ZHZL-m
ZHZL-m

Posted on

【Journey of HarmonyOS Next】HarmonyOS Development Basics (1)

Image description

1 -> Apply Fundamentals

1.1 -> User Applications

A user application is a program that runs on the operating system of a device and provides a specific service to the user, referred to as an "application".

There are two types of apps that run on HarmonyOS:

Traditional apps that need to be installed.
Apps that provide specific functionality (i.e., meta-services).
Unless otherwise specified, the term "app" refers to both forms.

1.2 -> User Application Package Structure

HarmonyOS user application packages are released as app packs (Application Packages), which consist of one or more HAPs (HarmonyOS Ability Packages) and pack.info that describe the attributes of each HAP. HAP is a deployment package for Ability, and the HarmonyOS application code revolves around Ability components.

A HAP is a module package consisting of code, resources, third-party libraries, and application configuration files, which can be divided into two module types: entry and feature.

entry: the main module of the application. For the same device type, an app can have one or more HAPs of the entry type to support specific devices with different specifications (such as API versions and screen specifications) in the device type. If there are multiple entry modules for the same device type, you must configure the distroFilter distribution rule so that the Marketplace can accurately distribute devices of different specifications under the device type when distributing applications in the cloud.
feature: the dynamic feature module of the application. An app may or may not contain HAPs of one or more feature types. Only HAPs that include Ability can be operated independently.

1.3 -> Ability

Ability is an abstraction of the capabilities that an application has, and an application can contain one or more abilities. There are two types of Ability: Feature Ability (FA) and Particle Ability (PA). FA/PA is the basic building block of an application that enables specific business functions. FA has a UI interface, while PA does not have a UI interface.

1.4 -> Library files

A library is a third-party code (such as so, jar, bin, har, and other binary files) that an application depends on and is stored in the libs directory.

1.5 -> Resource file

The resource files (strings, images, audios, etc.) of the application are stored in the resources directory for developers to use and maintain.

1.6 -> configuration file

A configuration file (config.json) is the ability information of an application, which is used to declare the ability of the application and the required permissions of the application.

1.7 -> pack.info

Describes the attributes of each HAP in the application package, which is compiled and generated by the IDE, and the application market unpacks and stores the HAP classification according to the file. Specific attributes of HAP include:

delivery-with-install: indicates whether the HAP can be installed with the application. "true" indicates that it can be installed with the app; "false" indicates that installation with the app is not supported.
name:HAP file name.
module-type: the type of the module, entry or feature.
device-type: indicates the type of device that supports HAP running.

1.8 -> HAR

HarmonyOS Ability Resources (HAR) provides everything you need to build an application, including source code, resource files, and config.json files. Unlike HAP, HAR cannot be installed and run on a device independently, but can only be referenced as a dependency of an application module.

2 -> Introduction to Profiles

There is a "config.json" configuration file in the root directory of each HAP of the application, and the file content mainly covers the following three aspects:

The global configuration information of the application, including basic information such as the package name, manufacturer, and version number of the application.
The configuration information of an application on a specific device, including the backup and restoration capabilities of the application, network security, and so on.
The configuration information of the HAP package contains the basic attributes that must be defined for each ability (such as package name, class name, type, and the capabilities provided by the ability), as well as the permissions required for the application to access the protected parts of the system or other applications.

2.1 -> Composition of the configuration file

The configuration file "config.json" is in JSON file format, which contains a series of configuration items, each of which consists of two parts: attributes and values:

Attributes: Attributes appear in no particular order, and each attribute can only appear once at most.
Value: The value of each property is the basic data type of JSON (numeric, string, boolean, array, object, or null type).

3 -> Configuration File Description (JS/ArkTS)

In the application development project, you need to declare the package structure of the application in the config.json configuration file.

3.1 -> Internal structure of the configuration file

The config.json consists of three parts: app, deviceConfig, and module, all of which are indispensable.

Image description

config.json examples:

{
  "app": {
    "bundleName": "com.example.myapplication",
    "vendor": "example",
    "version": {
      "code": 1,
      "name": "1.0"
    },
    "apiVersion": {
      "compatible": 4,
      "target": 5,
      "releaseType": "Beta1"
    }
  },
  "deviceConfig": {},
  "module": {
    "package": "com.example.myapplication.entrymodule",
    "name": ".MyApplication",
    "deviceType": [
      "default"
    ],
    "distro": {
      "moduleName": "entry",
      "moduleType": "entry"
    },
    "abilities": [
      {
        "skills": [
          {
            "entities": [
              "entity.system.home"
            ],
            "actions": [
              "action.system.home"
            ]
          }
        ],
        "name": "com.example.myapplication.entrymodule.MainAbility",
        "icon": "$media:icon",
        "description": "$string:mainability_description",
        "label": "$string:app_name",
        "type": "page",
        "launchType": "standard"
      }
    ],
    "js": [
      {
        "pages": [
          "pages/index/index"
        ],
        "name": "default",
        "window": {
          "designWidth": 720,
          "autoDesignWidth": false
        }
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

3.2 -> The internal structure of the app object

The app object contains the global configuration information of the app.

Image description

Image description

Image description

App examples:

"app": {
    "bundleName": "com.example.myapplication",
    "vendor": "example",
    "version": {
      "code": 1,
      "name": "1.0"
    },
    "apiVersion": {
      "compatible": 4,
      "target": 5,
      "releaseType": "Beta1"
    }
  }
Enter fullscreen mode Exit fullscreen mode

3.3 -> The internal structure of the deviceConfig object

deviceConfig contains the application configuration information on the device, which can include attributes such as default, phone, tablet, tv, car, and wearable. The configuration in the default tag is applicable to all common devices, and other device types need to be configured under the tag of the device type if they have special requirements.

Image description

Image description

Image description

Image description

Image description

Image description

deviceConfig example:

"deviceConfig": {
    "default": {
        "process": "com.example.test.example",
        "supportBackup": false,
        "network": {
            "cleartextTraffic": true,
            "securityConfig": {
                "domainSettings": {
                    "cleartextPermitted": true,
                    "domains": [
                        {
                            "subdomains": true,
                            "name": "example.ohos.com"
                        }
                    ]
                }
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

3.4 -> The internal structure of the module object

The module object contains the configuration information of the HAP package.

Image description

module example:

"module": {
    "mainAbility": "MainAbility",
    "package": "com.example.myapplication.entry",
    "name": ".MyOHOSAbilityPackage",
    "description": "$string:description_application",
    "supportModes": [
        "drive"
    ],
    "deviceType": [
        "default"
    ],
    "distro": {
        "moduleName": "ohos_entry",
        "moduleType": "entry"
    },
    "abilities": [
        ...
    ],
    "shortcuts": [
        ...
    ],
    "js": [
        ...
    ],
    "reqPermissions": [
        ...
    ],
    "colorMode": "light"
}
Enter fullscreen mode Exit fullscreen mode

Image description

Distro example:

"distro": {
    "moduleName": "ohos_entry",
    "moduleType": "entry",
    "installationFree": true,
    "deliveryWithInstall": true
}
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Image description

Example of metaData:

"metaData": {
    "parameters" : [{
        "name" : "string",
        "type" : "Float",
        "description" : "$string:parameters_description"
    }],
    "results" : [{
        "name" : "string",
        "type" : "Float",
        "description" : "$string:results_description"
    }],
    "customizeData" : [{
        "name" : "string",
        "value" : "string",
        "extra" : "$string:customizeData_description"
    }]
}
Enter fullscreen mode Exit fullscreen mode

Image description

Examples of abilities:

"abilities": [
    {
        "name": ".MainAbility",
        "description": "test main ability",
        "icon": "$media:ic_launcher",
        "label": "$media:example",
        "launchType": "standard",
        "orientation": "unspecified",
        "permissions": [
        ], 
        "visible": true,
        "skills": [
            {
                "actions": [
                    "action.system.home"
                ],
                "entities": [
                    "entity.system.home"
                ]
            }
        ],
        "configChanges": [
            "locale", 
            "layout", 
            "fontSize", 
            "orientation"
        ], 
        "type": "page"
    },
    {
        "name": ".PlayService",
        "description": "example play ability",
        "icon": "$media:ic_launcher",
        "label": "$media:example",
        "launchType": "standard",
        "orientation": "unspecified",
        "visible": false,
        "skills": [
            {
                "actions": [
                    "action.play.music",
                    "action.stop.music"
                ],
                "entities": [
                    "entity.audio"
                ]
            }
        ],
        "type": "service",
        "backgroundModes": [
            "audioPlayback"
        ]
    },
    {
        "name": ".UserADataAbility",
        "type": "data",
        "uri": "dataability://com.example.world.test.UserADataAbility",
        "visible": true
    }
]
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Examples of skills:

"skills": [
    {
        "actions": [
            "action.system.home"
        ], 
        "entities": [
            "entity.system.home"
        ],
        "uris": [
            {
                 "scheme": "http",
                 "host": "www.example.com",
                 "port": "8080",
                 "path": "query/student/name",
                 "type": "text/*"
             }
         ]
    }
]
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Image description

JS example:

"js": [
    {
        "name": "default", 
        "pages": [            
            "pages/index/index",
            "pages/detail/detail"
        ],         
        "window": {
            "designWidth": 720,
            "autoDesignWidth": false
        },
        "type": "form"
    }
]
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Example of shortcuts:

"shortcuts": [
    {
        "shortcutId": "id",
        "label": "$string:shortcut",
        "intents": [
            {
                "targetBundle": "com.example.world.test",
                "targetClass": "com.example.world.test.entry.MainAbility"
            }
        ]
    }
]
Enter fullscreen mode Exit fullscreen mode

Image description

Forms examples:

"forms": [
    {
        "name": "Form_Js",
        "description": "It's Js Form",
        "type": "JS",
        "jsComponentName": "card",
        "colorMode": "auto",
        "isDefault": true,
        "updateEnabled": true,
        "scheduledUpdateTime": "11:00",
        "updateDuration": 1,
        "defaultDimension": "2*2",
        "supportDimensions": [
            "2*2",
            "2*4",
            "4*4"
        ]
    },
    {
        "name": "Form_Js",
        "description": "It's JS Form",
        "type": "Js",
        "colorMode": "auto",
        "isDefault": false,
        "updateEnabled": true,
        "scheduledUpdateTime": "21:05",
        "updateDuration": 1,
        "defaultDimension": "1*2",
        "supportDimensions": [
            "1*2"
        ],
        "landscapeLayouts": [
            "$layout:ability_form"
        ],
        "portraitLayouts": [
            "$layout:ability_form"
        ],
        "formConfigAbility": "ability://com.example.myapplication.fa/.MainAbility",
        "metaData": {
            "customizeData": [
                {
                    "name": "originWidgetName",
                    "value": "com.example.weather.testWidget"
                }
            ]
        }
    }
]
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Image description

Image description

Image description

distroFilter example:

"distroFilter":  {
    "apiVersion": {
        "policy": "include",
        "value": [4,5]
    },
    "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"]
    }
}
Enter fullscreen mode Exit fullscreen mode

Image description

commonEvents example:

"commonEvents": [
    {
        "name":"MainAbility",
        "permission": "string",
        "data":[
            "string",
            "string"
        ],
        "events": [
            "string",
            "string"
        ]
    }
]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)