Brief analysis of Android.mk_android

Source: Internet
Author: User

ANDROID.MK Introduction:

The Android.mk file is used to inform the NDK build system of source information. ANDROID.MK will be part of the GNU makefile and will be parsed one or more times by the build system.

So, try to declare variables in ANDROID.MK as little as possible, and do not assume that nothing is defined in the parsing process.

The Android.mk file syntax allows us to package source as a "modules". Modules can be:

Static Library

Dynamic Library.

Only dynamic libraries can be install/copy to the application package (APK). The static library can be chained to the dynamic library.

You can define one or more modules in a android.mk. You can also add the same source to multiple modules.

Build system helps us deal with a lot of details without needing our attention. For example: You do not need to android.mk files and external dependencies.

NDK build system automatically helps us provide this information. This also means that when the user upgrades NDK, you will be able to benefit from the new toolchain/platform without having to modify the android.mk.

ANDROID.MK Syntax:

First look at one of the simplest android.mk examples:

Local_path: = $ (call My-dir)
include $ (clear_vars)
local_module: = Hello-jni
Local_src_files: = HELLO-JNI.C
include $ (build_shared_library)

Explained as follows:


 

Each android.mk file must start with a defined Local_path. It is used to find source files in the development tree.

The macro My-dir is provided by build system. Returns the directory path that contains the ANDROID.MK.

Include $ (clear_vars)

The Clear_vars variable is provided by build system. and point to a specified GNU Makefile, which is responsible for cleaning up a lot of local_xxx.

For example: Local_module, Local_src_files, local_static_libraries and so on. But do not clean up the local_path.

This cleanup action is necessary because all the compiled control files are parsed and executed by the same GNU make, and the variables are global. So the cleanup can avoid mutual influence.

Local_module: = Hello-jni

The Local_module module must be defined to represent each module in the ANDROID.MK. Names must be unique and contain no spaces.

The build system automatically adds the appropriate prefix and suffix. For example, Foo, to produce a dynamic library, generates libfoo.so. Note, however, that if the module name is set to: Libfoo. Then the libfoo.so is generated. No prefix is added.

Local_src_files: = Hello-jni.c

The local_src_files variable must contain C + + source code that will be packaged as a module.

Without having to list the header files, build System automatically helps us find the dependent files.

The default C + + source name extension is. cpp. can also be modified through local_cpp_extension.

Include $ (build_shared_library)

Build_shared_library: is a variable provided by build system that points to a GNU Makefile Script.

It is responsible for collecting all the local_xxx information since the last call to include $ (clear_vars). and decide what to compile.

Build_static_library: Compiled as a static library.
Build_shared_library: Compiling to Dynamic libraries
Build_executable: Compiled to native C executable program

I. Use of ANDROID.MK documents

One or more android.mk files will exist in an Android subproject

1, a single android.mk file

Refer directly to the HELLO-JNI project in NDK's sample directory, which has only one android.mk file in this project

2. Multiple android.mk files

If you need to compile more modules, we may place the corresponding modules in the corresponding directory, so that we can define the corresponding android.mk file in each directory (similar to the above), and finally, put a android.mk file in the root directory, which reads as follows:

Include $ (call All-subdir-makefiles)

You just need this line, and it's all about android.mk files in all subdirectories.

3. Multiple modules share a ANDROID.MK

This file allows you to organize your source files into modules that contain:

-Static library (. a file)
-Dynamic library (. so file)

Only shared libraries can be installed/copied into your application software (APK) package

Include $ (build_static_library), compiled with a static library
Include $ (build_shared_library), compiled with a dynamic library

Two, custom variable

The following is a list of variables that are dependent or defined in android.mk, and you can define other variables for your own use, but the NDK compilation system retains the following variable names:

-Name beginning with Local_ (e.g. Local_module)
-Name beginning with Private_, Ndk_ or APP_ (internal use)
-Lowercase name (internal use, e.g. ' My-dir ')

If you want to make it easier to define your own variables in android.mk, it is recommended that you use the My_ prefix, a small example:

My_sources: = foo.c 
ifneq ($ (My_config_bar),) 
my_sources + = bar.c 
endif 

Note: ' = ' is the meaning of the assignment; ' = = ' is the meaning of the append; ' $ ' indicates the value that refers to a variable.

Iii. GNU Make System variables

These GNU make variables are defined by the compilation system before your Android.mk file is parsed. Note that in some cases, NDK may analyze android.mk several times, and each time certain variables are defined differently.

(1) Clear_vars: Pointing to a compilation script, almost all undefined local_xxx variables are listed in the "Module-description" section. You must include this script before starting a new module: include$ (Clear_vars), which resets all LOCAL_XXX series variables except the Local_path variable.

(2) Build_shared_library: Point to the compilation script, compile the listed source code file into a shared library according to 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 into 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). A

(4) Target_arch: The name of the target CPU platform

(5) TARGET_PLATFORM:ANDROID.MK resolution, the target of the Android platform name. Details can be tested/development/ndk/docs/stable-apis.txt.

Android-3-> official Android 1.5 system images android-4->-Official 
Android 1.6 system images 

(6) Target_arch_abi: Only two Value,armeabi and armeabi-v7a are supported for the time being.

(7) Target_abi: The combination of target platform and ABI,

Four, module description variable

The following variables are used to describe your module to the compiling system. should be defined between ' include $ (clear_vars) ' and ' include $ (build_xxxxx) '. $ (clear_vars) is a script that clears all these variables.

(1) Local_path: This variable is used to give the path to the current file.

Must be defined at the beginning of the android.mk and can be used in this way: Local_path: = $ (call My-dir)

If there is a folder name src in the current directory, you can write $ (call src) so that you get the full path to the SRC directory

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: This is the name of the module, it must be unique and cannot contain spaces.

It must be defined before the $ (build_xxxx) script that contains any. The name of the module determines the name of the generated file.

(3) Local_src_files: This is the list of source code files to compile.

Just list the files to be passed to the compiler because the compilation system automatically calculates dependencies. Note that the source code file name is relative to the Local_path, you can use the path section, for example:

Local_src_files: = foo.c toto/bar.c\

hello.c

The file can be separated by a space or a TAB key, and the "\" is used for wrapping.

If you are appending source code file, please use Local_src_files + =

Note: You can local_src_files: = $ (call all-subdir-java-files) in this form to contain all the Java files in the Local_path directory.

(4) Local_c_includes: Optional variable that represents the search path for the header file.

The default header file's search path is the Local_path directory.

(5) Local_static_libraries: Indicates which static libraries the module needs to use to link at compile time.

(6) Local_shared_libraries: Represents a shared library (dynamic Library) that the module depends on at run time, and is needed when linking to embed its corresponding information when the file is generated.

Note: It does not append the listed modules to the compilation diagram, which is still needed to add them to the APPLICATION.MK in the program requirements module.

(7) Local_ldlibs: Additional linker options to use when compiling the module. This is useful for passing the name of the specified library using the '-l ' prefix.

For example, local_ldlibs: =-lz indicates that the module generated by the linker is told to link to/system/lib/libz.so at load time

You can view Docs/stable-apis. TXT gets a list of open system libraries that can be linked to using the NDK distribution.

(8) Local_module_path and Local_unstripped_path

In the Android.mk file, you can also specify the final target installation path with Local_module_path and Local_unstripped_path.

The different file system paths are selected with the following macros:
Target_root_out: Represents the root file system.
Target_out: Represents the system filesystem.
Target_out_data: Represents the DATA file system.
Usage such as: Local_module_path: =$ (target_root_out)
As for the difference between Local_module_path and Local_unstripped_path, it is not clear yet.

(9) Local_jni_shared_libraries: Defines the name of the so library file to include, if the program does not use JNI, do not need

Local_jni_shared_libraries: = Libxxx This way when compiling, NDK automatically packs this libxxx into APK, and puts it in the youapk/lib/directory.

function macros provided by NDK

The GNU make function macro must be invoked by using ' $ Call ', and the return value is textual information.

(1) My-dir: Returns the path to the directory where the current android.mk resides, relative to the top level of the NDK compilation system. This is useful as defined at the beginning of the Android.mk file:

Local_path: = $ (call My-dir)

(2) All-subdir-makefiles: Returns a list of all the android.mk in the subdirectory of the current ' my-dir ' path.

For example, the directory hierarchy for a subproject 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 will automatically contain src/foo/lib1/android.mk and src/foo/lib2/android.mk.

This feature is used to provide a deep nested code directory hierarchy to the compilation system.

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 parent makefile path in the call tree. The makefile path that contains the current makefile.

(5) Grand-parent-makefile: Returns the path to the parent makefile of the parent makefile in the call tree

Vi. android.mk Examples

#编译静态库 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: = Libhel Los Local_copy_headers: = hellos.h include $ (build_static_library) #编译动态库 Local_path: = $ (call My-dir) include $ (CLEA 
R_vars) Local_module = Libhellod Local_cflags = $ (l_cflags) Local_src_files = HELLOD.C Local_c_includes = $ (INCLUDES) Local_shared_libraries: = libcutils local_copy_headers_to: = Libhellod local_copy_headers: = hellod.h include $ (BUILD_ shared_library) #使用静态库 Local_path: = $ (call My-dir) include $ (clear_vars) Local_module: = Hellos Local_static_librari 
ES: = Libhellos local_shared_libraries: = Local_ldlibs =-LDL Local_cflags: = $ (l_cflags) Local_src_files: = MAINS.C  
Local_c_includes: = $ (INCLUDES) include $ (build_executable) #使用动态库 Local_path: = $ (call My-dir) include $ (clear_vars) Local_module: = Hellod 
Local_module_tags: = Debug Local_shared_libraries: = libc libcutils libhellod local_ldlibs + =-LDL local_cflags: = $ (  L_cflags) Local_src_files: = MAIND.C local_c_includes: = $ (INCLUDES) include $ (build_executable)

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.