DEV Community

wei chang
wei chang

Posted on

A Detailed explanation of the mixed development of ArkTS and Cangjie in the HarmonyOS NEXT system

I have been sharing the development tutorials of the HarmonyOS Cangjie language for many consecutive days. Now, everyone should know that there are two development languages for HarmonyOS: ArkTs and Cangjie. These two languages are somewhat similar and also support mixed development. Today, I will share how to achieve mixed development of ArkTs and Cangjie. Friends who can't tell the difference between these two languages might be having a headache today.

The official documentation provides a very extensive and detailed introduction to the mixed development of these two languages. However, after learning it myself, You LAN Jun felt that it was not so troublesome because the compiler can do the vast majority of the work for us.

First, open DevEco Studio and create a new project. Here, select CangJie Hybrid to create a hybrid development project:

Image description

Take a look at the created structure directory:

Image description

It can be seen that there are two directories in the project: the "cangjie" directory and the "ArkTs" project directory. If you take a close look, you will find that the entry file of the project is in the "ArkTS" directory. That is to say, although the project was created using the "Cangjie Mixed Compilation Project", in fact, the Cangjie code is attached to the "ArkTs" project. Therefore, during the operation of the hybrid development project, the page controller and the main thread are mostly unrelated to Cangjie. Apart from the back-end functions, the part Cangjie is mainly responsible for is the development of components.

To put it more simply, in a mixed programming project, ArkTS 'pages can embed part of Cangjie's UI, but Cangjie cannot create separate pages. So in a mixed project, the page lifecycle and route redirection have nothing to do with Cangjie either.

The detailed development process is shared below.

The newly created project demonstrates to us how ArkTs calls the methods in Cangjie. Now, let's take a look at how ArkTs references the components of Cangjie.

First, right-click in the cangjie folder to create a blank cangjie page. After the creation is completed, a new.cj file is added under the cangjie folder. Not only that, a new.ets file with the same name is added in the Arkts directory:

Image description

This is the compiler automatically completing the encapsulation work for us. Now, if we want to jump to the Cangjie page, we just need to jump to the ArkTs page with the same name.

Image description

Take a look at the code of this automatically created page. It creates an ArkTS page and introduces the encapsulated components developed by Cangjie.

So for hybrid development, all we need to do is write "Cangjie" in the Cangjie file and "ArkTS" in the ArkTS file. The compiler does all the other work for us. Harmonyos is really, really, really friendly to developers.

Finally, I have some thoughts. Youlan Jun has been in the industry for ten years and is familiar with multiple development languages. Some are object-oriented, some are procedural, and some focus on performance. ArkTs and Cangjie of HarmonyOS are the only two development languages that are designed with developers in mind, which deeply moves the veteran programmers who often write code behind the scenes of software.

That's all for today. Thank you all for reading.

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.