DEV Community

ZHZL-m
ZHZL-m

Posted on

【Journey of HarmonyOS Next】DevEco Studio User Guide (5) -> Add/Remove Modules

Image description

1 -> Create a new module

A module is the basic functional unit of an application/metaservice, including source code, resource files, third-party libraries, and application/metaservice configuration files, and each module can be compiled and run independently. An application/metaservice usually contains one or more modules, so you can create multiple modules in a project, and each module is divided into two types: Ability and Library. Modules support four types: entry, feature, har, and shared.

  1. Add a new module to the project in the following three ways:

Method 1: Move the mouse over the top of the project directory, right-click, and select New > Module..., and start creating a new module, which will be created in the root directory of the project.

Method 2: Select any file in the project directory, then select File > New > Module... in the menu bar to start creating a new module, which will be created in the root directory of the project.

Method 3: Create a new directory in the root directory of the project, right-click in the directory, select New > Module..., and create a new Module, at this time, the module will be created in the file directory, which is convenient for developers to classify and manage the modules.

illustrate

Currently, you cannot right-click a module in a directory that starts with AppScope, hvigor, oh_modules, build, or a click (for example, .hvigor and .idea).

  1. On the New Project Module page, select the template you want to create and click Next.

Image description

  1. On the Module Configuration page, set the basic information of the new module and click Next.

Module name: The name of the new module, the module name cannot be the same as the project name.
Module type: This field exists only in the Ability template, and you can select the Feature and Entry types.
illustrate

Only one Entry module can be created in the same project. If you want to build an Entry module, you can modify the type field in the module.json5 file.
If an entry module already exists on the same type of device, you need to configure a distroFilter distribution rule after a new entry module appears.
Device type: Select the device type of the module, if the module type of the new module is feature, you can only select the original device type of the project. If the module type is set to entry, you can select other device types that are supported by the module.
Enable native: Only the Library template exists, and a shared package that can be invoked in C/C++ will be created.

Image description

  1. If the template type of the module is Ability, you also need to set the Ability name and Exported parameters of the new Ability, which indicate whether the Ability can be called by other applications/metaservices (Visible parameters in the FA model).

Tick (true): can be called by other applications/metaservices.
Unchecked (false): cannot be called by other applications/metaservices.

  1. Click Finish, wait until the creation is complete, and then you can view and edit the new module in the project directory. You can configure the module information in the project in the module field in build-profile.json5.

2 -> Import Module

HarmonyOS projects support the ability to import other HarmonyOS modules. Currently, only modules of FA models can be imported to FA models, and modules of Stage models can be imported to Stage models. Modules of FA models cannot be imported to Stage models, or modules of Stage models cannot be imported to FA models.

illustrate

DevEco Studio supports referencing modules in other projects outside the current project directory. In addition to importing modules in the Import Module mode, you can configure the relative path import of modules outside the project in the srcPath field in the build-profile.json5 file. If you import a project in srcPath mode, only the module-related information is referenced and the module code is not completely copied to the local computer.

  1. Click File > New > Import... > Import Module。

Image description

  1. Select the module you want to import. In the specified path, select the module to be imported and click OK. The imported module can be in folder or zip format.

Image description

3 -> Configure the distroFilter/distributionFilter distribution rules

Devices of the same type (such as Phone, Wearable, and Lite Wearable) may differ in the API version, screenShape, and screen Window resolution. In view of these differences, it is necessary to adapt and develop for different models of the same type of device, and then achieve accurate distribution in the application market, so that users of different devices can get a better experience. In order to achieve accurate distribution in the application market, you need to add multiple entry modules for the same type of device to adapt to different types of devices in a project, and then configure different distribution rules. The specific rules are as follows:

An entry is uniquely determined by the combination of attributes such as DeviceType and screenShape.
distroFilter/distributionFilter contains at least one label in the property.
If any one or more tags, such as screenShape, are configured in an Entry module, the other Entry modules must also contain the same tags.
In general, the screenShape and screenWindow tags are used in Lite Wearable devices.
The configuration format of different attribute tags is as follows. If the policy value is set to include, the app store distributes the value to the device when the device meets the value value. If the policy value is set to exclude, it means that other valid values other than the value value value will be distributed to devices in the application market.
illustrate

The policy value of the screenWindow tag can only be set to include.

Configure distribution rules for the stage model

  1. Create a .json file in the entry > src > main > resources > profile folder and configure the following code information as required:
{
   "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"] 
      }
   }
}
Enter fullscreen mode Exit fullscreen mode
  1. Specify the distribution file in the module.json file.
{
  "module": {
    "name": "MyAbilityStage",
    "metadata": [
      {
        "name": "ohos.module.distro",
        "resource": "$profile:distro_filter_config"    //distro_filter_config为被指定的分发文件
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

Configure FA model distribution rules

If there are multiple entry modules on the same device in the same project, you need to configure the distroFilter distribution rule in the config.json file of each entry module.


"module": {
  ...
  "distroFilter": {
    "标签名字": {
      "policy": "include|exclude"
      "value": [ a, b, c] 
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

4 -> Delete the Module

Select the module you want to delete in the project directory, right-click, select Delete, and click Delete in the dialog box that appears.

Top comments (0)