A detailed explanation of the Android.mk file _c language in an Android system

Source: Internet
Author: User

Overview
The Android.mk file is used to describe how to compile your source code to the compilation system. Rather, the file is actually a small makefile. Because the file is parsed several times by the NDK compilation tool, you should minimize the declaration of variables in the source code because these variables may be defined multiple times to affect subsequent parsing. The syntax of this file allows the source code to be organized into modules, each of which belongs to one of the following types:

APK Program: General Android program, compile package to generate APK files.
Java Library: Java class Library, compiled and packaged to generate jar package files.
C\c++ application: An executable C + + application.
c\c++ Static Library: Compiles production of C/b + + static libraries and packs them into. a files.
C\c++ shared libraries: Compile and build shared libraries, packaged as. So files, and only shared libraries can be installed/copied into the APK package.


Example
Here is a general example of the Internet, compiling a simple "Hello world" to illustrate the android.mk writing. For example, the following file:
1. sources/test/hello.c
2. Sources/test/android.mk
where "hello.c" is a JNI shared library that implements the native method of returning the "Hello World" string. Therefore, the contents of the Android.mk file are as follows:

  Local_path: = $ (call My-dir) 
  include $ (clear_vars) 
  local_module: = Hello 
  local_src_files: = hello.c 
  Include $ (build_shared_library) 

Explain these lines of code:
1. Local_path: = $ (call My-dir): A android.mk file must first define a Local_path variable to find the source file in the development tree. In this example, the macro function My-dir is provided by the compilation system 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 I system, specifying that the GNU makefile clear many local_*** variables except the Local_path variable (for example: Local_module, Local_ Src_files, etc.). This is very necessary because all of the compiled files are in the same gun mkae execution environment, all variables are global variables, and no cleanup can cause parsing errors.
3. Local_module: = Hello:local_module variable must be defined to identify each module described in the Android.mk file. The name must be unique and cannot contain spaces. The compile system automatically produces the appropriate prefix and suffix, such as a shared library module named Hello, which generates libhello.so files. If you name the library Libhello, the compilation system will not add any lib prefixes and generate libhello.so files.
4. Local_src_files: = hello.c:local_src_files variable must contain the source code file that will be compiled into the module.
5. Include $ (build_shared_library): Build_shared_library is a compiler-supplied variable that points to a GNU Makefile script (which should be the Build/core directory Shared_ LIBRARY.MK), responsible for collecting all the information defined in the local_*** variable since the last call to include $ (clear_vars), and deciding what to compile, how to do it correctly, and generate a static library according to its rules.
6. Explain the ANDROID.MK variable definition character ": =". ": =" is similar to a macro in C that is explicitly expanded at the definition and is completely replaced with text.

Module Description Variable
The following variable is used to describe our own module to the system, which should be defined between include $ (clear_vars) and include $ (build_***). As mentioned earlier, $ (clear_vars) is a script that clears all of these variables unless indicated in the description.
1. Local_path: This variable is used to give the path to the current file, which must be defined at the beginning of the android.mk, so that it can be used: local_path: = $ (call My-dir) so that this variable will not be cleared by $ (clear_vars). Because each android.mk only needs to be defined once (even if multiple modules are defined in a file).
2. Local_src_files: All source code files contained in the current module.
3. Local_module: The name of the current module, which should be unique and cannot contain spaces. The dependencies between modules are referred to by this name.
4. Local_module_class: Identifies the location where the compiled module was last placed. The ETC representation is placed in the/system/etc. directory where the apps representation is placed under the/system/app directory, and the shared_libraries representation is placed under the/system/lib directory. If specified, the compiled module is not placed in the compilation system, and finally in the corresponding directory in the obj directory of the out corresponding product.
5. Local_src_files: This is the list of source code files to compile. As long as you list the files that you want to pass to the compiler, the compilation system automatically calculates the dependencies. The source code file path is relative to Local_path, so you can use a relative path to describe it.
6. Local_java_libraries: The current module relies on the Java shared library, also known as the Java Dynamic Library. such as Framework.jar package.
7. Local_static_java_libraries: The current module relies on the Java Static library, in Android, the imported jar package and referenced Third-party projects are owned by the Java Static Library.
8. Local_static_libraries: The name of the static library that the current module relies on at run time.
9. Local_shared_libraries: The name of the dynamic library that the current module relies on at run time.
The path of the header file required by the LOCAL_C_INCLUDES:C or C + + language.
Local_cflags: Additional compilation parameters provided to the C + + compiler.
Local_package_name: The name of the current APK application.
Local_certificate: Sign the name of the currently applied certificate.
Local_module_tags: The tag contained in the current module, a module can contain multiple labels. The value of the label may be Eng, user, Debug, development, optional. Where optional is the default label.
The Odex optimization switch for local_dex_preopt:apk is false by default.

    In addition, some functions are defined in the build system to facilitate use in android.mk, including:
1. $ (call My-dir): Gets the path to the current folder.
2. $ (call All-java-files-under <src>): Gets all the Java files in the specified directory.
3. $ (call All-c-files-under <src>): Gets all C files in the specified directory.
4. $ (call All-iaidl-files-under <src>): Gets all aidl files in the specified directory.
5. $ (Call All-makefiles-under <folder>): Gets all make files in the specified directory.
6. $ (call intermediates-dir-for, <class>, <app_name>,

Related Article

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.