Method and apparatus for generating code

By obtaining and analyzing the code of the first operating system and using the code correspondence relationship to generate replacement code of the second operating system, the problem of inefficient development of Hongmeng application is solved and efficient code conversion is achieved.

CN113900643BActive Publication Date: 2025-07-15BEIJING WODONG TIANJUN INFORMATION TECH CO LTD +1
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202111191097.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-10-13
Publication Date
2025-07-15
Estimated Expiration
2041-10-13

AI Technical Summary

Technical Problem

When developing Hongmeng application code, the development efficiency was inefficient due to lack of understanding of the functions of the Hongmeng system API and support library.

Method used

By obtaining the application code in the first operating system, based on the code correspondence between the first operating system and the second operating system, the replacement code in the second operating system is determined, and the corresponding code is generated.

Benefits of technology

Improves code development efficiency and simplifies the code conversion process from Android applications to Hongmeng applications.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113900643B_ABST
    Figure CN113900643B_ABST
Patent Text Reader

Abstract

This application discloses a method and apparatus for generating code. A specific embodiment of the method includes: obtaining the code of a first application in a first operating system; determining replacement code in a second operating system corresponding to at least part of the code of the first application based on the code correspondence between the first operating system and the second operating system; and generating, according to the replacement code, the code of a second application in the second operating system corresponding to the first application. This application provides a method for generating the code of a second application in a second operating system corresponding to a first application based on the code of the first application in the first operating system, improving the code development efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of the present application relate to the field of computer technology, and particularly to a method and device for generating code. Background Art

[0002] The main programming language for Android application code and HarmonyOS application code is both Java, and the development tools are both the community version of the open-source IntelliJ IDEA (an integrated development environment for Java programming language development). The project structures of the Android platform and the HarmonyOS platform are similar. Currently, as a new operating system, although the development language of applications in the HarmonyOS is the same as that in the Android system and the APIs (Application Programming Interfaces) are similar, when developers start to develop HarmonyOS application code, due to the lack of understanding of the functions provided by the HarmonyOS APIs and support libraries, they need to spend a lot of effort in the process of developing HarmonyOS applications corresponding to Android applications, resulting in low development efficiency. Summary of the Invention

[0003] The embodiments of the present application propose a method and device for generating code.

[0004] In a first aspect, the embodiments of the present application provide a method for generating code, including: obtaining the code of a first application in a first operating system; determining replacement code in a second operating system corresponding to at least part of the code of the first application based on the code correspondence between the first operating system and the second operating system; and generating the code of a second application in the second operating system corresponding to the first application according to the replacement code.

[0005] In some embodiments, the above determining replacement code in the second operating system corresponding to at least part of the code of the first application based on the code correspondence between the first operating system and the second operating system includes: determining whether the code of the first application includes code to be replaced according to a first code feature model library of the first operating system established in advance; in response to determining that it includes, for each part of the code to be replaced included in the code of the first application, determining the replacement code of this part of the code to be replaced from a second code feature model library of the second operating system.

[0006] In some embodiments, the above for each part of the code to be replaced included in the code of the first application, determining the replacement code of this part of the code to be replaced from the second code feature model library of the second operating system includes: for each part of the code to be replaced included in the code of the first application, based on a tree matching algorithm, determining the matching degree between the feature model tree of this part of the code to be replaced and the feature model tree of the code feature model in the second code feature model library, and determining the replacement code of this part of the code to be replaced from the second code feature model library according to the matching degree.

[0007] In some embodiments, the code to be replaced includes the function code to be replaced representing the function information of the first application and the layout code to be replaced representing the layout information of the first application; and for each part of the code to be replaced included in the code of the first application, based on the tree matching algorithm, the matching degree between the feature model tree of this part of the code to be replaced and the feature model tree of the code feature model in the second code feature model library is determined, and according to the matching degree, the replacement code for this part of the code to be replaced is determined from the second code feature model library, including: for each part of the function code to be replaced included in the code of the first application, based on the tree matching algorithm, the matching degree between the feature model tree of this part of the function code to be replaced and the feature model tree of the function code feature model in the second code feature model library is determined, and according to the matching degree between the feature model tree of this part of the function code to be replaced and the feature model tree of the function code feature model in the second code feature model library, the replacement function code for this part of the function code to be replaced is determined from the second code feature model library; for each part of the layout code to be replaced included in the code of the first application, based on the tree matching algorithm, the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the second code feature model library is determined, and according to the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the second code feature model library, the replacement layout code for this part of the layout code to be replaced is determined from the second code feature model library.

[0008] In some embodiments, for each part of the layout code to be replaced included in the code of the first application, based on the tree matching algorithm, the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the second code feature model library is determined, and according to the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the second code feature model library, the replacement layout code for this part of the layout code to be replaced is determined from the second code feature model library, including: using the depth - first search algorithm, starting from the root view node in the first application, for the layout code to be replaced corresponding to each view node, based on the tree matching algorithm, the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the second code feature model library is determined, and according to the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the second code feature model library, the replacement layout code for this part of the layout code to be replaced is determined from the second code feature model library.

[0009] In some embodiments, the above method further includes: establishing a first code feature model library for the first operating system; establishing a second code feature model library for the second operating system.

[0010] In some embodiments, the first operating system is the Android system and the second operating system is the HarmonyOS system.

[0011] In a second aspect, an embodiment of the present application provides a device for generating code, including: an acquisition unit configured to acquire the code of a first application in a first operating system; a determination unit configured to determine, based on the code correspondence between the first operating system and the second operating system, replacement code for at least part of the code of the first application in the second operating system; a generation unit configured to generate, according to the replacement code, the code of a second application in the second operating system corresponding to the first application.

[0012] In some embodiments, the determination unit is further configured to: determine whether the code of the first application includes code to be replaced according to the pre-established first code feature model library of the first operating system; in response to determining that it includes, for each part of the code to be replaced included in the code of the first application, determine the replacement code for this part of the code to be replaced from the second code feature model library of the second operating system.

[0013] In some embodiments, the determination unit is further configured to: for each part of the code to be replaced included in the code of the first application, determine the matching degree between the feature model tree of this part of the code to be replaced and the feature model tree of the code features in the second code feature model library based on a tree matching algorithm, and determine the replacement code for this part of the code to be replaced from the second code feature model library according to the matching degree.

[0014] In some embodiments, the code to be replaced includes the function code to be replaced characterizing the function information of the first application and the layout code to be replaced characterizing the layout information of the first application; and a determination unit, further configured to: for each part of the function code to be replaced included in the code of the first application, based on the tree matching algorithm, determine the matching degree between the feature model tree of this part of the function code to be replaced and the feature model tree of the function code feature model in the second code feature model library, and according to the matching degree between the feature model tree of this part of the function code to be replaced and the feature model tree of the function code feature model in the second code feature model library, determine the replacement function code of this part of the function code to be replaced from the second code feature model library; for each part of the layout code to be replaced included in the code of the first application, based on the tree matching algorithm, determine the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the second code feature model library, and according to the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the second code feature model library, determine the replacement layout code of this part of the layout code to be replaced from the second code feature model library.

[0015] In some embodiments, the determination unit is further configured to: starting from the root view node in the first application, using the depth - first search algorithm, for the layout code to be replaced corresponding to each view node, based on the tree matching algorithm, determine the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the second code feature model library, and according to the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the second code feature model library, determine the replacement layout code of this part of the layout code to be replaced from the second code feature model library.

[0016] In some embodiments, the above - mentioned device further includes: a first establishment unit, configured to establish a first code feature model library for the first operating system; a second establishment unit, configured to establish a second code feature model library for the second operating system.

[0017] In some embodiments, the first operating system is the Android system, and the second operating system is the HarmonyOS system.

[0018] In a third aspect, an embodiment of the present application provides a computer - readable medium, on which a computer program is stored, where when the program is executed by a processor, the method described in any implementation manner of the first aspect is implemented.

[0019] Fourthly, an embodiment of the present application provides an electronic device, including: one or more processors; a storage device storing one or more programs thereon, and when the one or more programs are executed by the one or more processors, the one or more processors implement the method described in any implementation manner of the first aspect.

[0020] The method and device for generating code provided by the embodiments of the present application obtain the code of a first application in a first operating system; determine replacement code in a second operating system corresponding to at least part of the code of the first application based on the code correspondence between the first operating system and the second operating system; generate the code of a second application in the second operating system corresponding to the first application according to the replacement code, thereby providing a method for generating the code of a second application in the second operating system corresponding to the first application based on the code of the first application in the first operating system, and improving the code development efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] Other features, objects, and advantages of the present application will become more apparent by reading the detailed description of the non-limiting embodiments with reference to the following drawings:

[0022] Figure 1 is an exemplary system architecture diagram to which an embodiment of the present application can be applied;

[0023] Figure 2 is a flowchart of an embodiment of the method for generating code according to the present application;

[0024] Figure 3 is a schematic diagram of an application scenario of the method for generating code according to this embodiment;

[0025] Figure 4 is a flowchart of another embodiment of the method for generating code according to the present application;

[0026] Figure 5 is a structural diagram of an embodiment of the device for generating code according to the present application;

[0027] Figure 6 is a schematic diagram of the structure of a computer system suitable for implementing the embodiments of the present application. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0028] The present application will be further described in detail below with reference to the drawings and embodiments. It can be understood that the specific embodiments described herein are only used to explain the related invention and do not limit the invention. Additionally, it should be noted that only parts related to the related invention are shown in the drawings for the convenience of description.

[0029] It should be noted that, without conflict, the embodiments in this application and the features in the embodiments can be combined with each other. The following will describe this application in detail with reference to the drawings and in combination with the embodiments.

[0030] Figure 1 FIG. 100 shows an exemplary architecture where the method and apparatus for generating code according to this application can be applied.

[0031] As Figure 1 shown, the system architecture 100 may include terminal devices 101, 102, 103, a network 104, and a server 105. The terminal devices 101, 102, 103 are communicatively connected to form a topology network, and the network 104 is used as a medium to provide a communication link between the terminal devices 101, 102, 103 and the server 105. The network 104 may include various connection types, such as wired, wireless communication links, or fiber optic cables, etc.

[0032] The terminal devices 101, 102, 103 may be hardware devices or software that support network connections for data interaction and data processing. When the terminal devices 101, 102, 103 are hardware, they may be various electronic devices that support network connections, information acquisition, interaction, display, processing, etc., including but not limited to smartphones, tablets, e-book readers, laptop portable computers, and desktop computers, etc. When the terminal devices 101, 102, 103 are software, they can be installed in the above-listed electronic devices. It can be implemented as, for example, multiple software or software modules for providing distributed services, or can also be implemented as a single software or software module. No specific limitation is made here.

[0033] The server 105 may be a server that provides various services. For example, it is a background processing server that obtains a code development request sent by the user through the terminal devices 101, 102, 103 and generates the code of the second application in the second operating system corresponding to the first application based on the code of the first application of the first operating system. As an example, the server 105 may be a cloud server.

[0034] It should be noted that the server can be hardware or software. When the server is hardware, it can be implemented as a distributed server cluster composed of multiple servers, or can also be implemented as a single server. When the server is software, it can be implemented as multiple software or software modules (such as software or software modules for providing distributed services), or can also be implemented as a single software or software module. No specific limitation is made here.

[0035] It should also be noted that the method for generating code provided in the embodiments of the present application can be executed by a server, or by a terminal device, or by the server and the terminal device cooperating with each other. Correspondingly, each part (such as each unit) included in the device for generating code can be all arranged in the server, or all arranged in the terminal device, or respectively arranged in the server and the terminal device.

[0036] It should be understood that Figure 1 the numbers of terminal devices, networks, and servers in

[0037] Continuing to refer to Figure 2 , a flowchart 200 of an embodiment of the method for generating code is shown, including the following steps:

[0038] Step 201, obtain the code of the first application in the first operating system.

[0039] In this embodiment, the execution subject of the method for generating code (such as Figure 1 the server in

[0040] ) can obtain the code of the first application in the first operating system from a remote location or locally through a wired network connection or a wireless network connection.

[0041] The first operating system can be any operating system, including but not limited to the Android operating system, the Symbian operating system, the Apple mobile phone operating system, the Windows mobile phone operating system, and the Firefox operating system. The first application can be an application that provides various services in the first operating system, including but not limited to social applications, life service applications, office applications, shopping applications, financial applications, entertainment applications, and camera applications.

[0042] Code is a source file written by a programmer in a language supported by a development tool, including function code for implementing the functions of each control in the application and layout code for implementing the layout of the controls in the application.

[0043] Step 202, based on the code correspondence relationship between the first operating system and the second operating system, determine replacement code in the second operating system that corresponds to at least part of the code of the first application.

[0043] In this embodiment, the above-mentioned execution entity may determine replacement code for at least part of the code corresponding to the first application in the second operating system based on the code correspondence between the first operating system and the second operating system.

[0044] The second operating system may be any operating system different from the first operating system. As an example, when the first operating system is the Apple mobile phone operating system, the second operating system may be the Android operating system.

[0045] The code correspondence represents the correspondence between the codes in the first operating system and the second operating system that implement the same or similar functions and complete the same or similar operations. Among them, the function may be any function of the first operating system and the second operating system, and the operation may be any operation of the first operating system and the second operating system.

[0046] As an example, the above-mentioned execution entity may determine the operation for determining the code correspondence according to the received correspondence determination operation, and determine the code correspondence between the first operating system and the second operating system.

[0047] As another example, for the code provided by the first operating system, the above-mentioned execution entity divides it into partial codes corresponding to each functional unit in units of functional units; for the code provided by the second operating system, the above-mentioned execution entity also divides it into partial codes corresponding to each functional unit in units of functional units. Among them, the functional unit is the smallest function obtained by subdividing the functions provided by the operating system. For the partial codes corresponding to each functional unit of the first operating system and the partial codes corresponding to each functional unit of the second operating system, the above-mentioned execution entity determines the matching degree between each pair, and further determines the code correspondence between the first operating system and the second operating system based on the matching degree. Specifically, when the matching degree between the partial code corresponding to the functional unit of the first operating system and the partial code corresponding to the functional unit of the second operating system is greater than the preset matching degree threshold, it can be determined that they correspond to each other. Among them, the preset matching degree threshold can be specifically set according to the actual situation and is not limited here.

[0048] In some optional implementation manners of this embodiment, the above-mentioned execution entity may establish a first code feature model library for the first operating system; and establish a second code feature model library for the second operating system.

[0049] As an example, for the support library of the first operating system, the above-mentioned execution entity may generate implementation examples of common functions in the support library, and establish a first code feature model library with the implementation examples as code feature models. Similarly, for the support library of the second operating system, the above-mentioned execution entity may generate implementation examples of common functions in the support library, and establish a second code feature model library with the implementation examples as code feature models.

[0050] In some alternative implementation manners of this embodiment, the above-mentioned execution entity may execute the above step 202 in the following manner:

[0051] First, according to the first code feature model library of the first operating system established in advance, determine whether the code of the first application includes the code to be replaced.

[0052] As an example, the above-mentioned execution entity scans the code of the first application, locates each method in each file in the application, and if the method name is the same as the method name of a certain code feature model in the first code feature model library, it is determined that the code to be replaced is included.

[0053] Second, in response to determining that it is included, for each part of the code to be replaced included in the code of the first application, determine the replacement code for this part of the code to be replaced from the second code feature model library of the second operating system.

[0054] As an example, the above-mentioned execution entity may determine the matching degree between each part of the code to be replaced included in the code of the first application and each code feature model in the second code feature model library, and determine the code represented by the code feature model that matches this part of the code to be replaced as the replacement code for this part of the code to be replaced.

[0055] In some alternative implementation manners of this embodiment, the above-mentioned execution entity may execute the above second step in the following manner:

[0056] For each part of the code to be replaced included in the code of the first application, based on the tree matching algorithm, determine the matching degree between the feature model tree of this part of the code to be replaced and the feature model tree of the code feature model in the second code feature model library, and according to the matching degree between the feature model tree of this part of the code to be replaced and the feature model tree of the code feature model in the second code feature model library, determine the replacement code for this part of the code to be replaced from the second code feature model library.

[0057] In this implementation manner, based on the matching degree between the two feature model trees, determine the matching degree between the codes corresponding to the two feature model trees. As an example, use the simple tree matching algorithm to calculate the maximum number of matching nodes between the two feature model trees, so as to obtain the similarity between the codes, that is, the matching degree between the feature model tree of this part of the code to be replaced and the feature model tree of the code feature model in the second code feature model library. Specifically, the above-mentioned execution entity may determine the code feature model with the highest matching degree between the feature model tree in the second code feature model library and the feature model tree of this part of the code to be replaced as the replacement code for this part of the code to be replaced.

[0058] In some alternative implementation manners of this embodiment, the code to be replaced includes the function code to be replaced representing the function information of the first application and the layout code to be replaced representing the layout information of the first application. The above execution entity can, for each part of the function code to be replaced included in the code of the first application, based on the tree matching algorithm, determine the matching degree between the feature model tree of this part of the code to be replaced and the feature model tree of the code feature model in the second code feature model library, and according to the matching degree between the feature model tree of this part of the function code to be replaced and the feature model tree of the function code feature model in the second code feature model library, determine the replacement function code of this part of the function code to be replaced from the second code feature model library; for each part of the layout code to be replaced included in the code of the first application, based on the tree matching algorithm, determine the matching degree between the feature model tree of this part of the function code to be replaced and the feature model tree of the function code feature model in the second code feature model library, and according to the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the second code feature model library, determine the replacement layout code of this part of the layout code to be replaced from the second code feature model library.

[0059] In this implementation manner, when establishing the first code feature model library and the second code feature model library, the function code and the layout code can be divided respectively to establish the function code feature model sub-library and the layout code feature model sub-library in the first code feature model library, as well as the function code feature model sub-library and the layout code feature model sub-library in the second code feature model library.

[0060] For each method included in the code of the first application, when the method name is the same as the method name of a certain code feature model in the function code feature model sub-library of the first code feature model library, it is determined that the function code to be replaced is included. Furthermore, according to the matching degree between the feature model tree of this part of the function code to be replaced and the feature model tree of the function code feature model in the function code feature model sub-library of the second code feature model library, the replacement function code of this part of the function code to be replaced is determined from the function code feature model sub-library of the second code feature model library. Similarly, when the method name is the same as the method name of a certain code feature model in the layout code feature model sub-library of the first code feature model library, it is determined that the layout code to be replaced is included. Furthermore, according to the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the layout code feature model sub-library of the second code feature model library, the replacement layout code of this part of the function code to be replaced is determined from the layout code feature model sub-library of the second code feature model library.

[0061] In some alternative implementation manners of this embodiment, for the layout code to be replaced, the above-mentioned execution entity may adopt a depth-first search algorithm. Starting from the root view node in the first application, for the layout code to be replaced corresponding to each view node, based on a tree matching algorithm, the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the second code feature model library is determined. And according to the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the second code feature model library, the replacement layout code for this part of the layout code to be replaced is determined from the second code feature model library.

[0062] Specifically, in the layout resource file of the first application, each control node is traversed. The depth-first search algorithm is adopted and starting from the root view node, each child node of this starting point is traversed in sequence. When traversing the child nodes, this child node is also used as the starting point to traverse each leaf node of this starting point until all control nodes are traversed. For the attribute methods of each control node (such as layout methods representing attributes such as the size and position of the control), the replacement layout code for the layout code to be replaced representing the attribute methods of this control node is determined from the second code feature model library.

[0063] Step 203, generate the code of the second application in the second operating system corresponding to the first application according to the replacement code.

[0064] In this embodiment, the above-mentioned execution entity may generate the code of the second application corresponding to the first application in the second operating system according to the replacement code.

[0065] As an example, when the replacement codes for all the codes in the first application are found in the second code feature model library of the second operating system, the above-mentioned execution entity may integrate all the replacement codes to generate the code of the second application corresponding to the first application.

[0066] As another example, when the replacement codes for some codes in the first application are found in the second code feature model library of the second operating system, the above-mentioned execution entity may integrate the replacement codes for some codes in the first application and combine them with the development codes of the part of the codes in the first application for which the codes to be replaced have not been determined by the developer to generate the code of the second application corresponding to the first application.

[0067] For the case where the replacement function codes and replacement layout codes of the codes in the first application are determined, the above-mentioned execution entity may combine the replacement function codes and replacement layout codes to generate the code of the second application corresponding to the first application.

[0068] In some alternative implementation manners of this embodiment, the first operating system is the Android system, and the second operating system is the HarmonyOS system.

[0069] In practice, a plugin for implementing the method shown in the above steps 201-203 can be developed in the HarmonyOS application development tool devEco studio, so as to execute the above steps 201-203 through the plugin, and generate the code of the second application in the second operating system corresponding to the first application based on the code of the first application in the first operating system.

[0070] Specifically, the above plugin development process needs to rely on IntelliJ IDEA. Open IntelliJ IDEA, and successively select File—>New—>Project in the interface, then select the plugin project, create a menu, and describe the functions of the plugin to be generated as in steps 201-203 to generate the plugin. After the development of the plugin is completed, the following plugin deployment operations are performed: Based on the plugin.xml file generated during the plugin generation process, successively select Build->Prepare Plugin Module in the interface of IntelliJ IDEA to generate a jar package in the project root directory; open devEco studio, and successively select File->Setting->Plugins->Install plugin from disk in the interface, and then select the just-generated jar package to install it into devEco studio. After the installation is successful, restart to complete the deployment of the plugin.

[0071] Continue to refer to Figure 3 , Figure 3 FIG. 300 is a schematic diagram of an application scenario of the method for generating code according to this embodiment. In Figure 3 the application scenario, the user 301 sends a code development request to the server 303 through the terminal device 302. Among them, the code development request represents generating the code of the second application in the second operating system corresponding to the first application based on the code of the first application in the first operating system. After receiving the code development request, the server first obtains the code of the first application in the first operating system. Then, based on the code correspondence relationship between the first operating system and the second operating system, it determines the replacement code of at least part of the code corresponding to the first application in the second operating system. The code correspondence relationship represents the correspondence relationship between the codes in the first operating system and the second operating system that implement the same or similar functions and complete the same or similar operations. Finally, according to the replacement code, it generates the code of the second application in the second operating system corresponding to the first application.

[0072] The method provided by the above embodiments of the present application obtains the code of a first application in a first operating system; determines replacement code in a second operating system corresponding to at least part of the code of the first application based on the code correspondence between the first operating system and the second operating system; and in the second operating system, generates the code of a second application corresponding to the first application according to the replacement code, thereby providing a method for generating the code of a second application in a second operating system corresponding to a first application based on the code of the first application in the first operating system, improving the code development efficiency.

[0073] Continuing to refer Figure 4 , a schematic flowchart 400 of an embodiment of a method for generating code according to the present application is shown, including the following steps:

[0074] Step 401, establish a first code feature model library for the first operating system.

[0075] Step 402, establish a second code feature model library for the second operating system.

[0076] Step 403, obtain the code of a first application in the first operating system.

[0077] Step 404, according to the first code feature model library of the first operating system established in advance, determine whether the code of the first application includes code to be replaced.

[0078] The code to be replaced includes replacement function code representing the function information of the first application and replacement layout code representing the layout information of the first application.

[0079] Step 405, in response to determining that it includes, for each part of the replacement function code included in the code of the first application, based on the tree matching algorithm, determine the matching degree between the feature model tree of this part of the replacement function code and the feature model tree of the function code feature model in the second code feature model library, and according to the matching degree between the feature model tree of this part of the replacement function code and the feature model tree of the function code feature model in the second code feature model library, determine the replacement function code of this part of the replacement function code from the second code feature model library.

[0080] Step 406, for each part of the replacement layout code included in the code of the first application, based on the tree matching algorithm, determine the matching degree between the feature model tree of this part of the replacement layout code and the feature model tree of the layout code feature model in the second code feature model library, and according to the matching degree between the feature model tree of this part of the replacement layout code and the feature model tree of the layout code feature model in the second code feature model library, determine the replacement layout code of this part of the replacement layout code from the second code feature model library.

[0081] Step 407: Generate the code of the second application in the second operating system corresponding to the first application according to the replacement function code and the replacement layout code.

[0082] As can be seen from this embodiment, compared with Figure 2 the corresponding embodiment, the process 400 of the method for generating code in this embodiment specifically describes the determination process of the first code feature model library and the second code feature model library, the determination process of the replacement function code for the function code to be replaced, and the determination process of the replacement layout code for the layout code to be replaced, improving the accuracy and efficiency of the code development process.

[0083] Continuing to refer to Figure 5 , as an implementation of the methods shown in the above figures, an embodiment of an apparatus for generating code is provided in this application. This apparatus embodiment corresponds to Figure 2 the method embodiment shown, and this apparatus can be specifically applied to various electronic devices.

[0084] As shown in Figure 5 , the apparatus for generating code includes: an obtaining unit configured to obtain the code of the first application in the first operating system; a determining unit configured to determine, based on the code correspondence between the first operating system and the second operating system, the replacement code for at least part of the code corresponding to the first application in the second operating system; and a generating unit configured to generate the code of the second application in the second operating system corresponding to the first application according to the replacement code.

[0085] In some embodiments, the determining unit is further configured to: determine whether the code of the first application includes the code to be replaced according to the first code feature model library of the first operating system established in advance; and in response to determining that it includes, for each part of the code to be replaced included in the code of the first application, determine the replacement code for this part of the code to be replaced from the second code feature model library of the second operating system.

[0086] In some embodiments, the determining unit is further configured to: for each part of the code to be replaced included in the code of the first application, determine the matching degree between the feature model tree of this part of the code to be replaced and the feature model tree of the code feature model in the second code feature model library based on the tree matching algorithm, and determine the replacement code for this part of the code to be replaced from the second code feature model library according to the matching degree between the feature model tree of this part of the code to be replaced and the feature model tree of the code feature model in the second code feature model library.

[0087] In some embodiments, the code to be replaced includes replacement function code representing the function information of the first application and replacement layout code representing the layout information of the first application; and a determination unit, further configured to: for each part of the replacement function code included in the code of the first application, based on a tree matching algorithm, determine the matching degree between the feature model tree of this part of the replacement function code and the feature model tree of the function code feature model in the second code feature model library, and according to the matching degree between the feature model tree of this part of the replacement function code and the feature model tree of the function code feature model in the second code feature model library, determine the replacement function code of this part of the replacement function code from the second code feature model library; for each part of the replacement layout code included in the code of the first application, based on a tree matching algorithm, determine the matching degree between the feature model tree of this part of the replacement layout code and the feature model tree of the layout code feature model in the second code feature model library, and according to the matching degree between the feature model tree of this part of the replacement layout code and the feature model tree of the layout code feature model in the second code feature model library, determine the replacement layout code of this part of the replacement layout code from the second code feature model library.

[0088] In some embodiments, the determination unit is further configured to: starting from the root view node in the first application, using a depth-first search algorithm, for the replacement layout code corresponding to each view node, based on a tree matching algorithm, determine the matching degree between the feature model tree of this part of the replacement layout code and the feature model tree of the layout code feature model in the second code feature model library, and according to the matching degree between the feature model tree of this part of the replacement layout code and the feature model tree of the layout code feature model in the second code feature model library, determine the replacement layout code of this part of the replacement layout code from the second code feature model library.

[0089] In some embodiments, the above device further includes: a first establishment unit, configured to establish a first code feature model library for the first operating system; a second establishment unit, configured to establish a second code feature model library for the second operating system.

[0090] In some embodiments, the first operating system is the Android system, and the second operating system is the HarmonyOS system.

[0091] In this embodiment, an obtaining unit in a device for generating code obtains the code of a first application in a first operating system; a determining unit determines replacement code in a second operating system that corresponds to at least part of the code of the first application based on the code correspondence between the first operating system and the second operating system; a generating unit generates the code of a second application in the second operating system that corresponds to the first application according to the replacement code, thereby providing a device for generating the code of a second application in a second operating system that corresponds to a first application based on the code of the first application in the first operating system, and improving the code development efficiency.

[0092] Reference is made below to Figure 6 , which shows a schematic structural diagram of a computer system 600 suitable for use in implementing the devices (such as Figure 1 the devices 101, 102, 103, 105 shown) of the embodiments of the present application. Figure 6 The devices shown are merely examples and should not impose any limitation on the functions and scope of use of the embodiments of the present application.

[0093] As Figure 6 shown, the computer system 600 includes a processor (such as a CPU, a central processing unit) 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage section 608 into a random access memory (RAM) 603. In the RAM 603, various programs and data required for the operation of the system 600 are also stored. The processor 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.

[0094] The following components are connected to the I / O interface 605: an input section 606 including a keyboard, a mouse, etc.; an output section 607 including such as a cathode ray tube (CRT), a liquid crystal display (LCD), etc. and a speaker, etc.; a storage section 608 including a hard disk, etc.; and a communication section 609 including a network interface card such as a LAN card, a modem, etc. The communication section 609 performs communication processing via a network such as the Internet. A drive 610 is also connected to the I / O interface 605 as required. A removable medium 611, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 610 as required so that a computer program read from it can be installed into the storage section 608 as required.

[0095] In particular, according to an embodiment of the present application, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present application includes a computer program product that includes a computer program carried on a computer-readable medium, and the computer program contains program code for performing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 609, and / or installed from the removable medium 611. When the computer program is executed by the processor 601, the above functions defined in the method of the present application are performed.

[0096] It should be noted that the computer-readable medium of the present application can be a computer-readable signal medium, a computer-readable storage medium, or any combination of the two. The computer-readable storage medium can be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the above. More specific examples of the computer-readable storage medium can include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present application, the computer-readable storage medium can be any tangible medium that contains or stores a program, and the program can be used by or in combination with an instruction execution system, apparatus, or device. In the present application, the computer-readable signal medium can include a data signal propagated in a baseband or as part of a carrier wave, which carries the computer-readable program code. Such a propagated data signal can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. The computer-readable signal medium can also be any computer-readable medium other than the computer-readable storage medium, and the computer-readable medium can send, propagate, or transmit a program for use by or in combination with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium can be transmitted by any suitable medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.

[0097] Computer program code for performing the operations of this application can be written in one or more programming languages or combinations thereof. The programming languages include object-oriented programming languages such as Java, Smalltalk, C++, and also include conventional procedural programming languages such as the "C" language or similar programming languages. The program code can be executed entirely on the client computer, partially on the client computer, executed as a stand-alone software package, partially on the client computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer can be connected to the client computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or it can be connected to an external computer (e.g., by connecting through the Internet using an Internet service provider).

[0098] The flowcharts and block diagrams in the accompanying drawings illustrate the possible architectures, functions, and operations of devices, methods, and computer program products according to various embodiments of this application. In this regard, each block in the flowchart or block diagram can represent a module, a program segment, or a portion of code that contains one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions marked in the blocks can occur in a different order than marked in the accompanying drawings. For example, two consecutive blocks shown can actually be executed substantially in parallel, and they can sometimes be executed in the reverse order, depending on the functions involved. It should also be noted that each block in the block diagram and / or flowchart, and combinations of blocks in the block diagram and / or flowchart, can be implemented by a dedicated hardware-based system that performs the specified functions or operations, or can be implemented by a combination of dedicated hardware and computer instructions.

[0099] The units involved in the embodiments described in this application can be implemented in software or in hardware. The described units can also be provided in a processor. For example, it can be described as: a processor including an acquisition unit, a determination unit, and a generation unit. Among them, the names of these units do not constitute a limitation on the unit itself in some cases. For example, the determination unit can also be described as "the unit that determines at least part of the replacement code of the code corresponding to the first application in the second operating system based on the code correspondence between the first operating system and the second operating system".

[0100] As another aspect, the present application also provides a computer-readable medium, which may be included in the device described in the above embodiments; or may exist alone without being assembled into the device. The above computer-readable medium carries one or more programs, and when the one or more programs are executed by the device, the computer device is caused to: obtain the code of the first application in the first operating system; determine, based on the code correspondence between the first operating system and the second operating system, replacement code in the second operating system corresponding to at least part of the code of the first application; and generate, according to the replacement code, the code of the second application in the second operating system corresponding to the first application.

[0101] The above description is only a preferred embodiment of the present application and an explanation of the applied technical principle. Those skilled in the art should understand that the scope of the invention involved in the present application is not limited to the technical solution formed by the specific combination of the above technical features, and should also cover other technical solutions formed by any combination of the above technical features or their equivalent features without departing from the above inventive concept. For example, the technical solutions formed by mutually replacing the above features with the (but not limited to) technical features having similar functions disclosed in the present application.

Claims

1. A method for generating code, comprising: Obtaining the code of a first application in a first operating system; Based on the code correspondence between the first operating system and a second operating system, determining replacement code in the second operating system corresponding to at least part of the code of the first application, including: determining whether the code of the first application includes code to be replaced according to a pre-established first code feature model library of the first operating system; in response to determining that it does, for each part of the code to be replaced included in the code of the first application, determining replacement code for that part of the code to be replaced from a second code feature model library of the second operating system, where the code correspondence characterizes the correspondence between codes in the first operating system and the second operating system that implement the same or similar functions and complete the same or similar operations; Generating, according to the replacement code, code of a second application in the second operating system corresponding to the first application.

2. The method according to claim 1, wherein The step of, for each part of the code to be replaced included in the code of the first application, determining replacement code for that part of the code to be replaced from the second code feature model library of the second operating system includes: For each part of the code to be replaced included in the code of the first application, based on a tree matching algorithm, determining the matching degree between the feature model tree of that part of the code to be replaced and the feature model tree of the code feature model in the second code feature model library, and according to the matching degree, determining replacement code for that part of the code to be replaced from the second code feature model library.

3. The method according to claim 2, wherein The code to be replaced includes replacement function code characterizing the function information of the first application and replacement layout code characterizing the layout information of the first application; And The step of, for each part of the code to be replaced included in the code of the first application, based on a tree matching algorithm, determining the matching degree between the feature model tree of that part of the code to be replaced and the feature model tree of the code feature model in the second code feature model library, and according to the matching degree, determining replacement code for that part of the code to be replaced from the second code feature model library includes: For each part of the replacement function code included in the code of the first application, based on a tree matching algorithm, determining the matching degree between the feature model tree of that part of the replacement function code and the feature model tree of the function code feature model in the second code feature model library, and according to the matching degree between the feature model tree of that part of the replacement function code and the feature model tree of the function code feature model in the second code feature model library, determining replacement function code for that part of the replacement function code from the second code feature model library; For each part of the layout code to be replaced included in the code of the first application, based on the tree matching algorithm, determine the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the second code feature model library, and based on the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the second code feature model library, determine the replacement layout code of this part of the layout code to be replaced from the second code feature model library.

4. The method according to claim 3, wherein, The step of, for each part of the layout code to be replaced included in the code of the first application, based on the tree matching algorithm, determine the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the second code feature model library, and based on the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the second code feature model library, determine the replacement layout code of this part of the layout code to be replaced from the second code feature model library, includes: Adopt the depth-first search algorithm, starting from the root view node in the first application, for the layout code to be replaced corresponding to each view node, based on the tree matching algorithm, determine the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the second code feature model library, and based on the matching degree between the feature model tree of this part of the layout code to be replaced and the feature model tree of the layout code feature model in the second code feature model library, determine the replacement layout code of this part of the layout code to be replaced from the second code feature model library.

5. The method according to claim 1, wherein It further includes: Establish the first code feature model library for the first operating system; Establish the second code feature model library for the second operating system.

6. The method according to any one of claims 1-5, wherein, The first operating system is the Android system, and the second operating system is the HarmonyOS system.

7. A device for generating code, including: An acquisition unit configured to acquire the code of a first application in a first operating system; A determination unit configured to determine, based on the code correspondence between the first operating system and the second operating system, the replacement code of at least part of the code in the second operating system corresponding to the first application, where the code correspondence represents the correspondence between the code in the first operating system and the second operating system that implements the same or similar functions and completes the same or similar operations; A generation unit configured to generate, according to the replacement code, the code of a second application in the second operating system corresponding to the first application; The determination unit is further configured to: According to the first code feature model library of the first operating system established in advance, determine whether the code of the first application includes code to be replaced; in response to determining that it includes, for each part of the code to be replaced included in the code of the first application, determine the replacement code of this part of the code to be replaced from the second code feature model library of the second operating system.

8. The apparatus according to claim 7, wherein, The determining unit is further configured to: For each part of the code to be replaced included in the code of the first application, based on a tree matching algorithm, determine the matching degree between the feature model tree of this part of the code to be replaced and the feature model tree of the code feature model in the second code feature model library, and determine the replacement code of this part of the code to be replaced from the second code feature model library according to the matching degree.

9. The device according to claim 8, wherein, The code to be replaced includes the code of the function to be replaced characterizing the function information of the first application and the code of the layout to be replaced characterizing the layout information of the first application; and The determining unit is further configured to: For each part of the code of the function to be replaced included in the code of the first application, based on a tree matching algorithm, determine the matching degree between the feature model tree of this part of the code of the function to be replaced and the feature model tree of the function code feature model in the second code feature model library, and determine the replacement function code of this part of the code of the function to be replaced from the second code feature model library according to the matching degree between the feature model tree of this part of the code of the function to be replaced and the feature model tree of the function code feature model in the second code feature model library; for each part of the code of the layout to be replaced included in the code of the first application, based on a tree matching algorithm, determine the matching degree between the feature model tree of this part of the code of the layout to be replaced and the feature model tree of the layout code feature model in the second code feature model library, and determine the replacement layout code of this part of the code of the layout to be replaced from the second code feature model library according to the matching degree between the feature model tree of this part of the code of the layout to be replaced and the feature model tree of the layout code feature model in the second code feature model library.

10. The device according to claim 9, wherein, The determining unit is further configured to: Adopt a depth-first search algorithm, starting from the root view node in the first application, for the code of the layout to be replaced corresponding to each view node, based on a tree matching algorithm, determine the matching degree between the feature model tree of this part of the code of the layout to be replaced and the feature model tree of the layout code feature model in the second code feature model library, and determine the replacement layout code of this part of the code of the layout to be replaced from the second code feature model library according to the matching degree between the feature model tree of this part of the code of the layout to be replaced and the feature model tree of the layout code feature model in the second code feature model library.

11. The device according to claim 7, wherein, It further includes: A first establishing unit configured to establish the first code feature model library for the first operating system; A second establishing unit configured to establish the second code feature model library for the second operating system.

12. The apparatus according to any one of claims 7-11, wherein, The first operating system is the Android system, and the second operating system is the HarmonyOS.

13. A computer-readable medium having a computer program stored thereon, wherein, When the program is executed by a processor, it implements the method according to any one of claims 1-6.

14. An electronic device, comprising: One or more processors; A storage device on which one or more programs are stored, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1-6.

Citation Information

Patent Citations

  • Compiler and realizing method thereof

    CN101546272A

  • Application generation method and device and device for generating application

    CN113220280A