Detailed Android.mk

Android.mk is a kind of makefile file provided by Android, which is used to specify such as compile and generate so library name, referenced header file directory, .c/.cpp file and .a static library file to be compiled, etc. To master jni, you must be proficient in the syntax specification of Android.mk. 

1. The purpose of the Android.mk file 
There will be one or more Android.mk files in an android subproject 
1. A single Android.mk file 
directly refers to the hello-jni project in the sample directory of the NDK. There is only one in this project. Android.mk file 
2. Multiple Android.mk files 
If there are many modules to be compiled, we may place the corresponding modules in the corresponding directories, 
so that we can define the corresponding Android.mk files in each directory (similar to the above), and 
finally, place an Android.mk file in the root directory with the following content: 
include $(call all-subdir-makefiles) 
only need this line, its function is to include all subdirectories in Android.mk file 
3. Multiple modules share one Android.mk 
This file allows you to organize source files into modules, this module contains: 
  - static library (.a file) 
  - dynamic library (.so file) 
only shared library In order to be installed/copied into your application software (APK) package 
include $(BUILD_STATIC_LIBRARY), the compiled static library 
include $(BUILD_SHARED_LIBRARY), the compiled dynamic library 



2. Custom variables 
The following is a list of variables that are dependent or defined in Android.mk. You can define other variables for your own use, but the NDK compilation system reserves the following variable names: 
- With Names starting with LOCAL_ (eg LOCAL_MODULE) 
- names starting with PRIVATE_, NDK_ or APP_ (for internal use) 
- lowercase names (for internal use, eg 'my-dir') 
  if you define your own variables in Android.mk for convenience , it is recommended to 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: ':=' means assignment; '+=' means to append; '$' means to refer to the value of a variable. 



3. GNU Make System Variables 
  These GNU Make variables are defined by the build system before your Android.mk file is parsed. Note that in some cases the NDK may parse Android.mk several times, each time with a different definition of certain variables. 
  (1) CLEAR_VARS: Points to a compilation script, almost all undefined LOCAL_XXX variables are listed in the "Module-description" section. This script must be included before starting a new module: include$(CLEAR_VARS), which resets all LOCAL_XXX series variables except the LOCAL_PATH variable. 
  (2) BUILD_SHARED_LIBRARY: Points to the compilation script, which compiles the listed source code files 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 including this file. 
  (3) BUILD_STATIC_LIBRARY: A BUILD_SHARED_LIBRARY variable is used to compile a static library. Static libraries are not copied into the APK package, but can be used to compile shared libraries. 
       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: When Android.mk is parsed, the target Android platform The name. Details can be found in /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 ​​are supported, armeabi and armeabi-v7a.
  (7) TARGET_ABI: The combination of target platform and ABI, 

                               
4. Module description variables 
  The following variables are used to describe your module to the compilation 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 of the current file. 
       It must be defined at the beginning of Android.mk, you can use it like this: LOCAL_PATH := $(call my-dir) 
       If there is a folder name src in the current directory, you can write $(call src) like this, then you will get the src directory The full path to 
       this variable is not cleared by $(CLEAR_VARS), so it only needs to be defined once per Android.mk (even in cases where several modules are defined in one file). 
  (2) LOCAL_MODULE: This is the name of the module, which must be unique and cannot contain spaces. 
       It must be defined before any $(BUILD_XXXX) scripts that contain it. The name of the module determines the name of the generated file. 
  (3) LOCAL_SRC_FILES: This is a list of source code files to be compiled. 
       Just list the files to pass to the compiler, as the build system automatically calculates the dependencies. Note that the source code file names are relative to LOCAL_PATH, you can use the path part, for example: 
        LOCAL_SRC_FILES := foo.c toto/bar.c\ 
        Hello.c 
       files can be separated by spaces or tabs, please use a newline "\" 
       If you are appending source code files, please use LOCAL_SRC_FILES += 
       Note: You can include all java files in the local_path directory in the form of LOCAL_SRC_FILES := $(call all-subdir-java-files). 
  (4) LOCAL_C_INCLUDES: An optional variable that represents the search path of the header file. 
        The default search path for header files is the LOCAL_PATH directory. 
  (5) LOCAL_STATIC_LIBRARIES: Indicates which static libraries the module needs to use in order to link at compile time. 
  (6) LOCAL_SHARED_LIBRARIES: Indicates the shared library (dynamic library) that the module depends on at runtime, which is needed when linking, so that its corresponding information can be embedded when the file is generated. 
       Note: it does not append the modules listed to the compilation graph, ie you still need to add them to the modules required by the program in Application.mk. 
  (7) LOCAL_LDLIBS: Additional linker options to use when compiling modules. This is useful for passing the name of the specified library with the '-l' prefix. 
       For example, LOCAL_LDLIBS := -lz tells the linker to generate modules to link to /system/lib/libz.so at load time. 
       See docs/STABLE-APIS.TXT for open system libraries that can be linked against using the NDK distribution list. 
   (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. 
       Different file system paths are selected with the following macros: 
       TARGET_ROOT_OUT: indicates the root file system. 
       TARGET_OUT: Indicates the system file system. 
       TARGET_OUT_DATA: Indicates 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 for the time being. 
   (9) LOCAL_JNI_SHARED_LIBRARIES: defines the name of the so library file to be included. If the program does not use jni, there is no need for 
        LOCAL_JNI_SHARED_LIBRARIES := libxxx. In this way, when compiling, NDK will automatically package this libxxx into apk; put it in youapk/lib/



5. Function macros provided by  NDK 
GNU Make function macro, which must be called by using '$(call )', the return value is textual information. 
   (1) my-dir: Returns the path of the directory where the current Android.mk is located, relative to the top level of the NDK compilation system. This is useful, defined at the beginning of the Android.mk file like this: 
        LOCAL_PATH := $(call my-dir) 
   (2) all-subdir-makefiles: returns all subdirectories located in the current 'my-dir' path List of Android.mk. 
       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 a line: 
           include $( call all-subdir-makefiles) 
      then it will automatically include src/foo/lib1/Android.mk and src/foo/lib2/Android.mk. 
      This feature is used to provide deeply nested code directory hierarchies to the build system. 
      Note that by default the NDK will only search for files in src/*/Android.mk. 
   (3) this-makefile: Returns the path of the current Makefile (that is, where this function is called) 
   (4) parent-makefile: Returns the parent Makefile path in the call tree. That is, the Makefile path that contains the current Makefile. 
   (5) grand-parent-makefile: Returns the path of the parent Makefile of the parent Makefile in the call tree 



6. Android.mk example 

Java code  Favorite code

  1. #Compile static library  
  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. #Compile dynamic library  
  14. LOCAL_PATH := $(call my-dir)  
  15. include $(CLEAR_VARS)  
  16. LOCAL_MODULE = libhellod  
  17. LOCAL_CFLAGS = $(L_CFLAGS)  
  18. LOCAL_SRC_FILES = hellod.c  
  19. LOCAL_C_INCLUDES = $(INCLUDES)  
  20. LOCAL_SHARED_LIBRARIES := libcutils  
  21. LOCAL_COPY_HEADERS_TO := libhellod  
  22. LOCAL_COPY_HEADERS := hellod.h  
  23. include $(BUILD_SHARED_LIBRARY)  
  24.   
  25. # use static library  
  26. LOCAL_PATH := $(call my-dir)  
  27. include $(CLEAR_VARS)  
  28. LOCAL_MODULE := hellos  
  29. LOCAL_STATIC_LIBRARIES := libhellos  
  30. LOCAL_SHARED_LIBRARIES :=  
  31. LOCAL_LDLIBS += -ldl  
  32. LOCAL_CFLAGS := $(L_CFLAGS)  
  33. LOCAL_SRC_FILES := mains.c  
  34. LOCAL_C_INCLUDES := $(INCLUDES)  
  35. include $(BUILD_EXECUTABLE)  
  36.   
  37. # use dynamic library  
  38. LOCAL_PATH := $(call my-dir)  
  39. include $(CLEAR_VARS)  
  40. LOCAL_MODULE := hellod  
  41. LOCAL_MODULE_TAGS := debug  
  42. LOCAL_SHARED_LIBRARIES := libc libcutils libhellod  
  43. LOCAL_LDLIBS += -ldl  
  44. LOCAL_CFLAGS := $(L_CFLAGS)  
  45. LOCAL_SRC_FILES := maind.c  
  46. LOCAL_C_INCLUDES := $(INCLUDES)  
  47. include $(BUILD_EXECUTABLE)  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326151489&siteId=291194637