Android. mk File Usage and basics

Source: Internet
Author: User

An Android. mk file is used to describe your source code to the compilation system. Specifically, this file is a small part of GNU Makefile and will be parsed once or multiple times by the compilation system. You can define one or more modules in each Android. mk file. You can also use the same source code file in several modules. The compilation system handles many details for you. For example, you do not need to list header files and dependent files in your Android. mk file. The NDK compilation system automatically handles these problems for you. This also means that after you upgrade the NDK, you should get the new toolchain/platform support and do not need to change your Android. mk file.
Let's take a look at a simple example: A simple "hello world", such as the following file:
Sources/helloworld. c
Sources/helloworld/Android. mk
The corresponding Android. mk file will look like the following:
---------- Cut here ------------------
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:
LOCAL_PATH: = $ (call my-dir)
An Android. mk file must first define the LOCAL_PATH variable. It is used to search for source files in the Development tree. In this example, the macro function 'my-dir' is provided by the compilation system and used to return the current path (that is, the directory containing the Android. mk file ).
Include $ (CLEAR_VARS)
CLEAR_VARS is provided by the compilation system, specifying that gnu makefile clears many LOCAL_XXX variables for you (for example, 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.
LOCAL_MODULE: = helloworld
The LOCAL_MODULE variable must be defined to identify each module you describe in the Android. mk file. The name must be unique and contain no spaces. Note that the compilation system automatically generates the appropriate prefix and suffix. In other words, a shared library module named 'foo' will generate the 'libfoo. so' file.
LOCAL_SRC_FILES: = helloworld. c
The LOCAL_SRC_FILES variable must contain the C or C ++ source code files to be compiled and packaged into the module. Note that you do not need to list header files and contained files here, because the compilation system will automatically find the dependent files for you; just list the source code files directly transmitted to the compiler.

Add local programs or libraries in Android. These programs and libraries have nothing to do with their paths, but only with their Android. mk files. Android. mk is different from a general Makefile. It has a unified Writing Method and mainly contains some common macros of the system.
Multiple executable programs, dynamic libraries, and static libraries can be generated in an Android. mk file.
1. Compile the Application Template:
# 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)
(Cainiao-level explanation: = is the meaning of the value assignment, $ is to reference the value of a variable.) Add the source file path to LOCAL_SRC_FILES and add the header file path to local_c_pair des, LOCAL_STATIC_LIBRARIES is added to the static library to be linked (*. a) Name. Add the dynamic library (*. so) name. LOCAL_MODULE indicates the final name of the module, and BUILD_EXECUTABLE indicates compiling in an executable program.
2. Compile the static library template:
# 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, it is similar to the above. BUILD_STATIC_LIBRARY indicates compiling a static library.
3. Compile the dynamic library template:
# 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, it is similar to the above. BUILD_SHARED_LIBRARY indicates compiling a static library.
The above three generation results are as follows, and the generic changes according to the specific target:
Out/target/product/generic/obj/EXECUTABLE
Out/target/product/generic/obj/STATIC_LIBRARY
Out/target/product/generic/obj/SHARED_LIBRARY
The target folders of each module are:
Executable program: XXX_intermediates
Static Library: XXX_static_intermediates
Dynamic library: XXX_shared_intermediates
In addition, in the Android. mk file, you can specify the final target installation path, which is specified using LOCAL_MODULE_PATH and LOCAL_UNSTRIPPED_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:
CAL_MODULE_PATH: = $ (TARGET_ROOT_OUT)

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.