This article mainly talks about the engineering structure and application configuration of Hongmeng application, during which the Android project is inserted as a comparison and understanding of migration.

To get to the point, take a look at the picture below.

Both Hongmeng project and Android project have similar structures, which can be divided into project construction and module content according to content

The project build

This will be familiar to Android developers.

The project builds dependencies (in the gradle/Wrapper folder), scripts ending with.gradle, configuration files ending with.properties, executables, and settings.gradle.

Settings. gradle defines project modules that contain metadata information such as information. Inside each module, a. Gradle script file is defined to describe how the module is compiled.

Summary of compilation information as shown below (left hongmeng, right Android)

Compilation information generally includes plug-in declarations, module information declarations, and dependency declarations.

To build modules on Android side, use the following plug-ins respectively:

  1. com.android.applicationThe plug-in buildApplicationModule, and the final product is.apkExecutable file
  2. com.android.libraryThe plug-in buildLibraryModule, and the final product is.aarfile

Hongmeng side also has the concept of Application. Hongmeng applications are released in the form of APP Pack (Application Package). It consists of one or more HAP (HarmonyOS Ability Package) and pack.info that describes the attributes of each HAP.

The simple understanding is that hongmeng application is constructed by one or more HAP, and each HAP corresponds to an engineering module

On the hongmengside, HAP is supported for independent operation, divided into two types of modules.

  1. entry, describes the main module of the application. There must be only one APP for the same device typeentryType HAP.
  2. feature, application dynamic feature module. An APP can contain one or morefeatureType HAP can also be omitted. As long as the containingAbilityHAP (think of it as the four components on the Android side) runs on its own.

The above two modules are constructed by com.huawei.ohos.hap plug-in, and the final product is.HAP file.

For the construction of Library Library, com.huawei.ohos. Library plug-in is used, and the final product is.har file, corresponding to.aar file on Android side.

The application of Hongmengside supports the independent operation of feature module, which makes the collaborative development and debugging of component engineering more convenient, and it is no longer necessary to dynamically change the module Gradle script switch plug-in.

The module content

As you can see from the above, each module has a Gradle script that defines the compilation information.

The corresponding business files in the module are stored in the SRC directory. The SRC directory structure is shown in the figure below.

Resources in the SRC directory can be divided into three parts

  1. Code files, such asmain/javaCode files in the directory;
  2. Resource fileDefined in themain/resourceDirectory;
  3. The configuration fileDefined in themain/config.jsonWithin the file.

The code files are the same as those on the Android side, and the libs directory in SRC is also used to store external dependencies.

Emphasis is placed on understanding resource files and configuration files.

Resource file

Application resource files (strings, images, and audio files) are stored in the Resources directory, which includes two categories

  • Base directory and qualifier directory
  • Rawfile directory.

A simple directory structure is shown below.

Resources | - base / / the default existing directory | | element - | | | - string. The json | | - media | | | - icon. The PNG | en_GB - vertical - car - mdpi / / qualifier example, require developers to create | | element - | | | - string. The json | | - media | | | - icon. The PNG | - rawfile / / the default directoryCopy the code

The base directory and qualifier directory are organized in the form of two-level directories. The directories must be named in accordance with official specifications to match resource files in the corresponding directories based on device status.

Level 1 subdirectories are the base directory and the qualifier directory.

The base directory is the default directory. If the resources directory of an application does not contain a qualifier directory that matches the device status, resource files in this directory are automatically referenced.

A qualifier directory needs to be created by the developer. For example, en_gB-vertical-car-MDpi, a directory name is a combination of one or more qualifiers that represent application scenarios or device characteristics.

Similar to the understanding of res directory and resource qualifiers on android side, but more abundant qualifiers are provided on Android side, which also means a wider range of scenarios and more flexible use. Supports multi-dimensional combination from language, text, country, region, vertical and horizontal screen, device type and screen density.

The secondary subdirectory is the resource directory, which is used to store basic elements such as strings, colors, booleans, as well as resource files such as media, animation, and layout.

The following resources are supported

| base -- - | | - element / / element resources | | | - Boolean. Json | | | - string. The json | | - media / / media resources | | | - icon. The PNG | | -- - | animation / / animation resources | | - zoom_in. XML | | - layout / / layout resources | | | - main_layout. XML | | - graphic / / can map resources | | | - dark. XML | | -- - profile / / stored in the form of the original file, do not restrict file name | | | - yummylau. TXTCopy the code

For example, define a boolea.json

{
  "boolean":[
      {
        "name":"follow_me",
        "value":true
      },
      {
        "name":"you_are_happy",
        "value":"$boolean:follow_me"
      }
  ]
}
Copy the code

These resource files are eventually compiled into binaries and given resource file ids.

Rawfile directory: Supports the creation of multi-layer subdirectories. You can customize the directory name and store various resource files in the rawfile directory.

The files in this directory do not match different resources based on device status. Resource files are packaged directly into the application without compilation and are not given resource file ids.

The configuration file

Each module has a config.json configuration file, which defines three parts of information in JSON format.

"DeviceConfig ":{} // Module configuration information "module":{}}Copy the code

App, including the package name, manufacturer, version number and other basic information of the application, is as follows:

{/ / application package name, used to identify the uniqueness of application, the kind of android side applicationId "bundleName" : "com. Effective. Harmony. Study", / / the description of the application development manufacturer "vendor" : "Effective ", // Application version information "version": {"code": 1, "name": "1.0"}, // Dependent API version" apiVersion": {"compatible": "Target ": 4, "releaseType": "Beta1"}}Copy the code

This information corresponds to the declaration information of the Android node in the Build. gradle script on android side.

DeviceConfig, which contains properties such as default, CAR, TV, wearable, liteWearable, smartVision, Phone, and Tablet. It describes the configuration of a device scenario.

The Settings in the default TAB are applicable to all devices and must be set. If other device types have special requirements, you need to configure them under the label of the device type.

{"default": {// Application process name "process": "com.huawei.harmony. Study ", // Whether to directly launch "directLaunch" when the device is unlocked: "SupportBackup ": false, "network security configuration ": {}}}Copy the code

This is something you don’t have on the Android side. Because hongmeng applications can be adapted to multiple terminals, Device FIG provides the ability to customize multiple terminals. For example, if you want your watch or vehicle to answer incoming calls when unlocked, set the directLaunch value to True.

Module, which contains the basic attributes that each Ability must define (such as package name, class name, type, and capabilities provided by Ability), as well as the permissions required by the application to access the system or other protected parts of the application.

Here we simply understand that Hongmeng Ability is equivalent to the four components of Android, which support both foreground interaction and background operation. There will be a special article about Ability in detail in the follow-up.

Take core attributes for example.

{/ / HAP package structure name, the application shall guarantee uniqueness "package" : "com. Effective. Hramony. Study", / / HAP entrance class name, class application "name" : ".myapplication ", // optional, HAP description" description":"", // allows Ability to run the deviceType, such as TV, car, phone etc "deviceType": [], // DISTRo: {}, // Optional, declare all ability "abilities" in the current module: [], // Optional, declare "reqPermissions": []... }}Copy the code

This part contains most of the core functions of Androidmanifest.xml on Android side, including package name information, application entry declaration and permission declaration.

At this point, the content is gone, really gone! Don’t you think it’s easy.

Let’s build a new project.

From the next issue, we will talk about development. Please follow us.

Welcome to the Android Zen account to share valuable and thoughtful technical articles with you. Can add wechat “Ming_Lyan” remarks “into the group” to join the technical exchange group, discussion of technical problems is strictly prohibited all advertising irrigation. If you have technical problems in the Android field or have doubts about your future career plan, discuss with us. Welcome to the party.