0. Download the ecodev editor
(Details omitted)
1. Create a Project
Create an empty project.
Fill in the new project information.
Tip: The device type corresponds to the platform(s) where your app will be published when it goes live.
2. Three-Tier Architecture Setup
First, let's understand what the three-tier architecture is. In simple terms:
- The first tier (Entry Layer): This is the entry point for different device types (e.g., phones, watches, foldable screens, etc.).
- The second tier (Basic Features Layer): Comprising various components and pages, this layer serves as a resource for the Entry Layer to reference.
- The third tier (Common Capabilities Layer): Stores utility libraries and common configurations, which can be referenced by both the Entry Layer and the Features Layer.
The structure of the three-tier project is as follows:
commons (Common Capabilities Layer): Stores common foundational capabilities (e.g., utility libraries, common configurations). This layer can be compiled into one or more HAR or HSP packages and can only be depended on by the
products
andfeatures
layers, not the other way around.features (Basic Features Layer): Contains basic feature sets (e.g., UI and business logic implementations for relatively independent functions in the application). Each feature is highly cohesive, loosely coupled, and customizable, enabling flexible deployment in products. Features that do not require separate deployment are typically compiled into HAR or HSP packages for use by
products
or other features. Features requiring separate deployment are usually compiled into Feature-type HAP packages, which are deployed in combination with the Entry-type HAP packages underproducts
. Thefeatures
layer can horizontally invoke and depend on thecommon
layer and can be depended on by HAPs of different device forms in theproducts
layer, but it cannot depend on theproducts
layer.products (Product Customization Layer): Integrates functions and features for different device forms. Each subdirectory in this layer is compiled into an Entry-type HAP package, serving as the main entry point for the application. The
products
layer cannot invoke other modules horizontally.
3. Create the Common Layer
Create a folder named commons
in the project root directory to place modules for the common capabilities layer. You can choose between a HAR package or an HSP package; here, we select an HSP package.
Here you can name the module and select the device types that can use this module.
4. Create the Features Layer
Similar to the third step, create a folder named features
in the project root directory to place modules for the basic features layer. You can choose between a HAR package or an HSP package; here, we still select an HSP package.
5. Create the Entry Layer
Create a folder named products
in the project root directory for product modules. Rename the entry
module to phone
(note that you should rename the module, not just the folder, as shown in the figure below), then drag it into the products
folder.
In this way, the three layers are basically created.
6. Architecture Association
When you need to use tools from the basic
module in the phone
module, follow these steps:
- Open the
phone/oh-package.json5
file. - Add the following content under the
dependencies
section:
"dependencies": {
"basic": "file:../../commons/basic"
}
- Click Sync Now to update the project dependencies.
Note: The modules you want to use should be either imported in basic/Index.ets
or exported from other .ets
files under the basic
module (e.g., basic/xx/xx.ets
).
7. Tips
I believe the three - tier architecture is suitable for building relatively large projects. For normal projects, a one - tier or two - tier architecture can meet the requirements. Currently, HarmonyOS is evolving rapidly. When developing a project, it is best not to frequently change the IDE version, otherwise, there may be various unexpected issues.
If you need the project, you can check the git repository.
git clone https://gitee.com/simple-lau/harmony_template.git
The version of the article and code used is HarmonyOS 5.0.1 Release SDK.
Top comments (0)