关闭

如何编写Android.ml文件详解

标签: AndroidAndroid.mkmakefileinclude
772人阅读 评论(0) 收藏 举报
分类:
(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_MODULE,LOCAL_SRC_FILES,LOCAL_SHARED_LIBRARIES,LOCAL_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. AudioHardwareGeneric.cpp \
  8. AudioHardwareStub.cpp \
  9. AudioHardwareInterface.cpp
  10. ifeq ($(ENABLE_AUDIO_DUMP),true)
  11. LOCAL_SRC_FILES += AudioDumpInterface.cpp
  12. LOCAL_CFLAGS += -DENABLE_AUDIO_DUMP
  13. endif
  14. LOCAL_SHARED_LIBRARIES := \
  15. libcutils \
  16. libutils \
  17. libbinder \
  18. libmedia \
  19. libhardware_legacy
  20. ifeq ($(strip $(BOARD_USES_GENERIC_AUDIO)),true)
  21. LOCAL_CFLAGS += -DGENERIC_AUDIO
  22. endif
  23. LOCAL_MODULE:= libaudiointerface
  24. ifeq ($(BOARD_HAVE_BLUETOOTH),true)
  25. LOCAL_SRC_FILES += A2dpAudioInterface.cpp
  26. LOCAL_SHARED_LIBRARIES += liba2dp
  27. LOCAL_CFLAGS += -DWITH_BLUETOOTH -DWITH_A2DP
  28. LOCAL_C_INCLUDES += $(call include-path-for, bluez)
  29. endif
  30. include $(BUILD_STATIC_LIBRARY) 模块一编译成静态库
  31. include $(CLEAR_VARS) 模块二
  32. LOCAL_SRC_FILES:= \
  33. AudioPolicyManagerBase.cpp
  34. LOCAL_SHARED_LIBRARIES := \
  35. libcutils \
  36. libutils \
  37. libmedia
  38. ifeq ($(TARGET_SIMULATOR),true)
  39. LOCAL_LDLIBS += -ldl
  40. else
  41. LOCAL_SHARED_LIBRARIES += libdl
  42. endif
  43. LOCAL_MODULE:= libaudiopolicybase
  44. ifeq ($(BOARD_HAVE_BLUETOOTH),true)
  45. LOCAL_CFLAGS += -DWITH_A2DP
  46. endif
  47. ifeq ($(AUDIO_POLICY_TEST),true)
  48. LOCAL_CFLAGS += -DAUDIO_POLICY_TEST
  49. endif
  50. include $(BUILD_STATIC_LIBRARY) 模块二编译成静态库
  51. include $(CLEAR_VARS) 模块三
  52. LOCAL_SRC_FILES:= \
  53. AudioFlinger.cpp \
  54. AudioMixer.cpp.arm \
  55. AudioResampler.cpp.arm \
  56. AudioResamplerSinc.cpp.arm \
  57. AudioResamplerCubic.cpp.arm \
  58. AudioPolicyService.cpp
  59. LOCAL_SHARED_LIBRARIES := \
  60. libcutils \
  61. libutils \
  62. libbinder \
  63. libmedia \
  64. libhardware_legacy
  65. ifeq ($(strip $(BOARD_USES_GENERIC_AUDIO)),true)
  66. LOCAL_STATIC_LIBRARIES += libaudiointerface libaudiopolicybase
  67. LOCAL_CFLAGS += -DGENERIC_AUDIO
  68. else
  69. LOCAL_SHARED_LIBRARIES += libaudio libaudiopolicy
  70. endif
  71. ifeq ($(TARGET_SIMULATOR),true)
  72. LOCAL_LDLIBS += -ldl
  73. else
  74. LOCAL_SHARED_LIBRARIES += libdl
  75. endif
  76. LOCAL_MODULE:= libaudioflinger
  77. ifeq ($(BOARD_HAVE_BLUETOOTH),true)
  78. LOCAL_CFLAGS += -DWITH_BLUETOOTH -DWITH_A2DP
  79. LOCAL_SHARED_LIBRARIES += liba2dp
  80. endif
  81. ifeq ($(AUDIO_POLICY_TEST),true)
  82. LOCAL_CFLAGS += -DAUDIO_POLICY_TEST
  83. endif
  84. ifeq ($(TARGET_SIMULATOR),true)
  85. ifeq ($(HOST_OS),linux)
  86. LOCAL_LDLIBS += -lrt -lpthread
  87. endif
  88. endif
  89. ifeq ($(BOARD_USE_LVMX),true)
  90. LOCAL_CFLAGS += -DLVMX
  91. LOCAL_C_INCLUDES += vendor/nxp
  92. LOCAL_STATIC_LIBRARIES += liblifevibes
  93. LOCAL_SHARED_LIBRARIES += liblvmxservice
  94. # LOCAL_SHARED_LIBRARIES += liblvmxipc
  95. endif
  96. include $(BUILD_SHARED_LIBRARY) 模块三编译成动态库
复制代码

   (4)编译一个应用程序(APK)

java代码:
  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)

  3. # Build all java files in the java subdirectory-->直译(建立在java子目录中的所有Java文件)
  4. LOCAL_SRC_FILES := $(call all-subdir-java-files)

  5. # Name of the APK to build-->直译(创建APK的名称

  6. LOCAL_PACKAGE_NAME := LocalPackage

  7. # Tell it to build an APK-->直译(告诉它来建立一个APK)
  8. include $(BUILD_PACKAGE)
复制代码
   (5)编译一个依赖于静态Java库(static.jar)的应用程序

java代码:
  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)

  3. # List of static libraries to include in the package
  4. LOCAL_STATIC_JAVA_LIBRARIES := static-library

  5. # Build all java files in the java subdirectory
  6. LOCAL_SRC_FILES := $(call all-subdir-java-files)

  7. # Name of the APK to build
  8. LOCAL_PACKAGE_NAME := LocalPackage

  9. # Tell it to build an APK
  10. include $(BUILD_PACKAGE)
复制代码

(6)编译一个需要用平台的key签名的应用程序

java代码:
  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)

  3. # Build all java files in the java subdirectory
  4. LOCAL_SRC_FILES := $(call all-subdir-java-files)

  5. # Name of the APK to build
  6. LOCAL_PACKAGE_NAME := LocalPackage

  7. LOCAL_CERTIFICATE := platform

  8. # Tell it to build an APK
  9. include $(BUILD_PACKAGE)
复制代码

      (7)编译一个需要用特定key前面的应用程序

java代码:
  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)

  3. # Build all java files in the java subdirectory
  4. LOCAL_SRC_FILES := $(call all-subdir-java-files)

  5. # Name of the APK to build
  6. LOCAL_PACKAGE_NAME := LocalPackage

  7. LOCAL_CERTIFICATE := vendor/example/certs/app

  8. # Tell it to build an APK
  9. include $(BUILD_PACKAGE)
复制代码

   (8)添加一个预编译应用程序

java代码:
  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)

  3. # Module name should match apk name to be installed.
  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. include $(BUILD_PREBUILT)
复制代码
  (9)添加一个静态JAVA库

java代码:
  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)

  3. # Build all java files in the java subdirectory
  4. LOCAL_SRC_FILES := $(call all-subdir-java-files)

  5. # Any libraries that this library depends on
  6. LOCAL_JAVA_LIBRARIES := android.test.runner

  7. # The name of the jar file to create
  8. LOCAL_MODULE := sample

  9. # Build a static jar file.
  10. 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类库,目前有core和framework两种
多数情况下定义成: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 //指定模块运行所依赖的模块(模块安装时将会同步安装它所
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:203452次
    • 积分:2578
    • 等级:
    • 排名:第14821名
    • 原创:63篇
    • 转载:97篇
    • 译文:0篇
    • 评论:17条
    最新评论