(1)Android.mk檔首先需要指定LOCAL_PATH變數,用於查找原始檔案。由於一般情況下

       Android.mk和需要編譯的原始檔案在同一目錄下,所以定義成如下形式:
       LOCAL_PATH:=$(call my-dir)
       上面的語句的意思是將LOCAL_PATH變數定義成本檔所在目錄路徑。

       (2)Android.mk中可以定義多個編譯模組,每個編譯模組都是以include $(CLEAR_VARS)開始以include $(BUILD_XXX)結束。

        include $(CLEAR_VARS)
        CLEAR_VARS由編譯系統提供,指定讓GNU MAKEFILE為你清除除LOCAL_PATH以外的所有LOCAL_XXX變數,
        LOCAL_MODULELOCAL_SRC_FILESLOCAL_SHARED_LIBRARIESLOCAL_STATIC_LIBRARIES等。

        include $(BUILD_STATIC_LIBRARY)表示編譯成靜態庫
        include $(BUILD_SHARED_LIBRARY)表示編譯成動態庫。
        include $(BUILD_EXECUTABLE)表示編譯成可執行程式

        (3)舉例如下(frameworks/base/libs/audioflinger/Android.mk)

java代碼:

  1. LOCAL_PATH:= $(call my-dir)
  2. include $(CLEAR_VARS) 模組一
  3. ifeq ($(AUDIO_POLICY_TEST),true)
  4. ENABLE_AUDIO_DUMP := true
  5. endif
  6. LOCAL_SRC_FILES:= \
  7. ifeq ($(ENABLE_AUDIO_DUMP),true)
  8. LOCAL_CFLAGS += -DENABLE_AUDIO_DUMP
  9. endif
  10. LOCAL_SHARED_LIBRARIES := \
  11. libcutils \
  12. libutils \
  13. libbinder \
  14. libmedia \
  15. libhardware_legacy
  16. ifeq ($(strip $(BOARD_USES_GENERIC_AUDIO)),true)
  17. LOCAL_CFLAGS += -DGENERIC_AUDIO
  18. endif
  19. LOCAL_MODULE:= libaudiointerface
  20. ifeq ($(BOARD_HAVE_BLUETOOTH),true)
  21. LOCAL_SHARED_LIBRARIES += liba2dp
  22. LOCAL_CFLAGS += -DWITH_BLUETOOTH -DWITH_A2DP
  23. LOCAL_C_INCLUDES += $(call include-path-for, bluez)
  24. endif
  25. include $(BUILD_STATIC_LIBRARY) 模組一編譯成靜態庫
  26. include $(CLEAR_VARS) 模組二
  27. LOCAL_SRC_FILES:= \
  28. LOCAL_SHARED_LIBRARIES := \
  29. libcutils \
  30. libutils \
  31. libmedia
  32. ifeq ($(TARGET_SIMULATOR),true)
  33. LOCAL_LDLIBS += -ldl
  34. else
  35. LOCAL_SHARED_LIBRARIES += libdl
  36. endif
  37. LOCAL_MODULE:= libaudiopolicybase
  38. ifeq ($(BOARD_HAVE_BLUETOOTH),true)
  39. LOCAL_CFLAGS += -DWITH_A2DP
  40. endif
  41. ifeq ($(AUDIO_POLICY_TEST),true)
  42. LOCAL_CFLAGS += -DAUDIO_POLICY_TEST
  43. endif
  44. include $(BUILD_STATIC_LIBRARY) 模組二編譯成靜態庫
  45. include $(CLEAR_VARS) 模組三
  46. LOCAL_SRC_FILES:= \
  47. LOCAL_SHARED_LIBRARIES := \
  48. libcutils \
  49. libutils \
  50. libbinder \
  51. libmedia \
  52. libhardware_legacy
  53. ifeq ($(strip $(BOARD_USES_GENERIC_AUDIO)),true)
  54. LOCAL_STATIC_LIBRARIES += libaudiointerface libaudiopolicybase
  55. LOCAL_CFLAGS += -DGENERIC_AUDIO
  56. else
  57. LOCAL_SHARED_LIBRARIES += libaudio libaudiopolicy
  58. endif
  59. ifeq ($(TARGET_SIMULATOR),true)
  60. LOCAL_LDLIBS += -ldl
  61. else
  62. LOCAL_SHARED_LIBRARIES += libdl
  63. endif
  64. LOCAL_MODULE:= libaudioflinger
  65. ifeq ($(BOARD_HAVE_BLUETOOTH),true)
  66. LOCAL_CFLAGS += -DWITH_BLUETOOTH -DWITH_A2DP
  67. LOCAL_SHARED_LIBRARIES += liba2dp
  68. endif
  69. ifeq ($(AUDIO_POLICY_TEST),true)
  70. LOCAL_CFLAGS += -DAUDIO_POLICY_TEST
  71. endif
  72. ifeq ($(TARGET_SIMULATOR),true)
  73. ifeq ($(HOST_OS),linux)
  74. LOCAL_LDLIBS += -lrt -lpthread
  75. endif
  76. endif
  77. ifeq ($(BOARD_USE_LVMX),true)
  78. LOCAL_CFLAGS += -DLVMX
  79. LOCAL_C_INCLUDES += vendor/nxp
  80. LOCAL_STATIC_LIBRARIES += liblifevibes
  81. LOCAL_SHARED_LIBRARIES += liblvmxservice
  82. # LOCAL_SHARED_LIBRARIES += liblvmxipc
  83. endif
  84. include $(BUILD_SHARED_LIBRARY) 模組三編譯成動態庫

複製代碼


   (4)編譯一個應用程式(APK)

java代碼:

  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3.  
  4. # Build all java files in the java subdirectory-->直譯(建立在java子目錄中的所有Java檔)
  5. LOCAL_SRC_FILES := $(call all-subdir-java-files)
  6.  
  7. # Name of the APK to build-->直譯(創建APK的名稱
  8. LOCAL_PACKAGE_NAME := LocalPackage
  9.  
  10. # Tell it to build an APK-->直譯(告訴它來建立一個APK
  11. include $(BUILD_PACKAGE)

複製代碼

   (5)編譯一個依賴於靜態Java(static.jar)的應用程式

java代碼:

  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3.  
  4. # List of static libraries to include in the package
  5. LOCAL_STATIC_JAVA_LIBRARIES := static-library
  6.  
  7. # Build all java files in the java subdirectory
  8. LOCAL_SRC_FILES := $(call all-subdir-java-files)
  9.  
  10. # Name of the APK to build
  11. LOCAL_PACKAGE_NAME := LocalPackage
  12.  
  13. # Tell it to build an APK
  14. include $(BUILD_PACKAGE)

複製代碼


(6)編譯一個需要用平臺的key簽名的應用程式

java代碼:

  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3.  
  4. # Build all java files in the java subdirectory
  5. LOCAL_SRC_FILES := $(call all-subdir-java-files)
  6.  
  7. # Name of the APK to build
  8. LOCAL_PACKAGE_NAME := LocalPackage
  9.  
  10. LOCAL_CERTIFICATE := platform
  11.  
  12. # Tell it to build an APK
  13. include $(BUILD_PACKAGE)

複製代碼


      (7)編譯一個需要用特定key前面的應用程式

java代碼:

  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3.  
  4. # Build all java files in the java subdirectory
  5. LOCAL_SRC_FILES := $(call all-subdir-java-files)
  6.  
  7. # Name of the APK to build
  8. LOCAL_PACKAGE_NAME := LocalPackage
  9.  
  10. LOCAL_CERTIFICATE := vendor/example/certs/app
  11.  
  12. # Tell it to build an APK
  13. include $(BUILD_PACKAGE)

複製代碼


   (8)添加一個預編譯應用程式

java代碼:

  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3.  
  4. LOCAL_MODULE := LocalModuleName
  5. LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
  6. LOCAL_MODULE_CLASS := APPS
  7. LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
  8.  
  9. include $(BUILD_PREBUILT)

複製代碼

  (9)添加一個靜態JAVA

java代碼:

  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3.  
  4. # Build all java files in the java subdirectory
  5. LOCAL_SRC_FILES := $(call all-subdir-java-files)
  6.  
  7. # Any libraries that this library depends on
  8. LOCAL_JAVA_LIBRARIES := 
  9.  
  10. # The name of the jar file to create
  11. LOCAL_MODULE := sample
  12.  
  13. include $(BUILD_STATIC_JAVA_LIBRARY)

複製代碼

  (10)Android.mk的編譯模組中間可以定義相關的編譯內容,也就是指定相關的變數如下:

java代碼:
[code]LOCAL_AAPT_FLAGS

LOCAL_ACP_UNAVAILABLE

LOCAL_ADDITIONAL_JAVA_DIR

LOCAL_AIDL_INCLUDES 

LOCAL_ALLOW_UNDEFINED_SYMBOLS

LOCAL_ARM_MODE

LOCAL_ASFLAGS

LOCAL_ASSET_DIR

LOCAL_ASSET_FILES //Android.mk檔中編譯應用程式(BUILD_PACKAGE)時設置此變數,表示資源檔案,
通常會定義成LOCAL_ASSET_FILES += $(call find-subdir-assets)

LOCAL_BUILT_MODULE_STEM 
LOCAL_C_INCLUDES //額外的C/C++編譯頭檔路徑,用LOCAL_PATH表示本檔所在目錄
舉例如下:
LOCAL_C_INCLUDES += extlibs/zlib-1.2.3
LOCAL_C_INCLUDES += $(LOCAL_PATH)/src 

LOCAL_CC //指定C編譯器

LOCAL_CERTIFICATE //簽名認證

LOCAL_CFLAGS //C/C++編譯器定義額外的標誌(如巨集定義),舉例:LOCAL_CFLAGS += -DLIBUTILS_NATIVE=1

LOCAL_CLASSPATH 

LOCAL_COMPRESS_MODULE_SYMBOLS

LOCAL_COPY_HEADERS //install應用程式時需要複製的頭檔,必須同時定義LOCAL_COPY_HEADERS_TO

LOCAL_COPY_HEADERS_TO //install應用程式時複製頭檔的目的路徑

LOCAL_CPP_EXTENSION //如果你的C++檔不是以cpp為檔尾碼,你可以通過LOCAL_CPP_EXTENSION//指定C++檔尾碼名 
如:LOCAL_CPP_EXTENSION := .cc
注意統一模組中C++檔尾碼必須保持一致。

LOCAL_CPPFLAGS //傳遞額外的標誌給C++編譯器,如:LOCAL_CPPFLAGS += -ffriend-injection

LOCAL_CXX //指定C++編譯器

LOCAL_DX_FLAGS

LOCAL_EXPORT_PACKAGE_RESOURCES

LOCAL_FORCE_STATIC_EXECUTABLE 
//如果編譯的可執行程式要進行靜態鏈結(執行時不依賴於任何動態庫),則設置LOCAL_FORCE_STATIC_EXECUTABLE:=true 
//目前只有libc有靜態庫形式,這個只有檔系統中/sbin目錄下的應用程式會用到,這個目錄下的應用程式在運行時通常
//檔系統的其他部分還沒有載入,所以必須進行靜態鏈結。

LOCAL_GENERATED_SOURCES

LOCAL_INSTRUMENTATION_FOR

LOCAL_INSTRUMENTATION_FOR_PACKAGE_NAME

LOCAL_INTERMEDIATE_SOURCES

LOCAL_INTERMEDIATE_TARGETS

LOCAL_IS_HOST_MODULE

LOCAL_JAR_MANIFEST

LOCAL_JARJAR_RULES

LOCAL_JAVA_LIBRARIES //編譯java應用程式和庫的時候指定包含的java類庫,目前有coreframework兩種
多數情況下定義成:LOCAL_JAVA_LIBRARIES := core framework
注意LOCAL_JAVA_LIBRARIES不是必須的,而且編譯APK時不允許定義(系統會自動添加)

LOCAL_JAVA_RESOURCE_DIRS 

LOCAL_JAVA_RESOURCE_FILES

LOCAL_JNI_SHARED_LIBRARIES

LOCAL_LDFLAGS //傳遞額外的參數給連接器(務必注意參數的順序)

LOCAL_LDLIBS //為可執行程式或者庫的編譯指定額外的庫,指定庫以"-lxxx"格式,舉例:
LOCAL_LDLIBS += -lcurses -lpthread
LOCAL_LDLIBS += -Wl,-z,origin 

LOCAL_MODULE //生成的模組的名稱(注意應用程式名稱用LOCAL_PACKAGE_NAME而不是LOCAL_MODULE)

LOCAL_MODULE_PATH //生成模組的路徑

LOCAL_MODULE_STEM 

LOCAL_MODULE_TAGS //生成模組的標記 

LOCAL_NO_DEFAULT_COMPILER_FLAGS 

LOCAL_NO_EMMA_COMPILE

LOCAL_NO_EMMA_INSTRUMENT

LOCAL_NO_STANDARD_LIBRARIES

LOCAL_OVERRIDES_PACKAGES

LOCAL_PACKAGE_NAME //APK應用程式的名稱

LOCAL_POST_PROCESS_COMMAND

LOCAL_PREBUILT_EXECUTABLES //預編譯including $(BUILD_PREBUILT)或者$(BUILD_HOST_PREBUILT)時所用,指定需要複製的可執行檔

LOCAL_PREBUILT_JAVA_LIBRARIES

LOCAL_PREBUILT_LIBS //預編譯including $(BUILD_PREBUILT)或者$(BUILD_HOST_PREBUILT)時所用, 指定需要複製的庫.

LOCAL_PREBUILT_OBJ_FILES

LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES 

LOCAL_PRELINK_MODULE //是否需要預連接處理(默認需要,用來做動態庫優化)

LOCAL_REQUIRED_MODULES //指定模組運行所依賴的模組(模組安裝時將會同步安裝它所

創作者介紹

尼爾's

NeilChen0710 發表在 痞客邦 PIXNET 留言(0) 人氣()