4
头图

Preface

A few days ago, the official Weibo of Huawei EMUI officially announced that it was renamed HarmonyOS.

EMUI更名为HarmonyOS

Doesn't this mean that in the future, Huawei mobile phones will all be Hongmeng systems? Hongmeng also issued a video showing that on June 2, 2021, Hongmeng operating system and Huawei's full-scene new product launch conference will be opened. It is expected that the models that now support the EMUI11 upgrade will all support the upgrade of the Hongmeng system. I took a look, isn’t this the one I’m going to push tomorrow, hurry up and learn.

I have been paying attention to the Hongmeng system before, but I feel that it should not be a large area push system soon. It wasn't until I saw this news that I felt it was time to learn about the Hongmeng system.

Learning Hongmeng system is very simple. Currently, Hongmeng has a lot of documents, developer communities, and learning videos. My main learning method is to follow the official website to read the document. Then the document did not understand the concept of knowledge points to sort out.

System positioning

系统定位

There is a paragraph about Hongmeng's system positioning in the document. Seeing this text introduction, you can summarize the three characteristics of Hongmeng system:

  1. Distributed: Hongmeng can adapt to multiple terminals, including mobile phones, tablets, smart home appliances, smart screens, and vehicle-mounted devices. From this positioning, it means that the Hongmeng system is different from the Android system from the beginning of its design. The most important thing is that in the face of different equipment terminals, only one system needs to be developed instead of multiple developments. That is, Hongmeng has realized the ability of one-time development and multi-terminal deployment.
  2. Easy to develop: Hongmeng system provides a variety of development language API, including Java, C, JS. That is, front-end and back-end developers can quickly learn Hongmeng. I think that when Hongmeng system architects design Hongmeng, they also hope that as many developers as possible can learn and master Hongmeng.
  3. Componentization: Hongmeng adopts a componentization design scheme, which can realize the requirements of different terminal devices for the operating system. In other words, different devices have different requirements for the operating system. For example, some smart home appliances do not require a screen display module, and home appliances using the Hongmeng system will freely combine system modules according to actual needs. One of the benefits brought by this is to reduce the size of the smart home appliance system and achieve flexible business tailoring.

From this study, I saw that Hongmeng's vision is very ambitious. Not only can it replace the Android system, but it can also connect all smart devices together to realize the Internet of Everything.

Technology Architecture

鸿蒙技术架构图

I personally feel that if you want to learn Hongmeng, it should be enough based on this technical architecture diagram. All the remaining relevant knowledge is nothing more than expansion and supplementation based on this technical architecture diagram. So this official technical architecture diagram is the top priority.

Hongmeng system is divided into kernel layer, system service layer, architecture layer and application layer from bottom to top. At first glance, these four layers are very similar to the architecture diagram of the Android system next door, but they are completely different. Hongmeng system functions can tailor some non-essential system functions and modules according to actual needs.

  • Kernel layer: The kernel layer selects suitable OS kernels for different resource-constrained devices. To put it in a nutshell, this layer provides a unified external access API. Because Hongmeng needs to face many equipment terminals, such as Linux and LiteOS. Therefore, this layer uniformly encapsulates the differences of various contents, and the operation API interface exposed to the outside is uniform. I guess it is because of this layer and other packaging that Hongmeng can be perfectly compatible with the Android system. I don’t know if Hongmeng can develop Apple applications, so I’ll learn to see later.
  • System service layer: the core competence layer of the entire Hongmeng system. That includes multi-device operation, security, AI, telephony, location services, wearable devices, etc., which are all tasks at this level. I am more interested in that it provides the Ark runtime. Because I haven't learned about Ark, I will sort it out separately later.
  • Framework layer: Provides two sets of UI frameworks, Java UI framework and JS UI framework. In other words, both JAVA programmers and JS programmers can get started with the UI interface.
  • Application layer: A Hongmeng application has multiple FA or PA. FA has a UI interface to provide user interaction capabilities; PA provides background tasks and data access. The advantage of this is that according to actual business needs, FA and PA can be freely combined to achieve cross-terminal equipment.

Ark Compiler

When reading the document, the system service layer provides the Ark multi-language runtime subsystem. The following sentence was found:

The Ark runtime provides C/C++/JS multi-language runtime and basic system class libraries. It also provides runtime for the static Java program (that is, the part of the application or framework layer developed in the Java language) that uses the Ark compiler. .

According to my understanding, this should be the key to Hongmeng's multi-language development. Then I found a article from the Internet, and instantly understood what the Ark compiler did.

Regardless of the early electronic tubes or the current semiconductor transistors, the basic unit of these calculations can only identify the level of level. In the computer, we use 0 and 1 to represent it. The various combinations of 1 and 0 are the language that the machine can understand, that is, the machine code. In the early days of computers, people used 0 and 1 to write program commands. This kind of anti-human coding is obviously hated by programmers. So with assembly language, characters are used to represent command codes. This is a lot more convenient, but large The program is still not feasible, and high-level languages such as C\C++, JAVA and so on have emerged. It can be said that high-level languages are designed for programmers and large-scale software projects. Therefore, the inheritance, polymorphism, callback, reflection and other mechanisms of object-oriented programming are gradually born. No matter how the high-level language changes, the processor can recognize It's just machine code, and the compiler assumes the role of translation, which is what the Ark compiler has to do.

One of the reasons why the current mobile phone system iOS is smoother than the Android system is because iOS does not have a virtual machine like Android. Although the reason why Java can be cross-platform is because there is a virtual machine, but on the mobile operating system, the virtual machine has dragged down Android. Although the bytecode packaged into the virtual machine can be done irrespective of the machine hardware, when the program is executed, it still needs to be compiled into machine code to be executed again. That is, a program developed in Java needs to be compiled twice before it can be executed, in contrast to iOS, it only needs to be executed once. Because iOS has an Xcode tool that integrates swiftc compilation commands.

One of the reasons why Hongmeng is smoother than Android is because there is no virtual machine. It uses the same mechanism as iOS. When generating the installation package, it is directly compiled into appropriate machine code.

方舟编译器编译机器指令

From the above figure, it can be found that the Android application needs to be statically compiled before running, and then in the ART virtual machine, the parser needs to be executed to parse the code. After parsing verbatim, JIT is also required. This creates additional overhead. On the other hand, the Ark compiler has been statically compiled its executable files during the development stage, and the rest only needs to be installed and run.

In addition to removing the virtual machine, the Ark compiler also implements multi-language joint development. Nowadays, developing an APP rarely uses only one language. When developing an application, libraries developed in various other languages, such as C language, are used. Different languages also need to communicate and call. In the Android lineup, different languages are used in separate environments, and then intermediate code is compiled into machine code for execution. And when the program is running. This will generate additional system overhead.

The Ark compiler uses a set of executable files that are compiled into a set of executable files in different languages in the development environment, and then executed in the runtime environment.

To sum up, the Ark Compiler aims at the existing Android problems, and the solution is to solve them in the development stage.

  • Remove the virtual machine and statically compile the executable file during the development phase.
  • Remove JNI, provide a unified program in the development phase, and then generate an executable file.

I think that through this strategy, the mobile phone system APP is definitely faster and smoother for users, because all the overhead is almost solved in the development stage, and the program runs naturally faster. This reminds me of Xcode. Now just downloading Xcode 12.5 requires 11.7G of storage space, and Xcode on my computer occupies 25G of storage space.

Basic knowledge of application

Before you start learning, learn about the basics of HarmonyOS applications. The HarmonyOS application package consists of HAP (HarmonyOS App Package) and the description file pack.info. A HAP is composed of code, resources, third-party libraries, and configuration files.

App逻辑图

The logic diagram of an App Pack is as shown above. This diagram contains a lot of information:

  • entry: Application main module, there is only one. Which is the yellow module in the picture
  • feature: refers to the dynamic module. There can be more than one, or none at all. The blue module on the way
  • Each App has a file used to describe HAP attributes, pack.info, in the green part of the picture.
  • Each individual hap module will have a configuration file, config.json, used to configure the module. It is used to declare Ablitiya.
  • The third-party dependent code of the application, such as so, jar, etc., is stored in libs.
  • Resource files, including pictures, audio, etc., are stored in resources.
  • Ability is a package of application capabilities, which is composed of FA with UI interface and PA without UI interface.

HarmonyOS application development foundation

Huawei Developer Academy has a talent plan, which includes a course HarmonyOS Application Development Series (Basic) . Through the study of this course, developers can have the ability to use HarmonyOS to develop applications smoothly.

The next step is to study this course, and at the same time combine the official website documents for supplementary learning.

First, create a small application that can realize page jump Quick Start given in the official document.

Development environment setup

My development environment is MacOS, just download and install DevEco Studio directly. At the same time, DevEco Studio needs to log in to the Huawei developer account, because subsequent development needs to use the remote simulator, so you must have a Huawei developer account.

After the installation is complete, you need to configure the development environment. According to the official method of configuring the development environment, the latest version of the HarmonyOS SDK installation package is automatically downloaded and installed. The SDK of Java language is used by default. Because I have not yet started to learn how to develop with JS, I will not install the SDK package of the JS language. The current version is 2.1.1.20, which is a bit more recent than the document above. This version is the latest version released on April 30, 2021. And you can find that my API Version is already 5. When creating a project later, the version that needs to be selected should also correspond to it.

HarmonyOS SDK

Run hello world

The old rules and the new environment will create a hello world, indicating whether the environment is successfully built.

When creating the project, I chose API Version 5 because I wanted to experience the latest API.

API Version 5

To use the emulator, you must log in to the Huawei developer account.

登录华为开发者账号

After logging in to the account, select the P40 model as the emulator, and then run the code into the emulator. There are some discrepancies between the results of my operation and the pictures given above in the document. The "Hello World" text in my interface is not centered in the vertical direction, but is frozen at the top. But it also shows that the development environment is set up.

hello world运行结果

Create UI layout with XML

The Java UI framework is provided in HarmonyOS, and the Java UI framework provides two layout methods: XML layout and code layout. Will HarmonyOS provide xml layout?

I think this may be that the architects need to attract some Android developers to join the development of the Hongmeng system. Provide a variety of different development methods to help the Hongmeng system develop and grow.

The document does not say how to create a Button or Text, but directly gives a piece of code. However, the document shows that the DependentLayout layout is used, and the connection addresses of the Text and Button components are given.

I created the project with DevEcoStudio, and opened the ability_main.xml file as prompted by the document. The code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:orientation="vertical">

    <Text
        ohos:id="$+id:text_helloworld"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="$string:mainability_HelloWorld"
        ohos:text_size="50px"
        />

</DirectionalLayout>

Comparing the code given in the document, I found that the layout is different. I used the DirectionalLayout layout here, and the document used the DependentLayout layout.

After following the steps given in the document, I found another problem. Every time I run the simulator, I need to reinstall the application and rerun the application. It seems that there is no hot update like Flutter.

Later, it was discovered that using the previewer mentioned in the document can solve the problem of real-time preview interface effects.

Hello World加按钮

Finally, the code I adjusted is as follows:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:orientation="vertical">

    <Text
        ohos:id="$+id:text_helloworld"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="$string:mainability_HelloWorld"
        ohos:text_size="50px"
        />
    <Button
        ohos:id="$+id:button"
        ohos:width="match_content"
        ohos:height="match_content"
        ohos:text="Next"
        ohos:text_size="19fp"
        ohos:text_color="#FFFFFF"
        ohos:top_padding="8vp"
        ohos:bottom_padding="8vp"
        ohos:right_padding="70vp"
        ohos:left_padding="70vp"
        ohos:center_in_parent="true"
        ohos:layout_alignment="horizontal_center"
        ohos:below="$id:text_helloworld"
        ohos:margin="10vp"
        ohos:background_element="$graphic:background_button"/>
</DirectionalLayout>

Because it is a copy of the official document code, but the places that need to be adjusted are:

  • The below attribute of the Button should be the id attribute of Text. I checked the manual, but I haven't found what this attribute is for.
  • After I copied the code, I found that the button was not centered horizontally, and added a line of ohos:layout_alignment="horizontal_center"
  • From the running result graph, the text and buttons are not centered in the vertical direction. It shows that some properties of the component have not been adjusted, and it seems that you have to adjust it in the future.

Create UI layout with code

To create a UI layout with code is to write the layout code in a Java file. Just copy the code given in the document.

Hi there运行图

The effect of this run is consistent with the document above. Then there are a few points to note

  • The copied Java code uses the DependentLayout layout method. This should be the reason why the first page is not vertically centered.
  • The java code also supports the real-time preview function. In the java code, just run the preview directly.

So far, the Quick Start manual on the document has run successfully. Although the code is all copies, I don't know the concept. So further study is needed.

Get the certificate

After mastering the above basic knowledge, it is very simple to study the courses given by Huawei. Carefully watch the video introduction given on the official website, then listen to the Q&A at the end of each class, and finally do the assessment of each class. After everything is completed, you only need to do a graduation exam question .

初级证书

This is my first online learning course with Huawei certification, and today happens to be June 1 Children’s Day. I hope I will still be full of childlike innocence every day to check and learn ✌️✌️✌️.

This certificate actually proves that you have a basic understanding of HarmonyOS. There is still a lot of distance from the ability to actually develop HarmonyOS applications, and we need to continue to work hard in the future💪💪💪.


siberiawolf
2.5k 声望853 粉丝

每天多学一点,幸福多攒一点。