DEV Community

simple lau
simple lau

Posted on

How to set up a HarmonyOS project from scratch

0. Download the ecodev editor

(Details omitted)

1. Create a Project

Create an empty project.

Image description

Image description

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.

Image description

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 and features 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 under products. The features layer can horizontally invoke and depend on the common layer and can be depended on by HAPs of different device forms in the products layer, but it cannot depend on the products 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.

Image description

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.

Image description

Image description

Here you can name the module and select the device types that can use this module.

Image description

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.

Image description

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:

  1. Open the phone/oh-package.json5 file.
  2. Add the following content under the dependencies section:
"dependencies": {
  "basic": "file:../../commons/basic"
}
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode

The version of the article and code used is HarmonyOS 5.0.1 Release SDK.

Top comments (0)