Hongmeng Development-build the first Hongmeng application

Preface

At the Huawei Developer Conference on September 10, 2020, Huawei upgraded its own Huawei Hongmeng system to version 2.0 of the Huawei Hongmeng system, namely HarmonyOS 2.0. Yu Chengdong said that the beta version of Hongmeng 2.0 will be provided to developers in December 2020. However, only the watch and smart screen (TV) versions are currently supported, and the mobile phone version is expected to be supported by the end of this year.

HarmonyOS system definition

HarmonyOS is a "future-oriented" distributed operating system for all scenarios (mobile office, sports health, social communication, media entertainment, etc.). On the basis of the traditional single-device system capability, HarmonyOS proposes a distributed concept based on the same system capability and adapting to multiple terminal forms, which can support multiple terminal devices.

  • For consumers, HarmonyOS can integrate the capabilities of various terminals in life scenes to form a "super virtual terminal", which can realize rapid connection, mutual assistance, resource sharing between different terminal devices, and match suitable equipment , Provide a smooth full-scene experience;
  • For application developers, HarmonyOS uses a variety of distributed technologies, making application development and realization independent of the differences in the form of different terminal devices, reducing development difficulty and cost. This allows developers to focus on the upper-level business logic and develop applications more conveniently and efficiently;
  • For device developers, HarmonyOS adopts a componentized design scheme, which can be flexibly tailored according to the resource capabilities and business characteristics of the device to meet the requirements of different types of terminal devices for the operating system.

Construction of development environment

As the saying goes: "Workers must first sharpen their tools if they want to do their jobs well." Then a good development environment is essential for the development of building applications. DevEco Studio is used to develop HarmonyOS applications, so first go to the HarmonyOS official website to download Installation package, download address: https://developer.harmonyos.com/cn/develop/deveco-studio

Take the Windows system as an example. After the installation package is downloaded, the operation of installing DevEco Studio follows.

First, double-click the downloaded "deveco-studio-xxxx.exe" to enter the DevEco Studio installation wizard, check the DevEco Studio launcher in the following installation options interface, and click Next until the installation is complete. As shown below:

Insert picture description here
The next step is to configure the development environment. The DevEco Studio development environment needs to rely on the network environment and needs to be connected to the network to ensure the normal use of the tool.

Download HarmonyOS SDK, Devco Studio provides SDK Manager to manage the SDK and tool chain uniformly. When downloading SDK packages of various programming languages, SDK Manager will automatically download the tool chains that the SDK packages depend on. We only use the Java SDK here, so only Just download the Java SDK. It should be noted that HarmonyOS supports the development of Java, JavaScript and C/C++ languages. When downloading the HarmonyOS SDK for the first time, only the Java SDK and Toolchains will be downloaded by default. Therefore, if you need to use JS or C/C++ language to develop applications, you need to manually download the corresponding SDK package.

  • Click Configure> Settings or the default shortcut key Ctrl+Alt+S on the menu bar to open the Settings configuration interface;
  • Enter Appearance&Behavior> System Settings> HarmonyOS SDK menu interface, click the Edit button to set the storage path of HarmonyOS SDK;

As shown below:

Insert picture description here

  • Select the HarmonyOS SDK storage path, and then click Next. In the pop-up License Agreement window, click Accept to start downloading the SDK;
  • Wait for the HarmonyOS SD K and tools to be downloaded, click Finish, you can see that the default SDK Platforms>Java SDK and SDK Tools>Toolchains have been downloaded.

As a digression, DevEco Studio and Android Studio for developing Android programs are based on the secondary development of IntelliJ IDEA Community Edition, so it should be easier for developers who have used Android Studio or IntelliJ IDEA to use DevEco Studio.

Build and run the first program

  • First open DevEco Studio, click Create HarmonyOS Project on the welcome page to create a new project;
  • Select the device type and template. Take TV as an example, select Empty Feature Ability (Java), and click Next. As shown below:

Insert picture description here

  • Fill in the project-related information, keep the default values, and click Finish. As shown below:

Insert picture description here

  • On the DevEco Studio menu bar, click Tools> HVD Manager. When using the simulator for the first time, you need to download the relevant resources of the simulator, please click OK, and after the resource download is complete, click the Refresh button in the lower left corner of the simulator interface;

  • The HUAWEI ID login interface pops up in the browser, please enter the user name and password of the HUAWEI ID that has been verified by real-name authentication to log in;

  • After logging in, please click the Allow button on the interface to authorize. As shown below:

Insert picture description here

  • Because what you just created is a TV project, in the device list, select the TV device and click the triangle button to run the simulator. As shown below:

Insert picture description here

  • Click the triangle button in the DevEco Studio toolbar to run the project, or use the default shortcut key Shift+F10 to run the project;

  • Select Connected Devices in the Select Deployment Target interface that pops up, and click the OK button;

  • DevEco Studio will start the compilation and construction of the application, and after completion, the application can run on the Remote Device. As shown below:

Insert picture description here

Engineering and code analysis

When creating a HarmonyOS Java project in DevEco Studio, the default generated Java project directory structure is shown in the following figure:

Insert picture description here

  • .gradle: Gradle configuration file, which is automatically generated by the system and generally does not need to be modified.

  • entry: The default startup module (main module), a directory used by developers to write source code files and development resource files.

  • entry>libs: used to store the dependent files of the entry module.

  • entry>.gitgnore: Identifies files that need to be ignored for git version management.

  • entry>build.gradle: The compilation configuration file of the entry module.

  • entry>src>main>Java: used to store Java source code.

  • entry>src>main>resources: used to store resource files.

  • entry>src>main>config.json: HAP manifest file.

  • entry>src>test: The directory for writing test files.

Code analysis

There are three Java code files generated by the project, namely TestDemo (project name).java, MainAbility.java and MainAbilitySlice.java.

The TestDemo.java code is as follows:

import ohos.aafwk.ability.AbilityPackage;

public class TestDemo extends AbilityPackage {
    
    
    @Override
    public void onInitialize() {
    
    
        super.onInitialize();
    }
}

I haven't come to look at the source code of HarmonyOS in a hurry, but I guess the function of this file is similar to the Application in the Android project, as opposed to the entry point when the application starts.

The MainAbility.java code is as follows:

import com.example.testdemo.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;

public class MainAbility extends Ability {
    
    
    @Override
    public void onStart(Intent intent) {
    
    
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
    }
}

This file is inherited from Ability, which is an abstraction of the capabilities of the application and an important part of the application. An application can have multiple capabilities (that is, it can contain multiple abilities), and HarmonyOS supports deployment of applications based on the ability. Ability can be divided into two types: FA (Feature Ability) and PA (Particle Ability). Each type provides developers with different templates to implement different business functions.

The MainAbilitySlice.java code is as follows:

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.DirectionalLayout.LayoutConfig;
import ohos.agp.components.Text;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;

public class MainAbilitySlice extends AbilitySlice {
    
    

    private DirectionalLayout myLayout = new DirectionalLayout(this);

    @Override
    public void onStart(Intent intent) {
    
    
        super.onStart(intent);
        LayoutConfig config = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
        myLayout.setLayoutConfig(config);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(255, 255, 255));
        myLayout.setBackground(element);

        Text text = new Text(this);
        text.setLayoutConfig(config);
        text.setText("_彼岸雨敲窗_构建的第一个鸿蒙应用程序");
        text.setTextColor(new Color(0xFF000000));
        text.setTextSize(50);
        text.setTextAlignment(TextAlignment.CENTER);
        myLayout.addComponent(text);
        super.setUIContent(myLayout);
    }

    @Override
    public void onActive() {
    
    
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
    
    
        super.onForeground(intent);
    }
}

The function of this file is similar to the Activity in the Android project, in which the life cycle onStart (Intent intent) of the overriding MainAbilitySlice is equivalent to the life cycle of the activity onCreate (Bundle savedInstanceState). The function of this line of code super.setUIContent(myLayout) in MainAbilitySlice is to load the layout file, similar to the setContentView(R.layout.activity_main) of Activity in Android. In the code, first construct a Directional Layout (DirectionalLayout), which is similar to the Linear Layout in Android; myLayout.setLayoutConfig(config) is to set the layout properties, such as the width and height of the layout; myLayout.setBackground(element ) Is to set the background color of the layout; Text is a text control, similar to the TextView control in Android; text.setLayoutConfig(config) is to set the properties of the text control, such as the width and height of the text control; text.setText("") is Set the content of the text; text.setTextColor(new Color(0xFF000000)) is to set the color of the text; text.setTextSize(50) is to set the size of the text; text.setTextAlignment(TextAlignment.CENTER) is to set the alignment of the text; myLayout. The function of addComponent(text) is to add text to the layout file; super.setUIContent(myLayout) is to add the layout file to the interface.

postscript

Although the launch of HarmonyOS 2.0 is highly controversial on the Internet, HarmonyOS Java API and Android API have many similarities. Many native Android developers question whether HarmonyOS is simply a "skin" of Android, or it is a direct "plagiarism". The source code of the Android system. However, we will not discuss this issue for the time being, after all, there is nothing bad about standing on the shoulders of giants and doing innovation.

Let our domestic developers embrace Hongmeng and contribute to the Hongmeng application ecology together!

———————— The end ————————

The code word is not easy. If you think this blog is written well, you can appreciate a cup of coffee~~
Insert picture description here


Guess you like

Origin blog.csdn.net/fukaimei/article/details/108549275