A code generation method for Android system
The code generation method for Android systems addresses inefficiencies in manual coding by automating code generation with templates and placeholders, enhancing development speed and reducing errors, thus supporting rapid iterative development.
Patent Information
- Application Number
- CN202210840434.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-07-18
- Publication Date
- 2024-12-06
- Estimated Expiration
- 2042-07-18
AI Technical Summary
The existing manual coding process for Android vehicle-mounted systems is cumbersome, inefficient, prone to inconsistencies, and errors due to numerous interfaces that frequently change, and lacks compatibility with Windows, hindering rapid iterative development.
A code generation method using a tool that automatically generates code for Android communication using AIDL mode, with predefined templates and placeholders, reducing human error and shortening development time.
The method significantly reduces development time, improves code robustness, and adapts to rapid iterative development by generating compiled jars and running jars efficiently, ensuring uniformity and correctness.
Smart Images

Figure CN115098076B_ABST
Abstract
Description
Technical Field
[0001] The invention relates to a code generation method in the field of vehicle-mounted systems. Background Art
[0002] The OEM provides a set of unified external interfaces, but does not implement them. Due to the large number of interfaces, a lot of code needs to be written, and the interfaces are constantly changing. Manual writing can lead to many inconsistencies. In Android, one process usually cannot access the memory of another process. Therefore, in order to communicate, the process needs to decompose its objects into primitives that the operating system can understand, and marshal them into objects that you can operate. Writing code to perform this marshaling operation is cumbersome. Summary of the invention
[0003] The purpose of the present invention is to provide a code generation method for an Android system, which has a simple operation process, high efficiency, greatly shortens the development time of the module in the project, improves the robustness of the code, greatly reduces human-induced errors, and can adapt to the rapid iterative development mode in the project.
[0004] To achieve the above purpose, the present invention provides a code generation method for an Android system, comprising the following steps: step 1, making a list and a code template; step 2, making a tool; step 3, the tool uses the code template to generate corresponding code.
[0005] Compared with the prior art, the beneficial effect of the present invention is that it can automatically generate code for all the contents of the communication in the AIDL mode in Android, provide a compiled jar for development and a running jar for actual operation. This tool can be developed based on WPF, has good windows compatibility, is simple to operate, has high efficiency, greatly shortens the development time of the module in the project, improves the robustness of the code, greatly reduces human errors, and can adapt to the rapid iterative development mode in the project.
[0006] As a further improvement of the present invention, the specific contents of the list production in step 1 are as follows: the contents that need to be generated by code are clearly defined, and the code structure is analyzed, including the contents of the package body, class body, and method body, and the Excel interface list file is designed. The fields in the list file are dynamically adjusted according to actual needs; for the fields used for generation logic judgment of code generation, corresponding descriptions must be given.
[0007] As a further improvement of the present invention, the specific content of making the code template in step 1 is as follows:
[0008] Separate the unchanged content from the changed content first. The unchanged content can be directly written into the template in the form of code. The changed content is then abstracted and represented by placeholders, and written into the template with the common content. The following necessary templates are included:
[0009] AIDL template, used to generate AIDL files;
[0010] Main template, used for code generation of the main body: service connection, service reconnection, event handling, copyright, import part, member variables, common methods;
[0011] Method templates, used for code generation of methods;
[0012] Constant template: used for code generation of various constants;
[0013] Stub template, used for code generation of service proxy for AIDL mechanism.
[0014] As a further improvement of the present invention, the AIDL template includes the following contents:
[0015] 1. The generated file has the suffix .aidl; 2. The template content is divided into two parts, the file header part and the method part.
[0016] As a further improvement of the present invention, the main template includes the following contents:
[0017] 1. Copyright description 2. Description of the import part; 3. Class declaration; 4. Declaration of member variables; 5. General method part.
[0018] As a further improvement of the present invention, the method template includes the following content, and the method template describes the interface in the specific manifest file:
[0019] 1. The first method template: the void type return value, method name, method comment, method parameters and the final call described by the method template;
[0020] 2. The second method template describes the return value, method name, method comment, method parameters, and the final call situation.
[0021] As a further improvement of the present invention, the constant template includes the following contents:
[0022] 1. Manifest template, based on Android's security mechanism, needs to declare relevant permissions in AndroidManifest.xml;
[0023] 2. Service constant template: The three constants ACTION, PACKAGE_NAME, and CLASS_NAME are associated with the corresponding fields in the manifest file.
[0024] As a further improvement of the present invention, the placeholders of the Stub template are replaced with the contents of the corresponding fields in the manifest when the code is generated.
[0025] As a further improvement of the present invention, the specific content of step 2 is as follows:
[0026] Step 2.1, import the Excel interface list file; Step 2.2, set the directory for aidl and code generation; Step 2.3, generate corresponding folders according to the fields; Step 2.4, generate corresponding class files according to the template; Step 2.5, generate corresponding interface methods in the generated class files according to the method or function template.
[0027] As a further improvement of the present invention, the specific content of step 3 is as follows:
[0028] Step 3.1, the tool first reads the built-in template file, that is, the file with the suffix .hsaetemplate. In the running interface, click "Browse" to select the list file, and "Code Directory" to select the directory for code generation. Finally, click Generate to complete the code generation action;
[0029] Step 3.2, generate compilation jar and running jar: Use the project's Android system source code environment to write Android.mk or Android.bp files, perform modular compilation, and generate the corresponding compilation jar and running jar. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] Figure 1 This is a diagram of the fields and description of the manifest file in the present invention.
[0031] Figure 2 This is a program diagram of the file header part of the AIDL template in the present invention.
[0032] Figure 3 This is a partial program diagram of the method of the AIDL template in the present invention.
[0033] Figure 4 This is a specific program diagram for the copyright description of the main template in the present invention.
[0034] Figure 5 This is a specific program diagram of the import part of the main template in the present invention.
[0035] Figure 6 This is a specific program diagram for declaring the main template class in the present invention.
[0036] Figure 7 This is a specific program diagram for declaring member variables of the main template in the present invention.
[0037] Figure 8 This is a description diagram of member variables in the declaration of member variables of the main template in the present invention.
[0038] Fig. 9 This is a specific procedure diagram of the general method part of the main template in the present invention.
[0039] Fig.10 This is a specific procedure diagram of the first method template of the method template in the present invention.
[0040] Fig.11 This is a specific procedure diagram of the second method template of the method template in the present invention.
[0041] Fig.12 It is a specific program diagram of the Manifest template of the constant template in the present invention.
[0042] Fig.13 This is a specific program diagram of the service constant template of the constant template in the present invention.
[0043] Fig.14 This is a specific program diagram of the Stub template in the present invention.
[0044] Fig.15 Create a flow chart for the tool in this invention.
[0045] Fig.16 This is a schematic diagram of a template file built into a tool in the present invention.
[0046] Fig.17 This is a diagram of the operating interface of the tool after it is completed in the present invention.
[0047] Fig.18 Screenshots of some folders, aidl and java files during actual use.
[0048] Fig.19 This is a screenshot of the number of files generated during actual use. DETAILED DESCRIPTION
[0049] The present invention is further described below in conjunction with the accompanying drawings:
[0050] like Figure 1-19 A code generation method for an Android system as shown, comprising the following steps:
[0051] Step 1, create list and code template;
[0052] Identify the content that needs to be generated by code, analyze it from the structure of the code, including the contents of the package body, class body, and method body, design the Excel interface manifest file, and dynamically adjust the fields in the manifest file according to actual needs; for the fields used for generation logic judgment of code generation, corresponding descriptions should be given.
[0053] The specific contents of making the code template are as follows:
[0054] Separate the unchanged content from the changed content first. The unchanged content can be directly written into the template in the form of code. The changed content is then abstracted and represented by placeholders, and written into the template with the common content. The following necessary templates are included:
[0055] 1. AIDL template, used to generate AIDL files;
[0056] 1. The generated file has the suffix .aidl;
[0057] 2. The template content is divided into two parts, the file header part and the method part.
[0058] 2. Main template, used for code generation of the main body: service connection, service reconnection, event handling, copyright, import part, member variables, common methods;
[0059] 1. Copyright description
[0060] 2. Description of the import part;
[0061] 3. Class declaration;
[0062] 4. Declaration of member variables;
[0063] 5. General methods section.
[0064] 3. Method template, used for method code generation; it describes the interface in the specific manifest file:
[0065] 1. The first method template: the void type return value, method name, method comment, method parameters and the final call described by the method template;
[0066] 2. The second method template describes the return value, method name, method comment, method parameters, and the final call situation.
[0067] 4. Constant template: used for code generation of various constants;
[0068] 1. Manifest template, based on Android's security mechanism, needs to declare relevant permissions in AndroidManifest.xml;
[0069] 2. Service constant template: The three constants ACTION, PACKAGE_NAME, and CLASS_NAME are associated with the corresponding fields in the manifest file.
[0070] 5. Stub template, used for code generation of service proxy for AIDL mechanism.
[0071] The placeholders of the Stub template are replaced with the contents of the corresponding fields in the manifest when the code is generated.
[0072] Step 2, tool making;
[0073] Step 2.1, import the excel interface list file;
[0074] Step 2.2, set the directory for aidl and code generation;
[0075] Step 2.3, generate corresponding folders according to the fields;
[0076] Step 2.4, generate the corresponding class file according to the template;
[0077] Step 2.5, generate the corresponding interface method according to the method or function template in the generated class file.
[0078] Step 3: The tool uses the code template to generate the corresponding code.
[0079] Step 3.1, the tool first reads the built-in template file, that is, the file with the suffix .hsaetemplate. In the running interface, click "Browse" to select the list file, and "Code Directory" to select the directory for code generation. Finally, click Generate to complete the code generation action;
[0080] Step 3.2, generate compilation jar and running jar: Use the project's Android system source code environment to write Android.mk or Android.bp files, perform modular compilation, and generate the corresponding compilation jar and running jar.
[0081] In the present invention, the AIDL usage process is as follows:
[0082] 1. Create an .aidl file - this file defines the programming interface with method signatures.
[0083] 2. Implement the interface - The Android SDK tool generates an interface based on the .aidl file using the Java programming language. This interface has an internal abstract class called Stub, which is used to extend the Binder class and implement the methods in the AIDL interface. You must extend the Stub class and implement these methods.
[0084] 3. Expose the interface to the client—implement Service and override onBind() to return the implementation of the Stub class.
[0085] When making a list file, that is, an Excel file, the description of the generation logic judgment field used for code generation is as follows Figure 1 shown.
[0086] In the AIDL template, the program in the file header is as follows Figure 2 The specific procedures in the method section are as shown in Figure 3 shown.
[0087] In the main template, the specific procedures for copyright instructions are as follows Figure 4 As shown, all files are the same and the content can be modified as required.
[0088] In the main template, the import part is described, and the specific procedures are as follows Figure 5 As shown, this part includes the common package description in Java, and the classes that need to be introduced when using the Android interface including the AIDL mechanism. Pay special attention to the interfaces or classes we define ourselves, which need to be replaced by special constant strings (also called placeholders), such as: root_package_hsae, package_name_hsae, aidl_name_hsae, aidl_callback_name_hsae, etc. These placeholders will be replaced with the contents of the corresponding fields in the prerequisite when code generation is being performed.
[0089] In the main template, the class declaration, the specific procedures are as follows Figure 6 As shown, this part describes the declaration of the class. The declaration of the class is related to the function module, function submodule and other fields mentioned in the prerequisites. It is generally used to generate class comments. In addition, it should be noted that the author, version number, date and other information need to be added to facilitate the traceability of the subsequently generated code. Similarly, class_name_hsae is a placeholder and will be replaced when the code is generated.
[0090] In the main template, the member variable declaration is as follows: Figure 7 As shown, the member variables that must be used in all situations described in this section are described as follows Figure 8 shown.
[0091] In the main template, the specific procedures in the general method section are as follows Fig. 9 As shown, due to the large amount of content, Fig. 9 Only a part of the common methods are shown, such as the construction method and the method for binding the target service. The content of this part must be common, because each functional module or sub-module must have such methods, and non-common methods cannot be written into it.
[0092] The first method template in the method template has the following specific procedures: Fig.10 As shown in the figure, the method template describes the void type return value, method name, method comment, method parameters and the final call; the second method template in the method template has the following specific procedures: Fig.11 As shown, the method template describes the return value, method name, method comment, method parameters and the final call situation; although there are many method templates, the principles are the same.
[0093] The Manifest template in the constant template has the following specific procedures: Fig.12 As shown; the specific program of the service constant template is as follows Fig.13 shown.
[0094] The specific procedures of the Stub template are as follows Fig.14 As shown, the placeholders will be replaced with the contents of the corresponding fields in the manifest when the code is generated.
[0095] The specific process of tool making is as follows: Fig.15 As shown, the template file can be built into the tool (or you can select the file yourself), as shown below Fig.16 shown.
[0096] After the tool is built, run it as follows Fig.17 As shown, its interface can be adjusted as needed.
[0097] The present invention can greatly reduce the code writing of engineers and ensure uniformity and correctness.
[0098] There are 1232 interfaces in the project under development. With this tool, it only takes 10 to 20 minutes to generate a compiled jar and run jar with 60,000 to 70,000 lines of code when there is no problem with the interface list. This greatly improves efficiency and eliminates code logic errors. Some folders, aidl and java files are as follows Fig.18 As shown; the number of files generated is 950, as shown in Fig.19 shown.
[0099] The present invention prepares an interface list, runs a code tool, generates code, writes Android.mk or Android.bp, and compiles through an Android source code environment to obtain a compiled jar and a running jar; templates of various functions can be prepared, and the templates can be reused to be abstract and universal.
[0100] The present invention is not limited to the above-mentioned embodiments. On the basis of the technical solution disclosed herein, technicians in this field can make some substitutions and deformations to some technical features therein according to the disclosed technical content without creative labor, and these substitutions and deformations are all within the protection scope of the present invention.
Claims
1. A code generation method for an Android system, characterized in that: The following steps are included: Step 1, create list and code template; The specific contents of the list are as follows: Clearly define the content that needs to be generated by code, analyze the structure of the code, including the package body, class body, and method body, design the Excel interface manifest file, and dynamically adjust the fields in the manifest file according to actual needs; for the fields used for the generation logic judgment of code generation, corresponding descriptions should be given; The specific contents of making the code template are as follows: Separate the unchanged content from the changed content first. The unchanged content can be directly written into the template in the form of code. The changed content is then abstracted and represented by placeholders, and written into the template with the common content. The following necessary templates are included: AIDL template, used to generate AIDL files; Main template, used for code generation of the main body: service connection, service reconnection, event handling, copyright, import part, member variables, common methods; Method templates, used for code generation of methods; Constant template: used for code generation of various constants; Stub template, used for code generation of service proxy for AIDL mechanism; The AIDL template includes the following:
1. The generated file has the suffix .aidl; 2. The template content is divided into two parts, the file header part and the method part; Step 2, tool making; Step 3: The tool uses the code template to generate the corresponding code.
2. A code generation method for an Android system according to claim 1, characterized in that: The main template includes the following, 1. Copyright description 2. Description of the import part; 3. Class declaration; 4. Declaration of member variables; 5. General methods section.
3. A code generation method for an Android system according to claim 2, characterized in that: The method template includes the following, The method template describes the interface in the specific manifest file:
1. The first method template: the void type return value, method name, method comment, method parameters and the final call described by the method template; 2. The second method template describes the return value, method name, method comment, method parameters, and the final call situation.
4. A code generation method for an Android system according to claim 3, characterized in that: The constant template includes the following:
1. Manifest template, based on Android's security mechanism, needs to declare relevant permissions in AndroidManifest.xml; 2. Service constant template: The three constants ACTION, PACKAGE_NAME, and CLASS_NAME are associated with the corresponding fields in the manifest file.
5. A code generation method for an Android system according to claim 4, characterized in that: The placeholders of the Stub template are replaced with the contents of the corresponding fields in the manifest when the code is generated.
6. A code generation method for an Android system according to claim 5, characterized in that: The specific contents of step 2 are as follows: Step 2.1, import the excel interface list file; Step 2.2, set the directory for aidl and code generation; Step 2.3, generate corresponding folders according to the fields; Step 2.4, generate the corresponding class file according to the template; Step 2.5, generate the corresponding interface method according to the method or function template in the generated class file.
7. A code generation method for an Android system according to claim 6, characterized in that: The specific contents of step 3 are as follows: Step 3.1, the tool first reads the built-in template file, that is, the file with the suffix .hsaetemplate. In the running interface, click "Browse" to select the manifest file, and "Code Directory" to select the directory for code generation. Finally, click Generate to complete the code generation action; Step 3.2, generate compilation jar and running jar: Use the project's Android system source code environment to write Android.mk or Android.bp files, perform modular compilation, and generate the corresponding compilation jar and running jar.
