ANDROID.MK Usage Explanation

Source: Internet
Author: User

I. Introduction of ANDROID.MK

ANDROID.MK is an makefile file provided by Android that specifies such things as compiling generated so library names, referenced header file directories,. c/.cpp files that need to be compiled, and. A static library files. To master the JNI, we must master Android.mk's grammatical norm.

Its basic format is as follows:

[CPP]View Plaincopy print?
    1. Local_path: = $ (call My-dir)
    2. Include $ (clear_vars)
    3. ................
    4. Local_xxx: = xxx
    5. Local_module: = Hello-jni
    6. Local_src_files: = Hello-jni.c
    7. Local_xxx: = xxx
    8. ................
    9. Include $ (build_shared_library)


The Local_path variable developed the path of the. MK, $ (call My-dir) calls the NDK internal function to get the path to the current. mk File

Include $ (clear_vars) clears the value of all local_xxx variables except Local_path

The middle of the ellipsis is the setting of module parameters, including: module name, module source file, module type, compiled module storage location, and compiled platform, etc.

Include $ (build_xxx_xxx) executes the NDK default script, which collects all defined local_xxx variables after the include $ (clear_vars) script, and then builds the module based on them.

Second, the ANDROID.MK grammar explanation

Local_path: = $ (call My-dir)
Each android.mk file must start with a definition of local_path. It is used to find source files in the development tree. Macro My-dir is provided by the build system. Returns the directory path that contains the ANDROID.MK.


Include $ (clear_vars)
The Clear_vars variable is provided by the build system. and points to a specified GNU Makefile, which is responsible for cleaning up many local_xxx.
For example: Local_module, Local_src_files, local_static_libraries and so on. But do not clean local_path.
This cleanup action is necessary because all compiled control files are parsed and executed by the same GNU make, and their variables are global. So we can avoid the mutual influence after cleaning.


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 prefixes and suffixes. For example, Foo, to produce a dynamic library, generates libfoo.so. Note, however, that if the module name is set to: Libfoo. Generates libfoo.so. No more prefixes


Local_module_path: =$ (target_root_out) specifies the destination address of the last generated module

Target_root_out: Root file system, path is Out/target/product/generic/root

Target_out:system file system, path is Out/target/product/generic/system

Target_out_data:data file system, path is Out/target/product/generic/data

In addition to the above, the NDK also provides a number of other target_xxx_xxx variables that are used to copy the generated modules to different paths in the output directory

Default is Target_out

Local_src_files: = Hello-jni.c

The local_src_files variable must contain a/C + + source code that will be packaged as a module. Instead of listing header files, the build System automatically helps us identify dependent files. The default C + + source extension is. cpp. can also be modified, by local_cpp_extension


Include $ (build_shared_library)
Build_shared_library: is a variable provided by the build system that points to a GNU Makefile Script.
It is responsible for collecting all local_xxx information since the last call to include $ (clear_vars). and decide what to compile.

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

Build_prebuilt: The module is pre-compiled

The NDK also defines a number of other build_xxx_xxx variables, which are used to specify how the module is generated.

Iii. Examples of Use

[CPP]View Plaincopy print?
  1. #编译静态库
  2. Local_path: = $ (call My-dir)
  3. Include $ (clear_vars)
  4. Local_module = Libhellos
  5. Local_cflags = $ (l_cflags)
  6. Local_src_files = HELLOS.C
  7. Local_c_includes = $ (includes)
  8. Local_shared_libraries: = Libcutils
  9. Local_copy_headers_to: = Libhellos
  10. Local_copy_headers: = Hellos.h
  11. Include $ (build_static_library)
  12. #编译动态库
  13. Local_path: = $ (call My-dir)
  14. Include $ (clear_vars)
  15. Local_module = Libhellod
  16. Local_cflags = $ (l_cflags)
  17. Local_src_files = HELLOD.C
  18. Local_c_includes = $ (includes)
  19. Local_shared_libraries: = Libcutils
  20. Local_copy_headers_to: = Libhellod
  21. Local_copy_headers: = Hellod.h
  22. Include $ (build_shared_library)
  23. #使用静态库
  24. Local_path: = $ (call My-dir)
  25. Include $ (clear_vars)
  26. Local_module: = hellos
  27. Local_static_libraries: = Libhellos
  28. Local_shared_libraries: =
  29. Local_ldlibs + =-ldl
  30. Local_cflags: = $ (l_cflags)
  31. Local_src_files: = MAINS.C
  32. Local_c_includes: = $ (includes)
  33. Include $ (build_executable)
  34. #使用动态库
  35. Local_path: = $ (call My-dir)
  36. Include $ (clear_vars)
  37. Local_module: = Hellod
  38. Local_module_tags: = Debug
  39. Local_shared_libraries: = libc libcutils Libhellod
  40. Local_ldlibs + =-ldl
  41. Local_cflags: = $ (l_cflags)
  42. Local_src_files: = MAIND.C
  43. Local_c_includes: = $ (includes)
  44. Include $ (build_executable)
  45. #拷贝文件到指定目录
  46. Local_path: = $ (call My-dir)
  47. Include $ (clear_vars)
  48. Local_module: = bt_vendor.conf
  49. Local_module_class: = ETC
  50. Local_module_path: = $ (target_out)/etc/bluetooth
  51. Local_module_tags: = Eng
  52. Local_src_files: = $ (Local_module)
  53. Include $ (build_prebuilt)
  54. #拷贝动态库到指定目录
  55. Local_path: = $ (call My-dir)
  56. Include $ (clear_vars)
  57. #the data or Lib want to copy
  58. Local_module: = libxxx.so
  59. Local_module_class: = shared_libraries
  60. Local_module_path: = $ (android_out_shared_libraries)
  61. Local_src_files: = lib/$ (Local_module)
  62. Override_build_module_path: = $ (target_out_intermediate_libraries)
  63. Include $ (build_prebuilt)


Reference article:

Http://www.cnblogs.com/wainiwann/p/3837936.html

http://blog.csdn.net/ly131420/article/details/9619269

http://blog.csdn.net/mawl2002/article/details/6118522

ANDROID.MK Usage Explanation

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.