Android. MK is a MAKEFILE file provided by Android. It is used to specify the so library name, the referenced header file directory, and the files to be compiled. c /. CPP file and. A static library files. To master JNI, you must be familiar with the syntax of Android. mk.
I. Android. mk File Usage
One or more Android. mk files exist in an android sub-project.
1. A single Android. mk File
Directly refer to the hello-JNI project under the ndk sample directory. There is only one android. mk file in this project.
2. Multiple Android. mk files
If many modules need to be compiled, we may place the corresponding modules in the corresponding directory,
In this way, we can define the corresponding Android. mk file in each directory (similar to the above Code ),
Finally, put an android. mk file in the root directory. The content is as follows:
Include $ (call all-subdir-makefiles)
You only need this line. Its function is to include the Android. mk file in all subdirectories.
3. Multiple modules share one android. mk
This file allows you to organize source files into modules, which contain:
-Static library (. A file)
-Dynamic library (. So file)
Only the shared library can be installed/copied to your application package.
Include $ (build_static_library). The compiled static library
Include $ (build_shared_library). The dynamic library is compiled.
Ii. Custom Variables
The following is a list of variables that are dependent on or defined in Android. mk. Other variables can be defined for your own use, but the ndk compilation system retains the following variable names:
-Name starting with local _ (for example, local_module)
-Name starting with private _, ndk _, or app _ (used internally)
-Lowercase name (for internal use, for example, 'My-dir ')
To define your own variables in Android. mk, we recommend that you use the my _ prefix. A small example:
My_sources: = Foo. c
Ifneq ($ (my_config_bar ),)
My_sources + = bar. c
Endif
Local_src_files + = $ (my_sources)
Note: ': =' indicates the value assignment. '+ =' indicates the append object. '$' indicates the value of a variable.
Iii. GNU make System Variables
These GNU make variables are defined by the compilation system before parsing your android. mk file. Note that in some cases, ndk may analyze Android. mk several times, and the definitions of some variables may vary.
(1) clear_vars:Point to a compilation script. Almost all undefined local_xxx variables are listed in "module-description. The script include $ (clear_vars) must be included before starting a new module to reset all local_xxx series variables except the local_path variable.
(2) build_shared_library:Point to the compilation script and compile the listed source code files into a shared library based on all the local_xxx variables.
Note that local_module and local_src_files must be defined at least before the file is included.
(3) build_static_library:A build_shared_library variable is used to compile a static library. The static library is not copied to the APK package, but can be used to compile the shared library.
Example: include $ (build_static_library)
Note that this will generate a file named Lib $ (local_module).
(4) target_arch: name of the target CPU Platform
(5) target_platform: name of the target Android platform when parsing Android. mk. For details, refer to/development/ndk/docs/stable-apis.txt.
Android-3-> official Android 1.5 System Images
Android-4-> official Android 1.6 System Images
Android-5-> official Android 2.0 System Images
(6) target_arch_abi:Currently only two values, armeabi and armeabi-v7a are supported ..
(7) target_abi: A combination of the Target Platform and Abi,
Iv. Module description Variables
The following variables are used to describe your module to the compilation system. It should be defined in 'includeBetween $ (clear_vars) 'and 'include $ (build_xxxxx. $ (Clear_vars) is a script that clears all these variables.
(1) local_path:This variable is used to show the path of the current file.
It must be defined at the beginning of Android. mk. You can use: local_path: = $ (call my-DIR)
If the current directory has a folder named SRC, you can write $ (call SRC) in this way, then the complete path of the src directory will be obtained.
This variable is not cleared by $ (clear_vars), so each android. mk only needs to be defined once (even if several modules are defined in a file ).
(2) local_module: the name of the module. It must be unique and cannot contain spaces.
It must be defined before it contains any $ (build_xxxx) script. The module name determines the name of the generated file.
(3) local_src_files:
This is the list of source code files to be compiled.
You only need to list the files to be passed to the compiler because the compilation system automatically calculates dependencies. Note that the source code file names are relative to local_path. You can use the path section, for example:
Local_src_files: = Foo. c toto/bar. c \
Hello. c
Files can be separated by spaces or tabs. Use "\" for line breaks "\"
If the source code file is appended, use local_src_files + =
Note: You can use local_src_files :=$ (call all-subdir-Java-files) to include all java files in the local_path directory.
(4) local_c_includes:Optional variable, indicating the search path of the header file.
The default search path for the header file is the local_path directory.
(5) local_static_libraries: indicates the static libraries used by the module for link during compilation.
(6) local_shared_libraries:Indicates the Shared Library (dynamic library) that the module depends on when running. It is required during the link so that the corresponding information is embedded when the file is generated.
Note: It does not append the listed modules to the compilation graph, that is, it still needs to add them to the module required by the program in application. mk.
(7) local_ldlibs:The additional linker option used to compile the module. This is useful for passing the name of the specified database using the '-l' prefix.
For example, local_ldlibs: =-LZ indicates that the module generated by the linker should be linked to/system/lib/libz. So at the time of loading.
You can view the docs/STABLE-APIS.TXT to get a list of open system libraries that can be linked using the ndk release.
(8) local_module_path and local_unstripped_path
In the Android. mk file, you can also use local_module_path and local_unstripped_path to specify the final target installation path.
Use the following macro to select different file system paths:
Target_root_out: indicates the root file system.
Target_out: indicates the system file system.
Target_out_data: indicates the data file system.
Usage example: local_module_path: = $ (target_root_out)
The difference between local_module_path and local_unstripped_path is unknown.
(9) local_jni_shared_libraries: defines the name of The so library file to be included. If the program does not use JNI, no
Local_jni_shared_libraries: = libxxx during compilation, ndk will automatically package libxxx into the APK; Put It In The youapk/lib/directory.
5. Function macros provided by ndk
The GNU make FUNCTION macro must be used by '$ (call.
(1) My-Dir: return the path of the directory where the current Android. mk is located, relative to the top layer of the ndk compilation system. This is useful. It is defined at the beginning of the android. mk file:
Local_path: = $ (call my-DIR)
(2) All-subdir-makefiles: returns a list Of all android. mk files in the subdirectory of the current 'my-dir' path.
For example, the directory hierarchy of a subitem is as follows:
Src/Foo/Android. mk
Src/Foo/lib1/Android. mk
Src/Foo/lib2/Android. mk
If src/Foo/Android. mk contains one line:
Include $ (call all-subdir-makefiles)
Then it automatically contains src/Foo/lib1/Android. mk and src/Foo/lib2/Android. mk.
This function provides the compilation system with a deep nested code directory hierarchy.
Note that by default, ndk will only search for files in src/*/Android. mk.
(3) This-makefile:Returns the path of the current makefile (where this function is called)
(4) parent-makefile:Returns the path of the parent makefile in the call tree. That is, the makefile path that contains the current makefile.
(5) Grand-parent-makefile: return the path of the parent makefile in the call tree.
6. Android. mk example
# Compile a static library
Local_path: = $ (call my-DIR)
Include $ (clear_vars)
Local_module = libhellos
Local_cflags = $ (l_cflags)
Local_src_files = hellos. c
Local_c_includes = $ (includes)
Local_shared_libraries: = libcutils
Local_copy_headers_to: = libhellos
Local_copy_headers: = hellos. h
Include $ (build_static_library)
# Compile a dynamic library
Local_path: = $ (call my-DIR)
Include $ (clear_vars)
Local_module = libhellevels
Local_cflags = $ (l_cflags)
Local_src_files = hellevels. c
Local_c_includes = $ (includes)
Local_shared_libraries: = libcutils
Local_copy_headers_to: = libhellevels
Local_copy_headers: = hellevels. h
Include $ (build_shared_library)
# Using static libraries
Local_path: = $ (call my-DIR)
Include $ (clear_vars)
Local_module: = hellos
Local_static_libraries: = libhellos
Local_shared_libraries: =
Local_ldlibs + =-LDL
Local_cflags: = $ (l_cflags)
Local_src_files: = mains. c
Local_c_includes: = $ (includes)
Include $ (build_executable)
# Use a dynamic library
Local_path: = $ (call my-DIR)
Include $ (clear_vars)
Local_module: = hellevels
Local_module_tags: = debug
Local_shared_libraries: = libc libcutils libhelfine
Local_ldlibs + =-LDL
Local_cflags: = $ (l_cflags)
Local_src_files: = maind. c
Local_c_includes: = $ (includes)
Include $ (build_executable)
What do Android. mk assign values to the variables and use ": =" and "=" at the same time?
": =" Means that if the value on the right is a variable, it can only use the variable defined before this statement, rather than the variable defined after this statement;
"=". When the value on the right is a variable, the definition of this variable can be done before or after this statement;
You are welcome to repost this article.
Author: meteor
Source: http://blog.sina.com.cn/staratsky