Fourth, Android build system and custom Android platform system
4.1Android compilation system
Android's source code is composed of hundreds of thousands of files, these files are interdependent, and some are independent of each other, they are placed in different directories by function or type, for this big project, Android through its own compilation system to complete the compilation process.
4.1.1 Android Compilation System Introduction
Android and Linux, their compilation system is through the Makefile tool to organize the compilation of source code. The Makefile tool is used to interpret and execute makefile files, define the compilation rules of the project source code in the makefile file, and complete the automatic compilation of the entire project through the make command. Therefore, analyzing the makefile file is the key to understanding the compilation system.
In Android, the following major makefile files form the Android compilation system.
Figure x-x Android compilation system composition
①makefile: Compile the system's entry Makefile file, which has only one line of code, including BUILD/CORE/MAIN.MK
②BUILD/CORE/MAIN.MK: Main makefile, defines the main line of the Android compiler system
③BUILD/CORE/CONFIG.MK: Export configuration variables According to user-entered compilation options , affecting compilation targets
④BUILD/CORE/ENVSETUP.MK: Define a large number of global variables, user-compiled configuration
⑤BUILD/CORE/PRODUCT_CONFIG.MK: Define the output directory of the compiled results according to the target product selected by the user
⑥ device/*/$ (target_device)/boardconfig.mk: Find the corresponding device Target_device according to the target product selected by the user, load the device's board level configuration
⑦BUILD/CORE/DEFINITIONS.MK: Defines a number of variables and macros used in the compilation process, which is the library of the compiled system
⑧ modules_dir/android.mk: The rule definition file for each module, which appears in each directory to be compiled, as shown in X-x, we can add our own modules to the Android system to achieve the purpose of customizing the system.
Figure X-x The Android.mk file in the module
⑨build/core/makefile:android Compile the target rule definition file, and the final compilation results are defined in the file, such as System.img, Ramdisk.img, boot.img, userdata.img, etc.
4.1.2 Android.mk File
In the Android source code, a large number of sources in accordance with the function of the directory classification, the same functionality is usually compiled into a target file, the target file contains not only the executable C + + application, but also the dynamic library, static library, Java class Library, Android application, etc. In the Android compilation system, each compiled target file is called a module, in the source directory of each module must create a android.mk file as a compilation rule, These android.mk files are included in the compile-time findleaves.py script in the compiled system.
@build/core/main.mk
489 Subdir_makefiles: = \
490 $ (Shell build/tools/findleaves.py--prune=out--prune=.repo--prune=.git$ (subdirs) android.mk)
491
492 include $ (subdir_makefiles)
Note: findleaves.py scripts compiled by Python, Python is a highly efficient object-oriented script that means returning android.mk files in the Subdirs directory, but skipping out,. Reop,. Git directories.
Usually when compiling a module, the compiler needs to know the following:
Ø what files are compiled? (Specify source directory and source file)
Ø What compiler parameters are required by the compiler?
Ø what library or header files are required for compiling?
Ø how to compile? ( compiled into a dynamic library, a static library, a binary program, an Android app, or a Java library?) )
Ø Compile target
The syntax of ANDROID.MK differs from that of the MAKEFILE,ANDROID.MK syntax, where the user simply defines some of the compiled variables in android.mk, and the Android compilation system compiles according to the values of the variables in the Android.mk file.
For example, the ANDROID.MK in the zygote process App_process module is shown in the following code:
@ frameworks/base/cmds/app_process/android.mk
1local_path:= $ (call My-dir) #指定源码目录
2include $ (clear_vars) #包含清除编译变量的mk文件 to prevent impact on this compilation
3
4local_src_files:= \ #指定被编译源码
5 App_main.cpp
6
7local_shared_libraries: = \ #指定编译Zygote时用到的其它动态库
8 libcutils \
9 libutils \
Ten Libbinder \
Libandroid_runtime
12
13local_module:= app_process #指定被编译模块的名字
14
15include $ (build_executable) #指定编译方式, compiled into executable program
Another example is the android.mk in the camera application:
@ packages/apps/camera/android.mk
1local_path:= $ (call My-dir) #指定源码目录
2include $ (clear_vars) #包含清除编译变量的mk文件 to prevent impact on this compilation
3
4local_module_tags: = Optional #指定应用程序标签
5
6local_src_files: = $ (call All-java-files-under, SRC) #指定被编译源码
7
8local_package_name: = Camera #指定Android应用程序名
9local_sdk_version: = Current #指定该应用程序依赖的SDK版本
10
11local_proguard_flag_files: = Proguard.flags #指定混淆编译配置文件
12
13include $ (build_package) #指定模块编译方式, compiled here into an Android app
14
# usethe following include to make our test apk.
16include $ (call all-makefiles-under,$ (Local_path)) # contains the Android.mk file in the subdirectory of the current directory, compiled down
As you can see from the above two examples, the ANDROID.MK file structure is simple, and the Android.mk file for each module must do the following:
Ø Specify the directory of the current module
Get the current module directory by calling the $ (call My-dir) command package (some makefile collections).
Ø Clear all the local_xx variables
Use the include command to include the Clear_vars.mk file to clear all the local_xx variables to prevent the results from being affected, CLEAR_VARS.MK files are defined by the variable Clear_vars
Ø Designated source file
Through the local_src_files variable to specify the source code file, for C + + files, they are all listed to assign to Local_src_files (see the program code above), for Java code, you can call the command package $ ( Callall-java-files-under, SRC), it will look up all the Java files in the SRC directory and list them.
Ø Specify compilation details
You may need to modify compiler parameters at compile time, need to link to other libraries, and need to compile details such as header files under other paths.
Ø Specify the target module name
In the case of a C + + library, executable program, or Java class Library, specify the final compiled module name via Local_module and, if it is an Android application, by the Local_package_name variable.
Ø Specify the target module type
The modules are eventually compiled, with the include command containing some predefined variables to specify the final type of the module, which corresponds to a makefile file that contains the compilation process for the module type. The main predefined compilation variables are as follows:
Compiling variables |
Function |
Build_shared_library |
Compiling a module into a shared library |
Build_static_library |
Compiling a module into a static library |
Build_executable |
Compiling the module into an executable file |
Build_java_library |
Compiling a module into a Java class library |
Build_package |
Compiling the module into an Android app package |
Note: The above compiler variables are defined in BUILD/CORE/DEFINITIONS.MK.
In Android.mk, the main compilation variables are shown in the following table:
Compiling variables |
Function |
Local_path |
specifying the compilation Path |
Local_module |
Specifies the name of the compiled module |
Local_src_files |
Specify compiled source list |
Local_shared_libraries |
Specifies the list of shared libraries that are used by C + + |
Local_static_libraries |
Specifies the list of C/+ + static libraries used |
Local_static_java_libraries |
Specifies the list of Java libraries to use |
Local_cflags |
specifying compiler parameters |
Local_c_includes |
Specify the C + + header file path |
Local_package_name |
Specify the Android app name |
Local_certificate |
Specify signature authentication |
Local_java_libraries |
Specifies the list of Java libraries to use |
Local_sdk_version |
Specifies the SDK version when compiling an android application |
Note: Additional compilation variables are shown in the appendix.
4.1.3 Experiment: Compiling HelloWorld applications
"Experimental Content"
Use the Eclipse development environment to write simple HelloWorld applications in Ubuntu systems, then compile using the Android compilation system, and eventually integrate the HelloWorld application into the Android system as a system application.
"Experimental Purpose"
Through the experiment, the learner compiles Android applications, libraries, executable programs in the Android source compilation system, understands the customization process of the Android system application, and eventually runs its own Android application compiled by compiling the system in the Android emulator.
"Experimental Platform"
Ubuntu os with Android source (can be virtual Ubuntu system in Windows System).
"Experimental Steps"
1. Open the Eclipse development environment and create an Android application: HelloWorld:
$ CD ~/android/eclipse
$./eclipse &
2. Copy the newly created HelloWorld project to the Packages/apps directory in the source directory:
$ CP-RF Helloworld/~/android/android_source/packages/apps
Delete files and directories automatically generated by the Eclipse development Environment in the HelloWorld project directory, preserving only the project directory structure shown in X-x:
3. Compiling the Android.mk file for the HelloWorld project, we can emulate the Android.mk file of the app that comes with Android, such as the Android.mk file in the camera project:
Copy the Android.mk file from the camera project to the HelloWorld project:
$ CP.. /camera/android.mk./
Modify the Android.mk file to remove unnecessary compilation variables:
Local_path:= $ (call My-dir)
Include $ (clear_vars)
Local_module_tags: = Optional
Local_src_files: = $ (call all-java-files-under,src)
Local_package_name: = HelloWorld
Local_sdk_version: = Current
Include $ (build_package)
4. Compiling HelloWorld project:
Ø Switch to the Android source directory:
$ CD ~/android/android_source/
Ø load the compilation function:
$ source Build/envsetup.sh
Ø Select the compile target:
$ Lunch Generic-eng
Ø compile HelloWorld project by MMM command:
$ mmm packages/apps/helloworld/
Ø Compile Build simulator image system.img:
$ make Snod
Note: We can also compile the HelloWorld project directly from the Make command and generate the system.img image file, but it takes a long time, so we use the above compilation method, can save the experimental time, about the Android source code compilation details, please see 2.3.2 compiled Android chapter.
5. Start the emulator to see how the HelloWorld application works:
$./run_emulator.sh
Note: run_emulator.sh is a quick script to run the simulator, please see the 2.5 Custom Android Simulator section for details.
In layman's-Android system porting and Platform development (10)-Android build system with custom Android platform system (Mad Fuzi Modified chapter II)