Android Compilation Period API Replacement Method, Device, Computer Equipment and Storage Medium
By adding and replacing API annotations during the compilation period of Android application and using the code conversion tool subclass for API replacement, the problem of not being able to replace the API during the compilation period is solved, and the inspection and modification of the API to obtain the privacy information before the installation package is generated.
Patent Information
- Application Number
- CN202210709742.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-22
- Publication Date
- 2025-05-27
- Estimated Expiration
- 2042-06-22
AI Technical Summary
The existing technology cannot perform API replacement during the compilation period of Android application, resulting in the inability to locate and resolve the issue of obtaining privacy information before the installation package file is generated.
Scan and replace the target API during compilation by adding the target API annotations and replacement API annotations to the target Android project's preset files.
It implements the replacement of the target API before the Android project generates the installation package file, ensuring that the API involving user privacy information access is checked and modified before the installation package is generated.
Smart Images

Figure CN115202665B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software development, and in particular, to an Android compilation period API replacement method, device, computer device, and storage medium. Background Art
[0002] In the technical field of software development, mobile application review organizations are very strict in reviewing the acquisition of privacy information on mobile devices, and severely punish mobile applications that obtain privacy information on mobile devices without the user's permission. Therefore, developers of mobile applications need to conduct self-checks on the acquisition of privacy information of mobile applications before release. At present, the self-checks on the problem of privacy information acquisition of mobile applications are all based on the analysis of the installed package files of mobile applications after compilation, which belongs to the method of post-analysis and cannot locate and solve the problem of privacy information acquisition before the generation of mobile application installed package files. Summary of the Invention
[0003] Embodiments of the present invention provide an Android compilation period API replacement method, device, computer device, and storage medium to solve the problem that API replacement cannot be performed during the compilation period of Android applications in the prior art.
[0004] In the first aspect of the present application, an Android compilation period API replacement method is provided, including:
[0005] Adding the annotations of the target API and the annotations of the corresponding replacement API to a preset first target file of the target Android project according to a preset annotation definition rule;
[0006] Adding at least one target API and the corresponding replacement API to a preset second target file of the target Android project by using the annotations of the target API and the annotations of the replacement API included in the first target file;
[0007] Creating a first subclass of the code conversion tool for the second target file and a second subclass of the code conversion tool for performing API replacement processing in the target Android project, where the first subclass of the code conversion tool and the second subclass of the code conversion tool respectively inherit the code conversion tool class in a preset build tool;
[0008] Running a compilation command in the target Android project and compiling the target Android project using a preset compilation environment;
[0009] Scanning the preset second target file by using the first subclass of the code conversion tool and generating a first configuration file including the target API and the corresponding replacement API according to the annotation definition rule;
[0010] Use the second code conversion tool subclass to detect the code of the target Android project during the compilation period. If the code contains the target API in the first configuration file, replace the corresponding API in the code with the replacement API according to the preset first replacement rule.
[0011] In the second aspect of the present application, there is provided an Android compilation period API replacement device, including:
[0012] A first target file module, configured to add the annotations of the target API and the annotations of the corresponding replacement API to a preset first target file of the target Android project according to the preset annotation definition rule.
[0013] A second target file module, configured to add at least one target API and the corresponding replacement API to a preset second target file of the target Android project by using the annotations of the target API and the annotations of the replacement API included in the first target file.
[0014] A code conversion tool subclass module, configured to create a first code conversion tool subclass of the second target file and a second code conversion tool subclass for performing API replacement processing on the code in the target Android project. The first code conversion tool subclass and the second code conversion tool subclass respectively inherit the code conversion tool class in the preset build tool.
[0015] A code compilation module, configured to run the compilation command in the target Android project and compile the target Android project using the preset compilation environment.
[0016] A first configuration file module, configured to scan the preset second target file by using the first code conversion tool subclass and generate a first configuration file including the target API and the corresponding replacement API according to the annotation definition rule.
[0017] An API replacement module, configured to use the second code conversion tool subclass to detect the code of the target Android project during the compilation period. If the code contains the target API in the first configuration file, replace the corresponding API in the code with the replacement API according to the preset first replacement rule.
[0018] In the third aspect of the present application, there is provided a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the steps of the above Android compilation period API replacement method are implemented.
[0019] In a fourth aspect of the present application, there is provided a computer-readable storage medium storing a computer program, which when executed by a processor implements the steps of the above Android compilation period API replacement method.
[0020] For the above Android compilation period API replacement method, device, computer device and storage medium, by adding the annotations of the target API and the corresponding replacement API to a preset first target file of the target Android project, and adding the target API and the corresponding replacement API to a preset second target file of the target Android project. At the same time, a first subclass of the code conversion tool and a second subclass of the code conversion tool are newly created. Then, during the compilation of the target Android project, the first subclass of the code conversion tool is used to scan the preset second target file to generate a first configuration file containing the target API and the corresponding replacement API. At the same time, the code of the target Android project during the compilation period is detected. If the target API is included in the code, the corresponding API in the code is replaced with the replacement API. The purpose of replacing the target API during the compilation of the target Android project is achieved, and the target API in the target Android project can be replaced before the installation package file of the target Android project is generated, that is, before the installation package file of the Android project is generated, the target API involving the access to user privacy information in the target Android project is replaced, and the inspection and modification of the access to user privacy information are realized before the installation package file of the target Android project is generated. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for the description of the embodiments of the present invention will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments of the present invention, and those of ordinary skill in the art can obtain other drawings without creative efforts.
[0022] Figure 1 is a schematic diagram of an application environment of the Android compilation period API replacement method in an embodiment of the present invention;
[0023] Figure 2 is a flowchart of the Android compilation period API replacement method in an embodiment of the present invention;
[0024] Figure 3 is a schematic structural diagram of the Android compilation period API replacement device in an embodiment of the present invention;
[0025] Figure 4 is a schematic diagram of a computer device in an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0026] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.
[0027] The Android compilation period API replacement method provided by this application can be applied in, for example, Figure 1 such application environments. Among them, the computer device can be, but is not limited to, various personal computers and laptop computers. The computer device can also be a server, which can be an independent server or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, Content Delivery Network (CDN), and big data and artificial intelligence platforms. It can be understood that Figure 1 the number of computer devices in
[0028] In one embodiment, as Figure 2 shown, an Android compilation period API replacement method is provided. Taking the method applied in the Figure 1 server in
[0029] S101. Add the annotations of the target API and the corresponding annotations of the replacement API to the preset first target file of the target Android project according to the preset annotation definition rules.
[0030] Specifically, first, add the dependency of the annotation tool library to the project configuration file of the target Android project and download the annotation tool library to the target Android project. Among them, annotation is a conventional software development technology. Annotation can simplify code and improve the coding efficiency of software developers. On the one hand, annotation provides information to the compiler, and on the other hand, it increases the processing of code during the compilation stage and the processing of code during the running stage. Then, in the target Android project, use the annotation tool library to create the first target file containing the annotation of the target API and the annotation of the replacement API according to the annotation definition rules. Among them, software developers can customize new annotations according to their needs using the annotation tool library. For example, software developers define according to the annotation tool library that originFunction is used to represent the API before replacement, and targetFunction is used to represent the API after replacement. Among them, the annotation of the target API and the annotation of the replacement API need to be persistently saved in the first target file to ensure that the build tool in the target Android project can obtain the annotation of the target API and the annotation of the replacement API from the first target file during code compilation. Finally, add a reference to the first target file to the project configuration file in the target Android project. Because a reference to the first target file must be added to the project configuration file in the Android project, the build tool of the target Android project can find the first target file through the project configuration file, and then the build tool of the target Android project can obtain the annotation of the target API and the annotation of the replacement API through the first target file. Further, not only define the annotation of the target API and the annotation of the replacement API, but also define other annotations related to the annotation of the target API and the annotation of the replacement API. The other annotations include but are not limited to: target API description annotation, replacement API description annotation, target API class annotation, replacement API class annotation.
[0031] S102. Add at least one target API and the corresponding replacement API to the preset second target file of the target Android project by using the annotation of the target API and the annotation of the replacement API included in the first target file.
[0032] Specifically, first, obtain the type of the target API, and verify whether the type of the replacement API is the same as that of the target API. Among them, if the type of the replacement API is different from that of the target API, unpredictable bugs will occur during the operation of the target Android project or the target Android project will fail to compile after using the replacement API to replace the target API. If the type of the replacement API is the same as that of the target API, obtain the first annotation corresponding to the type of the target API from the first target file. Then, obtain the second annotation corresponding to the type of the replacement API from the first target file. Among them, the first annotation and the second annotation are different annotations based on the premise of the same type. For example, both the first annotation and the second annotation are annotations of the static method type. On the contrary, if the types of the first annotation and the second annotation are different, the first annotation and the second annotation cannot establish a corresponding relationship. For example, the first annotation is of the static method type, but the second annotation is of the member method type. As is well known in software development technology, static methods and member methods cannot be replaced with each other. Finally, generate an API replacement function that uses the replacement API to replace the target API in the preset second target file using the first annotation and the second annotation. Among them, after generating the replacement function using the first annotation and the second annotation, it must also be persistently saved in the second target file so that the build tool can obtain the API replacement function from the second target file during the compilation of the target Android project, and then execute the function of the API replacement function.
[0033] S103. Create a first subclass of the code conversion tool for the second target file and a second subclass of the code conversion tool for API replacement processing in the target Android project. The first subclass of the code conversion tool and the second subclass of the code conversion tool inherit the code conversion tool class in the preset build tool respectively.
[0034] Among them, the build tool is used to build the target Android project, and the build tool includes a code conversion tool class. The code conversion tool class can operate on the class file of the target Android project before the class file is converted into a dex format file. At the same time, the code conversion tool class is an abstract class, that is, software developers can inherit this code conversion tool class according to their needs and customize subclasses with custom functions.
[0035] S104. Run the compilation command in the target Android project and compile the target Android project using the preset compilation environment.
[0036] Among them, as Figure 1The compilation environment is pre-deployed on the computer device shown, and all code files of the target Android project are correspondingly pre-stored on the computer device or downloaded from the corresponding code server and stored on the computer device.
[0037] S105. Use the first code conversion tool subclass to scan the preset second target file, and generate a first configuration file containing the target API and the corresponding replacement API according to the annotation definition rule.
[0038] Specifically, first, use the first code conversion tool subclass to obtain the preset second target file in the compilation period. Then, use the second code conversion tool subclass to scan the preset second target file to obtain the annotations of the target API and the replacement API in the second target file. At the same time, obtain the target API corresponding to the annotation of the target API and the replacement API corresponding to the annotation of the replacement API respectively. Among them, during the compilation of the target Android project, all code files of the target Android project are converted into bytecode, and the code conversion tool class can operate on the bytecode. Further, the first code conversion tool subclass and the second code conversion tool subclass that inherit the code conversion tool class can also operate on the bytecode. Among them, in the preset second target file, the annotation is used for identification, that is, the annotation of the target API identifies the target API, and the annotation of the replacement API identifies the replacement API. When the second code conversion tool subclass scans the preset second target file, it first scans the annotation of the target API or the annotation of the replacement API, and then finds the corresponding target API or replacement API identified by the annotation according to the annotation of the target API or the annotation of the replacement API. Finally, establish an association relationship between the target API and the corresponding replacement API, and add the association relationship to the first configuration file.
[0039] S106. Use the second code conversion tool subclass to detect the code of the target Android project in the compilation period. If the code contains the target API in the first configuration file, replace the corresponding API in the code with the replacement API according to the preset first replacement rule.
[0040] Specifically, first, use the second code conversion tool subclass to obtain the code of the target Android project that has been converted into bytecode. Then, use the class accessor and function accessor included in the second code conversion tool subclass to obtain each function in each class in the code of the target Android project from the bytecode. At the same time, match each function in each class with the target API in the first configuration file to obtain the successfully matched functions and the successfully matched target APIs. It should be noted that both the target API and the replacement API are functions, and the judgment condition for matching the target API with each function in each class is to judge whether the function names are the same. Optionally, judging whether the function names are the same also includes whether to ignore letter case. Finally, replace the successfully matched functions with the replacement APIs corresponding to the successfully matched target APIs according to the preset first replacement rule.
[0041] Further, the step of replacing the successfully matched functions with the replacement APIs corresponding to the successfully matched target APIs according to the preset first replacement rule includes: First, obtain the judgment condition of the successfully matched functions from the code of the target Android project according to the preset first replacement rule. The preset first replacement rule is set accordingly according to specific business requirements. For example, a specific business requirement is to replace the location acquisition function for obtaining the location information of the Android device in the target Android project, and the location acquisition function must be called after obtaining the user's consent. Then the first replacement rule is to judge whether the user has consented to the acquisition of the location information of their Android device. If the user has not consented, other functions must be used to replace the location acquisition function. If the user has consented, there is no need to use other functions to replace the location acquisition function. At the same time, if the judgment condition is satisfied, replace the successfully matched functions with the replacement APIs corresponding to the successfully matched target APIs. If the replacement operation of replacing the successfully matched functions with the replacement APIs corresponding to the successfully matched target APIs fails, retry until the replacement operation is successful, where the number of retries is equal to or greater than the preset number of repetitions. Retrying the replacement operation can further improve the success rate of the replacement operation.
[0042] Further, after replacing the successfully matched function with the replacement API corresponding to the preset first replacement rule, the following steps are included: First, generate an API replacement operation record according to the successfully matched function and the information of the replacement API that is successfully or unsuccessfully replaced. Then, add the API replacement operation record to the compilation output log information of the target Android project. Further, the replacement operation record includes, but is not limited to: the function name of the successfully matched function, the class name of the class where the successfully matched function is located, the relative path of the file of the class where the successfully matched function is located in the target Android project, the line number of the code of the successfully matched function, the function name of the replacement API, the successful replacement time, the failed replacement time, and the number of replacement retries. Further, save the compilation output log information in a preset compilation output log database, so that the API replacement operations performed during the compilation of the target Android project can be traced and documented.
[0043] The Android compilation period API replacement method proposed in this embodiment adds the annotations of the target API and the corresponding replacement API to the preset first target file of the target Android project, adds the target API and the corresponding replacement API to the preset second target file of the target Android project, and at the same time, creates a first subclass of the code conversion tool and a second subclass of the code conversion tool. Then, during the compilation of the target Android project, use the first subclass of the code conversion tool to scan the preset second target file to generate a first configuration file containing the target API and the corresponding replacement API, and at the same time, detect the code of the target Android project during the compilation period. If the target API is included in the code, replace the corresponding API in the code with the replacement API. The purpose of replacing the target API during the compilation of the target Android project is achieved, and the target API in the target Android project can be replaced before the installation package file of the target Android project is generated, that is, before the installation package file of the Android project is generated, the target API involving user privacy information access in the target Android project is replaced, and the inspection and modification of user privacy information access are realized before the installation package file of the target Android project is generated.
[0044] It should be understood that the magnitudes of the sequence numbers of the steps in the above embodiments do not mean the order of execution. The order of execution of each process should be determined according to its function and internal logic, and should not constitute any limitation to the implementation process of the embodiments of the present invention.
[0045] In one embodiment, an Android compilation period API replacement device 100 is provided. The Android compilation period API replacement device 100 corresponds one-to-one to the Android compilation period API replacement method in the above embodiment. As Figure 3As shown in the figure, the Android compilation - period API replacement device 100 includes a first target - file module 11, a second target - file module 12, a code - conversion tool subclass module 13, a code - compilation module 14, a first configuration - file module 15, and an API replacement module 16. The detailed description of each functional module is as follows:
[0046] The first target - file module 11 is used to add the annotations of the target API and the corresponding annotations of the replacement API to the preset first target file of the target Android project according to the preset annotation - definition rules.
[0047] The second target - file module 12 is used to add at least one target API and the corresponding replacement API to the preset second target file of the target Android project by using the annotations of the target API and the annotations of the replacement API included in the first target file.
[0048] The code - conversion tool subclass module 13 is used to create a first code - conversion tool subclass of the second target file and a second code - conversion tool subclass for API replacement processing in the target Android project. The first code - conversion tool subclass and the second code - conversion tool subclass respectively inherit the code - conversion tool class in the preset build tool.
[0049] The code - compilation module 14 is used to run the compilation command in the target Android project and compile the target Android project using the preset compilation environment.
[0050] The first configuration - file module 15 is used to scan the preset second target file by using the first code - conversion tool subclass and generate a first configuration file containing the target API and the corresponding replacement API according to the annotation - definition rules.
[0051] The API replacement module 16 is used to detect the code of the target Android project in the compilation period by using the second code - conversion tool subclass. If the code contains the target API in the first configuration file, the corresponding API in the code is replaced with the replacement API according to the preset first replacement rule.
[0052] Furthermore, the first target - file module 11 further includes:
[0053] The annotation - tool management sub - module is used to add the dependency of the annotation - tool library to the project configuration file of the target Android project and download the annotation - tool library to the target Android project.
[0054] The annotation - creation sub - module is used to create the first target file containing the annotations of the target API and the annotations of the replacement API in the target Android project by using the annotation - tool library according to the annotation - definition rules.
[0055] A reference adding sub-module is used to add a reference to the first target file in the project configuration file of the target Android project.
[0056] Furthermore, the second target file module 12 further includes:
[0057] A type verification sub-module is used to obtain the type of the target API and verify whether the type of the replacement API is the same as the type of the target API.
[0058] A first annotation sub-module is used to, if they are the same, obtain a first annotation corresponding to the type of the target API from the first target file.
[0059] A second annotation sub-module is used to obtain a second annotation corresponding to the type of the replacement API from the first target file.
[0060] A function generation sub-module is used to generate an API replacement function that uses the replacement API to replace the target API in the preset second target file by using the first annotation and the second annotation.
[0061] Furthermore, the first configuration file module 15 further includes:
[0062] A file acquisition sub-module is used to use the first code conversion tool subclass to obtain the preset second target file in the compilation period.
[0063] An annotation acquisition sub-module is used to scan the preset second target file by using the second code conversion tool subclass to obtain the annotation of the target API and the annotation of the replacement API in the second target file.
[0064] An API acquisition sub-module is used to respectively obtain the target API corresponding to the annotation of the target API and the replacement API corresponding to the annotation of the replacement API.
[0065] An association relationship sub-module is used to establish an association relationship between the target API and the corresponding replacement API and add the association relationship to the first configuration file.
[0066] Furthermore, the API replacement module 16 further includes:
[0067] A bytecode acquisition sub-module is used to use the second code conversion tool subclass to obtain the code of the target Android project that has been converted into bytecode.
[0068] The function acquisition sub-module is used to obtain each function in each class of the code of the target Android project from the bytecode by using the class accessor and function accessor included in the second code conversion tool subclass;
[0069] The API matching sub-module is used to match each function in each class with the target API in the first configuration file to obtain the successfully matched functions and the successfully matched target APIs;
[0070] The API replacement execution sub-module is used to replace the successfully matched functions with the replacement APIs corresponding to the successfully matched target APIs according to the preset first replacement rule.
[0071] Further, the API replacement execution sub-module further includes:
[0072] The judgment condition sub-unit is used to obtain the judgment condition of the successfully matched function from the code of the target Android project according to the preset first replacement rule;
[0073] The replacement execution sub-unit is used to, if the judgment condition is satisfied, replace the successfully matched function with the replacement API corresponding to the successfully matched target API;
[0074] The replacement retry sub-unit is used to, if the replacement operation of replacing the successfully matched function with the replacement API corresponding to the successfully matched target API fails, perform a retry until the replacement operation is successful, where the number of retries is equal to or greater than the preset number of repetitions.
[0075] Further, the API replacement execution sub-module further includes:
[0076] The replacement operation record sub-unit is used to generate an API replacement operation record according to the successfully matched function and the information of the replacement API that is successfully replaced or failed;
[0077] The replacement log information sub-unit is used to add the API replacement operation record to the compilation output log information of the target Android project.
[0078] The meanings of "first" and "second" in the above-mentioned modules / units are only used to distinguish different modules / units, and are not used to define which module / unit has a higher priority or other defined meanings. In addition, the terms "comprising" and "having" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device comprising a series of steps or modules does not necessarily have to be limited to those steps or modules clearly listed, but may include other steps or modules not clearly listed or inherent to these processes, methods, products or devices. The division of modules in this application is only a logical division, and there may be other division methods in actual implementation.
[0079] For the specific limitations of the Android compilation period API replacement device, reference can be made to the limitations of the Android compilation period API replacement method in the above text, which will not be elaborated here. Each module in the above Android compilation period API replacement device can be implemented in whole or in part by software, hardware and their combination. The above modules can be embedded in the processor in the computer device in hardware form or independent of the processor, or stored in the memory in the computer device in software form, so that the processor can call and execute the operations corresponding to the above modules.
[0080] In one embodiment, a computer device is provided. The computer device may be a server, and its internal structure diagram may be as Figure 4 shown. The computer device includes a processor, a memory, a network interface and a database connected by a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store the data involved in the Android compilation period API replacement method. The network interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, it implements an Android compilation period API replacement method.
[0081] In one embodiment, a computer device is provided, including a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the computer program, it implements the steps of the Android compilation period API replacement method in the above embodiment, such as Figure 2 the steps S101 to S106 shown and the extensions and related steps of the method. Or, when the processor executes the computer program, it implements the functions of each module / unit of the Android compilation period API replacement device in the above embodiment, such as Figure 3The functions of modules 11 to 16 shown. To avoid repetition, they will not be elaborated here.
[0082] The processor may be a Central Processing Unit (CPU), or may also be other general-purpose processors, Digital Signal Processors (DSPs), Application Specific Integrated Circuits (ASICs), Field-Programmable Gate Arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc. The processor is the control center of the computer device, connecting various parts of the entire computer device through various interfaces and lines.
[0083] The memory can be used to store the computer program and / or modules. The processor realizes various functions of the computer device by running or executing the computer program and / or modules stored in the memory, and by calling the data stored in the memory. The memory may mainly include a program storage area and a data storage area. Among them, the program storage area may store an operating system, application programs required for at least one function (such as a sound playback function, an image playback function, etc.); the data storage area may store data created according to the use of the mobile phone (such as audio data, video data, etc.).
[0084] The memory may be integrated in the processor or may be separately provided from the processor.
[0085] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, it implements the steps of the Android compilation period API replacement method in the above embodiment, for example Figure 2 The steps S101 to S106 shown and the extensions and related steps of the method. Or, when the computer program is executed by a processor, it implements the functions of each module / unit of the Android compilation period API replacement device in the above embodiment, for example Figure 3 The functions of modules 11 to 16 shown. To avoid repetition, they will not be elaborated here.
[0086] Those of ordinary skill in the art can understand that all or part of the processes in the methods of the above embodiments can be completed by instructing relevant hardware through a computer program. The computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above methods. Among them, any reference to a memory, storage, database, or other medium used in the various embodiments provided in the present application can include non-volatile and / or volatile memories. Non-volatile memories can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memories can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in various forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDR SDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), Rambus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and Rambus dynamic RAM (RDRAM), etc.
[0087] Those skilled in the art can clearly understand that, for the convenience and brevity of description, only the above-mentioned division of each functional unit and module is used as an example. In actual applications, the above functions can be allocated to different functional units and modules according to needs, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above.
[0088] The above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it; although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that they can still modify the technical solutions recorded in the foregoing embodiments, or perform equivalent replacements for some of the technical features; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present invention, and should all be included in the protection scope of the present invention.
Claims
1. An Android compilation - period API replacement method, characterized in that, it includes: Adding the annotations of the target API and the corresponding annotations of the replacement API to a preset first target file of the target Android project according to a preset annotation definition rule; Adding at least one target API and the corresponding replacement API to a preset second target file of the target Android project by using the annotations of the target API and the replacement API included in the first target file; Creating a first subclass of the code conversion tool for the second target file and a second subclass of the code conversion tool for API replacement processing in the target Android project, where the first subclass of the code conversion tool and the second subclass of the code conversion tool respectively inherit the code conversion tool class in a preset build tool; Running the compilation command in the target Android project and compiling the target Android project using a preset compilation environment; Using the first subclass of the code conversion tool to scan the preset second target file and generating a first configuration file containing the target API and the corresponding replacement API according to the annotation definition rule; Using the second subclass of the code conversion tool to detect the code of the target Android project during the compilation period. If the code contains the target API in the first configuration file, replacing the corresponding API in the code with the replacement API according to a preset first replacement rule.
2. The Android compilation - period API replacement method according to claim 1, characterized in that, The step of adding the annotations of the target API and the corresponding annotations of the replacement API to a preset first target file of the target Android project according to a preset annotation definition rule includes: Adding a dependency on the annotation tool library to the project configuration file of the target Android project and downloading the annotation tool library to the target Android project; In the target Android project, using the annotation tool library to create the first target file containing the annotations of the target API and the replacement API according to the annotation definition rule; Adding a reference to the first target file to the project configuration file in the target Android project.
3. The Android compilation - period API replacement method according to claim 1, characterized in that, The step of adding at least one target API and the corresponding replacement API to a preset second target file of the target Android project by using the annotations of the target API and the replacement API included in the first target file includes: Obtaining the type of the target API and verifying whether the type of the replacement API is the same as the type of the target API; If they are the same, obtaining a first annotation corresponding to the type of the target API from the first target file; Obtaining a second annotation corresponding to the type of the corresponding replacement API from the first target file; Generating an API replacement function for replacing the target API with the replacement API in the preset second target file by using the first annotation and the second annotation.
4. The Android compilation - period API replacement method according to claim 1, characterized in that, Scanning the preset second target file using the first code conversion tool subclass, and generating a first configuration file including the target API and the corresponding replacement API according to the annotation definition rule includes: Using the first code conversion tool subclass to obtain the preset second target file in the compilation period; Using the second code conversion tool subclass to scan the preset second target file, and obtaining the annotation of the target API and the annotation of the replacement API in the second target file; Respectively obtaining the target API corresponding to the annotation of the target API and the replacement API corresponding to the annotation of the replacement API; Establishing an association relationship between the target API and the corresponding replacement API, and adding the association relationship to the first configuration file.
5. The Android compilation period API replacement method according to claim 1, wherein, using the second code conversion tool subclass to detect the code of the target Android project in the compilation period, if the code contains the target API in the first configuration file, replacing the corresponding API in the code with the replacement API according to the preset first replacement rule includes: Using the second code conversion tool subclass to obtain the code of the target Android project that has been converted into bytecode; Using the class accessor and function accessor included in the second code conversion tool subclass to obtain each function in each class in the code of the target Android project from the bytecode; Matching each function in each class with the target API in the first configuration file to obtain the successfully matched function and the successfully matched target API; Replacing the successfully matched function with the replacement API corresponding to the successfully matched target API according to the preset first replacement rule.
6. The Android compilation period API replacement method according to claim 5, wherein, replacing the successfully matched function with the replacement API corresponding to the successfully matched target API according to the preset first replacement rule includes: Obtaining the judgment condition of the successfully matched function from the code of the target Android project according to the preset first replacement rule; If the judgment condition is satisfied, replacing the successfully matched function with the replacement API corresponding to the successfully matched target API; If the replacement operation of replacing the successfully matched function with the replacement API corresponding to the successfully matched target API fails, retry until the replacement operation is successful, where the number of retries is equal to or greater than the preset number of repetitions.
7. The Android compilation period API replacement method according to claim 5, wherein, after replacing the successfully matched function with the replacement API corresponding to the successfully matched target API according to the preset first replacement rule includes: Generating an API replacement operation record according to the successfully matched function and the information of the replacement API that is successfully replaced or failed; Adding the API replacement operation record to the compilation output log information of the target Android project.
8. An Android compilation period API replacement device, wherein, comprises: The first target file module is used to add the annotations of the target API and the annotations of the corresponding replacement API to the preset first target file of the target Android project according to the preset annotation definition rules; The second target file module is used to add at least one target API and the corresponding replacement API to the preset second target file of the target Android project by using the annotations of the target API and the annotations of the replacement API included in the first target file; The code conversion tool subclass module is used to create a first code conversion tool subclass of the second target file and a second code conversion tool subclass for performing API replacement processing on the code in the target Android project. The first code conversion tool subclass and the second code conversion tool subclass respectively inherit the code conversion tool class in the preset build tool; The code compilation module is used to run the compilation command in the target Android project and compile the target Android project using the preset compilation environment; The first configuration file module is used to scan the preset second target file by using the first code conversion tool subclass and generate a first configuration file including the target API and the corresponding replacement API according to the annotation definition rules; The API replacement module is used to detect the code of the target Android project in the compilation period by using the second code conversion tool subclass. If the code contains the target API in the first configuration file, the corresponding API in the code is replaced with the replacement API according to the preset first replacement rule.
9. A computer device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein, when the processor executes the computer program, the steps of the Android compilation period API replacement method according to any one of claims 1 to 7 are implemented.
10. A computer-readable storage medium storing a computer program, wherein, when the computer program is executed by a processor, the steps of the Android compilation period API replacement method according to any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Authority application method and device based on Android platform, equipment and storage medium
CN110197052A
Third-party code snippet replacement method and device, terminal and storage medium
CN111638907A