Method for rapidly compiling component suitable for Andorid
By creating a cross compiler on the Openwrt platform and modifying the interpreter path on Android, the underlying dependency problem when porting complex components on Android is solved, and rapid compilation and operation are achieved, and development efficiency is improved.
Patent Information
- Application Number
- CN202510154047.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-12
- Publication Date
- 2025-05-13
AI Technical Summary
When migrating complex components based on glibc as the underlying library on Android system, it is necessary to solve the underlying dependency problem, which leads to excessive time and resource consumption, making it difficult to meet the needs of tight development resources and short application launch cycle.
Create a cross compiler with the same architecture as the Android target machine on the Openwrt platform, compile the target components or their source code, generate binary elf service files and related dependencies so files, modify the interpreter path through the patchelf tool, and install the files on the Android system for rapid compilation and operation.
Through this method, binary compilation of complex functions can be quickly implemented on Android, avoiding the difficulty of solving underlying dependencies, improving compilation efficiency, and shortening the development cycle.
Smart Images

Figure CN119987783A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of compilation technology, and in particular to a method for quickly compiling components suitable for Android. Background Art
[0002] As Android development becomes more and more popular, the number of people engaged in Android low-level development is also increasing. Since the Android App layer and FrameWork layer mostly rely on low-level technology development, developing a binary or even a service program with complex functions and many dependent components on Android cannot be separated from the dependence on the low-level library. For these components with complex functions and many functional dependencies, most of them use glibc as the low-level library. The reason is that glibc, as a C language standard library, provides a series of basic functions and system calls, covering file operations, process control, memory management, string processing, mathematical operations and other aspects. It not only provides basic C language standard library function implementation, but also contains the packaging interface of Linux system calls. It is a bridge between system hardware and application programs, and is therefore the basis for the operation of many Linux programs. It is almost relied on by all other runtime libraries, and even many source codes that can be downloaded from the public Internet use glibc as the low-level library. However, in the Android system, Bionic is the most basic C library. Compared with glibc, there are some differences in API and functions. In particular, the difference in API makes it very time-consuming and labor-intensive to port these components based on glibc as the underlying library, with complex functions and many functional dependencies to Android, and even takes a lot of time to solve the underlying dependency problems. Therefore, in order to meet the needs of tight development resources and short application launch cycles, it is very necessary to find a way to quickly implement the underlying basic services on the Android system, which can help Android customers quickly implement related functions, seize the market, and improve product competitiveness. Summary of the invention
[0003] The purpose of the present invention is to overcome the above-mentioned shortcomings of the prior art and provide a method for quickly compiling components suitable for Android that can quickly implement binary compilation of complex functions.
[0004] The present invention is achieved through the following technical solutions:
[0005] A method for quickly compiling a component suitable for Android includes the following steps:
[0006] S1. Make a cross-compiler with the same architecture as the Android target machine on the Openwrt platform, and use the made cross-compiler to compile the target component or the target source code corresponding to the target component to obtain the binary elf service file and related dependent so files;
[0007] S2. Use the patchelf tool to modify the interpreter path in the binary elf service file, generate a new binary file that can be run on the Android target machine, and push the interpreter file to the same directory as the interpreter path on the Android system;
[0008] S3. Grant the new binary file executable permissions, find the dependent so files and configure the dependent so file paths;
[0009] S4. After verifying that the new binary file can run successfully on the Android target machine, install the binary file and its dependent so files to the corresponding locations of the Android target machine through Android.mk, and run the target component.
[0010] Furthermore, in step S1, a cross-compiler with the same architecture as the Android target machine is made on the Openwrt platform, and the method of using the made cross-compiler to compile the target component or the target source code corresponding to the target component is as follows:
[0011] S1-1. On the Openwrt platform, configure the compiler architecture, the basic C library used by the compiler, and some additional features of the compiler through make menuconfig to make the compiler consistent with the architecture of the Android target machine, and then use the make tool to compile a cross-compiler and basic firmware consistent with the Android target machine architecture;
[0012] S1-2. Select the target component through make menuconfig, compile the target component separately, and obtain the corresponding compilation product elf service file and its related dependent so files; if the corresponding target component is not found on the openwrt package manager, directly download the target source code corresponding to the target component to the openwrt package directory, and write the corresponding Makefile and Config files.
[0013] Furthermore, in step S2, before modifying the linker information in the binary elf service file by using the patchelf tool, it also includes: pushing the binary elf service file to the Android target machine by using adb push to try to run it. If an execution error occurs, the cause of the error is checked by using File, and the corresponding information in the binary elf service file is modified according to the cause of the error.
[0014] Furthermore, in step S3, readelf -d is used to view the dependent so files.
[0015] Furthermore, in step S3, the method of granting executable permissions to the new binary file is: check the permissions of the binary file through ls -l, and if the file does not have executable permissions (that is, there is no x character in the permission part), use chmod to add executable permissions.
[0016] Furthermore, the architecture of the Android target machine is armv7-eabi architecture or aarch64 architecture.
[0017] Furthermore, in step S2, when the modified interpreter path is the link library path under the Android system directory, the su permission of the system directory is required, and the directory partition needs to be remounted to have read-write permissions.
[0018] The present invention compiles the target component or its source code on the Openwrt platform framework, modifies the obtained binary file, and compiles it into the Android system in a non-source code manner, so as to realize running the component on Android, avoiding the need to write Android.bp to solve the source code compilation on Android and even the need to solve the compilation problem caused by the lack of support of the underlying C library. It is fast and convenient, improves the efficiency of program compilation on Android, and shortens the development cycle. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] Figure 1 The present invention is a flow chart of the method.
[0020] Figure 2 The figure is a flow chart of a method according to an embodiment of the present invention.
[0021] Figure 3 This is a screenshot of the interface for viewing source code dependencies in an embodiment of the present invention.
[0022] Figure 4 This is a screenshot of the interface of the elf file compiled in the embodiment of the present invention.
[0023] Figure 5 This is a screenshot of the interface for viewing elf files in an embodiment of the present invention.
[0024] Figure 6 This is a screenshot of the interface for modifying the interpreter path of the elf file in an embodiment of the present invention.
[0025] Figure 7 This is a screenshot of the interface for running the elf file in an embodiment of the present invention.
[0026] Figure 8 This is a screenshot of the interface for viewing the dependencies of the elf file in an embodiment of the present invention. DETAILED DESCRIPTION
[0027] A method to quickly compile components for Android, such as Figure 1 , including the following steps:
[0028] S1. Make a cross-compiler with the same architecture as the Android target machine on the Openwrt platform, and use the made cross-compiler to compile the target component or the target source code corresponding to the target component to obtain the binary elf service file and related dependent so files.
[0029] The architecture of the Android target machine can be armv7-eabi architecture or aarch64 architecture, as well as other architectures. The architecture of the cross-compiler based on Openwrt must be the same as the architecture of the Android target machine.
[0030] To compile target codes with many dependencies, you can usually use a cross-compiler to compile the component source code and its dependent source code one by one by writing a Makefile. The present invention uses the Openwrt platform environment. Openwrt is a compilation framework. Its rich package management functions and convenient compilation methods enable it to configure cross-compilers of different architectures. Its library resources are rich, and these library dependencies are configured. When compiling a component, its dependent components will be automatically compiled together. The method for making a cross-compiler with the same architecture as the Android target machine based on the Openwrt platform and compiling it is specifically as follows:
[0031] S1-1. On the Openwrt platform, configure the compiler architecture, the basic C library used by the compiler, and some additional features of the compiler through make menuconfig to make the compiler consistent with the architecture of the Android target machine, and then use the make tool to compile a cross-compiler and basic firmware that are consistent with the Android target machine architecture. After the compiler is configured, executing make in the root working directory of Openwrt will automatically perform cross-compilation.
[0032] S1-2. Select the target component through make menuconfig, compile the target component separately, and obtain the corresponding compilation product elf service file and its related dependent so files; if the corresponding target component is not found on the openwrt package manager, directly download the target source code corresponding to the target component to the openwrt package directory, and write the corresponding Makefile and Config files.
[0033] S2. Use the patchelf tool to modify the interpreter path in the binary elf service file, generate a new binary file that can be run on the Android target machine, and push the interpreter file to the directory on the Android system that is the same as the interpreter path.
[0034] Before using the patchelf tool to modify the linker information (interpreter path) in the binary elf service file, it also includes: pushing the binary elf service file to the Android target machine through adb push and trying to run it. If an execution error occurs, check the cause of the error through File, so that corresponding modifications can be made according to the cause of the error, such as modifying the interpreter interpreter path in the binary elf service file, that is, using the patchelf tool to modify the interpreter path in the binary elf service file, and the interpreter interpreter path can be modified to the dynamic linker path of the Android target system.
[0035] Generally speaking, errors in binary elf service files are basically the following problems: the interpreter does not match, the binary has no executable permission, and other dependent so files cannot be found. Therefore, you only need to find the cause and modify it accordingly.
[0036] S3. Grant executable permissions to the new binary file, find the dependent so files, and configure the dependent so file paths.
[0037] You can use commands such as ls-l to check file permissions. If the file does not have executable permissions (that is, there is no x character in the permissions section), you need to use commands such as chmod to add executable permissions. For the dependent so files, you can use the readelf-d command to view them.
[0038] S4. After verifying that the new binary file can run successfully on the Android target machine, install the binary file and its dependent so files to the corresponding location of the Android target machine through Android.mk, and run the target component. View the dependent so library of the target binary through readelf-d, and push the corresponding so file to the Android target machine through adb. In this way, the binary with the same architecture as the Android machine can be quickly compiled.
[0039] The technical solution of the present invention is described below with a specific example in actual development. Figure 2 Specifically: compile the usb-mitm (USB middleman proxy service) service from the Openwrt system, then port it to Android, and select the arm64 architecture.
[0040] Those who are familiar with the functions of usb-mitm may know that the source code of usb-mitm depends on: libusb, cppzm, etc. Figure 3 As shown, it has many dependencies, and the basic C library it depends on is libc. Normally, if you want to implement usb-mitm on an Android device, you must find the source code of the above-mentioned dependent libraries one by one, and then change the source code to Android.mk for compilation. This is not only a huge workload, but also prone to errors, and you may encounter problems that cannot be solved. Some functions of Android-specific Bionic and libc libraries are not synchronized, and some are even missing, which makes the compilation process very difficult, time-consuming and labor-intensive.
[0041] The method steps of this embodiment are as follows:
[0042] 1. Compile usb-mitm components based on Openwrt
[0043] When Openwrt compiles the usb-mitm component, it will automatically compile its dependent components as well. The compilation process is not described here. It should be noted that the infrastructure of the cross compiler must be the same as that of the target Android machine.
[0044] 2. Modify the interpreter path in the executable binary through Patchelf
[0045] After compiling the target binary and related dependent libraries on Openwrt, push the compiled binary adbpush to the Android target machine and try to execute it, such as Figure 4 As shown, it is found that the execution will report an error. View the elf file through File, especially the file and path of the interpreter, such as Figure 5As shown, looking at usb-mitm, we can see that the reason why it cannot be executed is because there is no interpreter on the Android target machine. Figure 6 As shown, use patchelf to set the interpreter of usb-mitm and modify the interpreter path of the elf file. You can modify it to the link library path under the Android system directory, such as / system / lib or / vendor / lib, or you can modify it to other paths, such as Figure 6 in / data / .
[0046] When modifying the interpreter path, you need to push the interpreter file to the corresponding location of the Android system, that is, push the ld-linux-armhf.so.3 (interpreter file) on Openwrt to the Android / data / directory through adb ( Figure 6 The path should be the same as the path of the interpreter modified by patchelf. Of course, you can also push it to the Android system directory, such as / system / lib or / vendor / lib, but please note that if it is a system directory, you need su permission and need to remount the partition to read and write permissions.
[0047] 3. Push the modified binary and dependent so files to the corresponding locations of the Android target machine and run usb-mitm
[0048] Push the patchelf-modified usb-mitm to Android again for execution, such as Figure 7 , it is found that there is no problem with the binary itself, but the corresponding so file is missing. At this time, these missing so files can be found on Openwrt, as follows Figure 8 , use readelf -d usb-mitm to check the dependencies of the target binary, and then push the dependent so files directly to / system / lib(64) or / vendor / lib(64). Otherwise, you may need to manually export LD_LIBRARY_PATH to find the corresponding so files. Then compile them into the Android target machine in the form of files together with the patchelf-modified usb-mitm and interpreter through Android.mk.
[0049] Through the above method, it is possible to quickly run the binary usb-mitm of the same architecture compiled by other compilers on Android.
[0050] The above detailed description is a specific description of a feasible embodiment of the present invention. The embodiment is not intended to limit the patent scope of the present invention. Any equivalent implementation or modification that does not deviate from the present invention should be included in the patent scope of this case.
Claims
1. A method for quickly compiling components suitable for Android, characterized in that: The steps include: S1. Make a cross-compiler with the same architecture as the Android target machine on the Openwrt platform, and use the made cross-compiler to compile the target component or the target source code corresponding to the target component to obtain the binary elf service file and related dependent so files; S2. Use the patchelf tool to modify the interpreter path in the binary elf service file, generate a new binary file that can be run on the Android target machine, and push the interpreter file to the same directory as the interpreter path on the Android system; S3. Grant the new binary file executable permissions, find the dependent so files and configure the dependent so file paths; S4. After verifying that the new binary file can run successfully on the Android target machine, install the binary file and its dependent so files to the corresponding locations of the Android target machine through Android.mk, and run the target component.
2. A method for quickly compiling components suitable for Android according to claim 1, characterized in that: In step S1, a cross-compiler with the same architecture as the Android target machine is made on the Openwrt platform, and the method of using the made cross-compiler to compile the target component or the target source code corresponding to the target component is as follows: S1-1. On the Openwrt platform, configure the compiler architecture, the basic C library used by the compiler, and some additional features of the compiler through make menuconfig to make the compiler consistent with the architecture of the Android target machine, and then use the make tool to compile a cross-compiler and basic firmware consistent with the Android target machine architecture; S1-2. Select the target component through make menuconfig, compile the target component separately, and obtain the corresponding compilation product elf service file and its related dependent so files; if the corresponding target component is not found on the openwrt package manager, directly download the target source code corresponding to the target component to the openwrt package directory, and write the corresponding Makefile and Config files.
3. A method for quickly compiling components suitable for Android according to claim 1, characterized in that: In step S2, before modifying the linker information in the binary elf service file by using the patchelf tool, the step also includes: pushing the binary elf service file to the Android target machine by using adbpush to try to run it. If an execution error occurs, the cause of the error is checked by using File, and the corresponding information in the binary elf service file is modified according to the cause of the error.
4. A method for quickly compiling components suitable for Android according to claim 1, characterized in that: In step S3, use readelf -d to view the dependent so files.
5. A method for quickly compiling components suitable for Android according to claim 1, characterized in that: In step S3, the method of granting executable permissions to the new binary file is to check the permissions of the binary file through ls -l. If the file does not have executable permissions, use chmod to add executable permissions.
6. A method for quickly compiling components suitable for Android according to claim 1, characterized in that: The architecture of the Android target machine is armv7-eabi architecture or aarch64 architecture.
7. A method for quickly compiling components suitable for Android according to claim 1, characterized in that: In step S2, when the modified interpreter path is the link library path under the Android system directory, the su permission of the system directory is required, and the directory partition needs to be remounted to have read-write permissions.