Details Android compiler command _android

Source: Internet
Author: User

工欲善其事, its prerequisite, for want to delve into the Android source, you must first master the Android compiler command.

First, the introduction

On the Android build system, this topic is very early to be sorted out, and the delay has not been writing, decided to share with you. Read the following instructions first, I believe that the people who have compiled the Android code are familiar with.

source/opt/android1204_17.conf 
source setenv.sh
Lunch
make-j12

I remember when I first contacted Android, my colleague told me to use the above instructions to compile the Android source code, although the instructions are short but a few days to remember incomplete or forget the order, each compile also need to look at their own cloud notes, cold instructions are always difficult to let me remember. Then I decided to study the meaning of the instruction carefully. Know it but still need to know why, this can be a deeper understanding and memory, in order to establish a strong connection with their own knowledge system, and perhaps there are unexpected gains, and indeed, then share with you in the study of the meaning of the above several instructions in the process, in-depth understanding of the Android Build (compiler) system.

Second, compile the command

When you are ready to compile the environment, the first step to compiling the Android code is source build/envsetup.sh, where the source command is used to run the shell Script command, which is equivalent to ".", so the command is equivalent to. Build/envsetup.sh. In file envsetup.sh declares the commands available to the current session terminal, it is important to note that the current session terminal also means that the instructions must be executed again each time a new terminal is opened. At first I did not understand why the new terminal could not execute the make instruction directly, and came here to understand.

Next, explain the command referenced at the beginning of this article:

SOURCE/OPT/ANDROID1204_17.CONF//initialization of JDK environment variables (this is not required, varies by vendor)
SOURCE setenv.sh//initialization of the compilation environment, including subsequent lunch and make directives
Lunch//Specifies the target device for this compilation and the type of compilation
MAKE-J12//Start compiling, the default is to compile the entire system, where-J12 represents a compiled job number of 12.
All compiler commands in the envsetup.sh file can find corresponding function, such as the above command Lunch,make, in the file will be able to find

function Lunch () {
  ...
}

function make () {
  ...
}

Concrete implementation here does not expand the description, the following refining summarized the use of each instruction and efficacy.

2.1 Code compilation

Compiler instruction Interpretation
m in the root directory of the source tree to execute the compilation
MM compiles all modules under the current path, but does not contain dependencies
mmm [Module_path] compiles all modules under the specified path, but does not contain dependencies
MMA compiles all modules under the current path and contains dependencies
Mmma [Module_path] compiles all modules under the specified path and contains dependent
Make [module_name] has no parameters, it means compiling the entire Android code
The following is an enumeration of the compiler directives for some modules:

Module make command MMM command
init make init mmm system/core/init
Zygote make app_process mmm frameworks/base/cmds/app_process
System_server Make Services mmm frameworks/base/services
Java framework make framework MMM frameworks/base
Framework resources make Framework-res MMM frameworks/base/core/res
JNI framework make Libandroid_runtime mmm Frameworks/base/core/jni
Binder make Libbinder mmm frameworks/native/libs/binder

The MMM command is also applicable to Mm/mma/mmma, which uses incremental compilation and compiles only the object files that have changed. When you need to recompile all of the related modules, you need to compile the command to increment the parameter-B, such as Make-b [Module_name], or Mm-b [Module_path].

Tips:

For m, MM, MMM, MMA, mmma the implementation of these commands is done by make way.
The efficiency of MMM/MM compilation is very high, while Make/mma/mmma compilation is slow;
Make/mma/mmma compiles all the dependent modules together, but mmm/mm does not;

Recommendation: Compile with Make/mma/mmma at first compile, and compile with mmm/mm when dependent modules have been compiled.

2.2 Code Search

Search instruction Explanation
Perform a search operation on all Cgrep/C + + files
Jgrep all Java files to perform search operations
Ggrep all gradle files to perform a search operation
mangrep [keyword] all androidmanifest.xml files perform a search operation
sepgrep [keyword] all sepolicy files perform a search operation
resgrep [keyword] all local res/*.xml files perform a search operation
sgrep [keyword] All resource files perform a search operation

The final implementation of the instruction usage is based on the grep instruction, each instruction usage format:

xgrep [keyword]//x represents the search instruction on the table above.
For example, search for the exact location of the file where the Launcher keyword is located in all androidmanifest.xml files, instruction

MANGREP Launcher
Again, search for all System_app selinux permission information

Sepgrep System_app
Tips:android source is very large, directly using grep to search for code, not only the method clumsy, waste time, and search out a lot of meaningless confusing results. According to the specific needs, to select the appropriate code search instructions, can save code search time, improve the accuracy of search results, easy to locate the target code.

2.3 Navigation Instructions

Navigation instruction Interpretation
Croot switch to the Android root directory
Cproj switch to the root directory of the project
Godir [filename] jumps to the directory containing a file
Tips: When a file needs to be compiled after each modification, the execution of cproj jumps to the root directory of the current module. That is, the directory of ANDROID.MK files, and then execute the MM instruction, you can compile the target module; When you go deep into the source hierarchy, you need to return to the root directory, use a croot command, and the CD-directive can be used to quickly switch to the last directory.

2.4 Information Inquiry

Query instruction Explanation
Hmm query for all instruction help information
Findmakefile Query the Android.mk file path for the project where the current directory resides
Print_lunch_menu Query Lunch Optional product
Printconfig query each compilation variable value
GetTop Query the root directory of Android source
Gettargetarch Get Target_arch value
Tips: When you forget all of the previous instructions, you can perform a hmm to output the help information for these instructions.

Other instructions:

Make clean: Performs cleanup operations, equivalent to RM-RF out/
Make UPDATE-API: Update API, execute the instruction after the framework API changes, API record in directory Frameworks/base/api;

Third, compile the system

The Android build system is part of the Android code that compiles the Android system, the Android SDK, and related documentation. The compilation system is composed of make files, shells, and Python scripts, the most important of which is made files. About the compilation system can refer to the Android build system.

3.1 Makefile Classification

The make file for the entire build system is divided into three broad categories:

System core Make file: Defines a build system framework, all files are located in the path/build/core, other make files are based on the framework written;
Make file for the product: defines a specific type of mobile phone make file, the file path is located in/device, the directory is often the company name and product name divided into two subcategories, such as/device/qcom/msm8916;
Make file for module: The whole system is divided into individual modules, each module is a special make file, the name is unified as "Android.mk", the file defines the current module of the compilation method. The build system scans the entire source tree for a problem named "Android.mk" and performs the compilation of the corresponding module.

3.2 Compilation Products

The products compiled by make are located in the/out directory, which focuses on the following directories:

/out/host:android development tool products, including the SDK tools, such as ADB,DEX2OAT,AAPT.
/out/target/common: A common compilation of some products, including Java application code and Java library;
/out/target/product/[product_name]: For a specific device of the compiler products and platform-related C + + code and binary files;
In the/out/target/product/[product_name] directory, there are several heavyweight mirrored files:

System.img: Mount as root partition, mainly contains the system files of the Android OS;
RAMDISK.IMG: mainly include init.rc files and configuration files;
USERDATA.IMG: Is mounted in/data, mainly contains user and application-related data;
Of course, there are boot.img,reocovery.img, such as mirror files, here is not introduced.

3.3 Android.mk Analysis

In the source tree, all files for each module usually have a folder of their own, with a name called "Android.mk" in the root directory of the module.
of the file. Compiling system is a module for the compilation of units, each module has a unique module name, a module can rely on a number of other modules, the dependencies between the modules are referred to by the module name. That is, when a module needs to rely on a jar package or APK, it must first define a jar package or apk as a module, and then rely on the corresponding module.

For android.mk files, the following two lines are usually

Local_path: = $ (call My-dir)//Set when compiling path for current folder path
include $ (clear_vars)//Empty compilation environment variables (variables set by other modules)

To facilitate the compilation of modules, the compilation system set up a number of compilation environment variables, as follows:

Local_src_files: All source files contained in the current module;
Local_module: The name of the current module (unique);
Local_package_name: The name of the current APK application (unique);
Local_c_includes:c/c++ the required header file path;
Local_static_libraries: The library name required by the current module in the static link;
Local_shared_libraries: The dynamic library name that the current module relies on at run time;
Local_static_java_libraries: The JAVA Static library that the current module relies on;
Local_java_libraries: The JAVA shared library that the current module relies on;
Local_certificate: Sign the name of the currently applied certificate, such as Flatform.
Local_module_tags: The current module contains labels that can contain multiple labels, possibly values of debgu,eng,user,development or optional (default)

For these environment variables, the compilation system also defines some convenient functions, as follows:

$ (call My-dir): Gets the current folder path;
$ (call All-java-files-under <src>): Gets all Java files in the specified directory;
$ (call All-c-files-under <src>): Get all C files in the specified directory;
$ (call All-iaidl-files-under <src>): Get all aidl files in the specified directory;
$ (call All-makefiles-under <folder>): Gets all make files in the specified directory;
Example:

 Local_path: = $ (call My-dir) 
 include $ (clear_vars) 
  
 # Gets the Java files in all subdirectories
 local_src_files: = $ (call All-subdir-java-files) 
  
 # The current module relies on the dynamic Java library name
 local_java_libraries: = Com.gityuan.lib 
  
 # The name of the current module
 Local_ Module: = Demo 
  
 # compiles the current module into a static JAVA library
 include $ (build_static_java_library)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.