A android.mk file is used to describe your source code to the compilation system. Specifically: The file is a small part of the GNU makefile, which is parsed one or more times by the compiled system. You can define one or more modules in each of the Android.mk file. Each module belongs to one of the following types:
1) apk program, General Android program, compile package build apk file
2) Java library, Java Class Library, compile package build jar file
3) c\c++ application, executable c\c++ application
4) c\c++ Static library, compile and build c\c++ static library, and package as. a file
5) C\c++ shared libraries, compile and build shared libraries (dynamic link libraries), and package them as. So, and only shared libraries can be installed/copied into your application (APK) package.
(1) Let's look at a simple example: a simple "Hello World", such as the following file:
Sources/helloworld/helloworld.c
Sources/helloworld/android.mk
The corresponding android.mk file will look like this:
----------Cut here------------------
[CPP]View PlainCopy
- local_path := $ (call my-dir)
- include $ (Clear_vars)
- LOCAL_MODULE  
- : = helloworld
- LOCAL_SRC_FILES := HELLOWORLD.C  
- include $ ( build_shared_library)
---------Cut here------------------
Let's explain these lines of code:
1,local_path: = $ (call My-dir), a android.mk file must first define a good local_path variable. It is used to find source files in the development tree. In this example, the macro function ' My-dir ', provided by the compilation system, is used to return the current path (that is, the directory containing the android.mk file).
2,include $ (clear_vars), Clear_vars is provided by the compilation system (the/build/core/config.mk file can be seen in the Android installation directory for its definition, clear_vars:= $ (build_ SYSTEM) (/CLEAR_VARS.MK)), specifies that the GNU makefile for you to clear many local_xxx variables (such as Local_module, Local_src_files, Local_static_libraries, etc...), except Local_path. This is necessary because all the compilation control files are in the same GNU make execution environment, and all the variables are global.
3,local_module: = Helloworld,local_module variables must be defined to identify each module that you describe in the Android.mk file. The name must be unique. Note that the compilation system automatically generates the appropriate prefix and suffix, in other words, a shared library module named ' foo ' will generate a ' libfoo.so ' file (which can also be named directly libxxx).
4,local_src_files: = helloworld.c,local_src_files variable must contain C or C + + source code files that will be compiled into the module. Note that you do not have to list header files and include files here, because the compilation system will automatically find the dependent files for you; Just list the source code files that are passed directly to the compiler.
There is no use for other common use here:
5,local_c_includes: Optional variable that represents the search path for the header file. The default header file search path is the Local_path directory. Example: local_c_includes: = Sources/foo or Local_c_includes: = $ (local_path)/... /foo
6,target_arch: The name of the target CPU platform; TARGET_PLATFORM:Android.mk the name of the target Android platform at the time of parsing, Target_arch_abi: temporarily supports only two Value,armeabi and armeabi-v7a
7,local_static_libraries: Indicates which static libraries the module needs to use in order to link at compile time.
8,local_shared_libraries: Represents a shared library (dynamic Library) that the module relies on at run time, which is required at link time to embed its corresponding information when the file is generated.
9,local_ldlibs: Additional linker options to use when compiling the module.
10,local_arm_mode: By default, the arm target binary is generated as a thumb (16 bits) and you can set this variable to arm if you want your module to be in the form of a 32-bit instruction
11,local_cflags: Optional compiler option to use when compiling C code files
12,include $ (call All-subdir-makefiles): Returns a list of all android.mk in the subdirectory of the current ' my-dir ' path.
(2) to generate native programs or libraries in Android, these programs and libraries have no relation to their path, only their android.mk files. Android.mk and ordinary makefile different, it has a unified writing, mainly contains some of the system public macros. Multiple executable programs, dynamic libraries, and static libraries can be generated in a single android.mk.
A, compile the templates for C + + applications:
#Test Exe
Local_path: = $ (call My-dir)
Include $ (clear_vars)
local_src_files:= MAIN.C
local_module:= Test_exe
#LOCAL_C_INCLUDES: =
#LOCAL_STATIC_LIBRARIES: =
#LOCAL_SHARED_LIBRARIES: =
Include $ (build_executable)
The build_executable represents the compilation as an executable program. Supplemental Note: The Include $ (build_package) is compiled out of a Apk,include $ (build_static_java_library) is compiled out of the jar package.
B, compile the template for the static library:
#Test Static Lib
Local_path: = $ (call My-dir)
Include $ (clear_vars)
local_src_files:=/
Helloworld.c
local_module:= libtest_static
#LOCAL_C_INCLUDES: =
#LOCAL_STATIC_LIBRARIES: =
#LOCAL_SHARED_LIBRARIES: =
Include $ (build_static_library)
In general, similar to the above, Build_static_library represents compiling a static library. a file. Static libraries are not copied to the APK package, but can be used to compile shared libraries.
C, compile the template for the dynamic library:
#Test Shared Lib
Local_path: = $ (call My-dir)
Include $ (clear_vars)
local_src_files:=/
Helloworld.c
local_module:= libtest_shared
Target_prelink_modules: = False
#LOCAL_C_INCLUDES: =
#LOCAL_STATIC_LIBRARIES: =
#LOCAL_SHARED_LIBRARIES: =
Include $ (build_shared_library)
In general, similar to the above, build_shared_library means compiling a dynamic library.
The results of the above three are as follows, generic depending on the target will change:
Out/target/product/generic/obj/executable
Out/target/product/generic/obj/static_library
Out/target/product/generic/obj/shared_library
The target folders for each module are:
Executable Program: Xxx_intermediates
Static Library: Xxx_static_intermediates
Dynamic Library: Xxx_shared_intermediates
(3) In addition, in the Android.mk file, you can also specify the final target installation path, specified with Local_module_path and Local_unstripped_path. Different file system paths are selected with the following macros:
Target_root_out: Represents the root file system out/target/product/xxxxx/root.
Target_out: Represents the system filesystem out/target/product/xxxx/system.
Target_out_data: Represents the DATA file system out/target/product/xxxx/data.
Target_out_shared_libraries: Indicates Out/target/product/xxxx/system/lib
Target_out_apps: Indicates Out/target/product/xxxx/system/app
android_product_out:out/target/product/xxxx/
Target_out_java_libraries:out/target/product/xxxx/system/framework
================================================================================================
Android Source directory under the build/envsetup.sh file that describes the compiled commands
-M:makes from the top of the tree.
-Mm:builds all of the modules in the current directory.
-Mmm:builds all of the modules in the supplied directories.
So to use these commands, you first need to execute the build/envsetup.sh script Setup environment in the Android source root directory.
M: Compiling all the modules
MM: Compile the current directory of the module, the current directory to have android.mk file
MMM: Compile the module under the specified path, specify the path to the Android.mk file
Here is an example, let's say I want to compile the \hardware\libhardware_legacy\power module under Android, the current directory is the source root directory, the method is as follows:
1,. build/envsetup.sh
2, MMM hardware/libhardware_legacy/power/
Or:
1,. build/envsetup.sh
2. CD hardware/libhardware_legacy/power/
3, MM
M never tried. By default, these two compile commands compile only the files that have changed. If you want to compile all the files for a module, you need the-B option, such as Mm-b or mmm-b
The make command can also be used to compile. If it is include $ (build_package), use make Local_package_name value, if include $ (build_executable) or include $ (build_java_library ), with make Local_module value (not verified).
Reference Original: http://hubingforever.blog.163.com/blog/static/171040579201152185542166/
Reference Original: http://blog.163.com/zz_forward/blog/static/212898222201442873435471/
The usage and foundation of ANDROID.MK