基于Android2.3.5系统:Android.mk文件解析

***************************************************************************************************************************
作者:EasyWave                                                                                                               时间:2015.01.23
类别:Android系统源码分析-基于Android2.3.5系统:Android.mk文件解析           声明:转载,请保留链接
注意:如有错误,欢迎指正。这些是我学习的日志文章......

***************************************************************************************************************************

一:Android.mk文件简单介绍

        在Linux下编辑经常要写一个Makefile文件, 可以把这个Makefile文件理解成一个编译配置文件,即指导编译器如何来编译程序,并决定编译的结果是什么。当然在Linux对于大项目而言手工编写Makefile文件是很可怕的一件事,因此出现了automake,autoconf这类工具,而在Android下的Android.mk文件也是这种类似的功能,即Android.mk文件是针对Android的Makefile的文件。你可以在每一个Android.mk file中定义一个或多个模块,你也可以在几个模块中使用同一个源代码文件。其它就交给Android的编译系统啦。


二:Android.mk的整体结构 

    Android.mk的结构,这里采用android2.3.5/frameworks/base/core/jni/Android.mk为例,如下所示:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_CFLAGS += -DHAVE_CONFIG_H -DKHTML_NO_EXCEPTIONS -DGKWQ_NO_JAVA
LOCAL_CFLAGS += -DNO_SUPPORT_JS_BINDING -DQT_NO_WHEELEVENT -DKHTML_NO_XBL
LOCAL_CFLAGS += -U__APPLE__

ifeq ($(TARGET_ARCH), arm)
	LOCAL_CFLAGS += -DPACKED="__attribute__ ((packed))"
else
	LOCAL_CFLAGS += -DPACKED=""
endif

ifeq ($(WITH_JIT),true)
	LOCAL_CFLAGS += -DWITH_JIT
endif

ifneq ($(USE_CUSTOM_RUNTIME_HEAP_MAX),)
  LOCAL_CFLAGS += -DCUSTOM_RUNTIME_HEAP_MAX=$(USE_CUSTOM_RUNTIME_HEAP_MAX)
endif

LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES -DEGL_EGLEXT_PROTOTYPES

LOCAL_SRC_FILES:= 	ActivityManager.cpp 	AndroidRuntime.cpp 	CursorWindow.cpp 	Time.cpp 	com_google_android_gles_jni_EGLImpl.cpp 	com_google_android_gles_jni_GLImpl.cpp.arm 	android_app_NativeActivity.cpp 	android_opengl_GLES10.cpp 	android_opengl_GLES10Ext.cpp 	android_opengl_GLES11.cpp 	android_opengl_GLES11Ext.cpp 	android_opengl_GLES20.cpp 	android_database_CursorWindow.cpp 	android_database_SQLiteCompiledSql.cpp 	android_database_SQLiteDebug.cpp 	android_database_SQLiteDatabase.cpp 	android_database_SQLiteProgram.cpp 	android_database_SQLiteQuery.cpp 	android_database_SQLiteStatement.cpp 	android_emoji_EmojiFactory.cpp 	android_view_Display.cpp 	android_view_Surface.cpp 	android_view_ViewRoot.cpp 	android_view_InputChannel.cpp 	android_view_InputQueue.cpp 	android_view_KeyEvent.cpp 	android_view_MotionEvent.cpp 	android_text_AndroidCharacter.cpp 	android_text_AndroidBidi.cpp 	android_text_KeyCharacterMap.cpp 	android_os_Debug.cpp 	android_os_FileUtils.cpp 	android_os_MemoryFile.cpp 	android_os_MessageQueue.cpp 	android_os_ParcelFileDescriptor.cpp 	android_os_Power.cpp 	android_os_StatFs.cpp 	android_os_SystemClock.cpp 	android_os_SystemProperties.cpp 	android_os_UEventObserver.cpp 	android_net_LocalSocketImpl.cpp 	android_net_NetUtils.cpp 	android_net_TrafficStats.cpp 	android_net_wifi_Wifi.cpp 	android_net_ethernet.cpp 	android_nio_utils.cpp 	android_nfc_NdefMessage.cpp 	android_nfc_NdefRecord.cpp 	android_pim_EventRecurrence.cpp 	android_text_format_Time.cpp 	android_security_Md5MessageDigest.cpp 	android_util_AssetManager.cpp 	android_util_Binder.cpp 	android_util_EventLog.cpp 	android_util_Log.cpp 	android_util_FloatMath.cpp 	android_util_Process.cpp 	android_util_StringBlock.cpp 	android_util_XmlBlock.cpp 	android/graphics/AutoDecodeCancel.cpp 	android/graphics/Bitmap.cpp 	android/graphics/BitmapFactory.cpp 	android/graphics/Camera.cpp 	android/graphics/Canvas.cpp 	android/graphics/ColorFilter.cpp 	android/graphics/DrawFilter.cpp 	android/graphics/CreateJavaOutputStreamAdaptor.cpp 	android/graphics/Graphics.cpp 	android/graphics/Interpolator.cpp 	android/graphics/LayerRasterizer.cpp 	android/graphics/MaskFilter.cpp 	android/graphics/Matrix.cpp 	android/graphics/Movie.cpp 	android/graphics/NinePatch.cpp 	android/graphics/NinePatchImpl.cpp 	android/graphics/Paint.cpp 	android/graphics/Path.cpp 	android/graphics/PathMeasure.cpp 	android/graphics/PathEffect.cpp 	android_graphics_PixelFormat.cpp 	android/graphics/Picture.cpp 	android/graphics/PorterDuff.cpp 	android/graphics/BitmapRegionDecoder.cpp 	android/graphics/Rasterizer.cpp 	android/graphics/Region.cpp 	android/graphics/Shader.cpp 	android/graphics/Typeface.cpp 	android/graphics/Utils.cpp 	android/graphics/Xfermode.cpp 	android/graphics/YuvToJpegEncoder.cpp 	android_media_AudioRecord.cpp 	android_media_AudioSystem.cpp 	android_media_AudioTrack.cpp 	android_media_JetPlayer.cpp 	android_media_ToneGenerator.cpp 	android_hardware_Camera.cpp 	android_hardware_SensorManager.cpp 	android_debug_JNITest.cpp 	android_util_FileObserver.cpp 	android/opengl/poly_clip.cpp.arm 	android/opengl/util.cpp.arm 	android_bluetooth_HeadsetBase.cpp 	android_bluetooth_common.cpp 	android_bluetooth_BluetoothAudioGateway.cpp 	android_bluetooth_BluetoothSocket.cpp 	android_bluetooth_ScoSocket.cpp 	android_server_BluetoothService.cpp 	android_server_BluetoothEventLoop.cpp 	android_server_BluetoothA2dpService.cpp 	android_server_Watchdog.cpp 	android_message_digest_sha1.cpp 	android_ddm_DdmHandleNativeHeap.cpp 	com_android_internal_os_ZygoteInit.cpp 	com_android_internal_graphics_NativeUtils.cpp 	android_backup_BackupDataInput.cpp 	android_backup_BackupDataOutput.cpp 	android_backup_FileBackupHelperBase.cpp 	android_backup_BackupHelperDispatcher.cpp 	android_content_res_ObbScanner.cpp     android_content_res_Configuration.cpp

LOCAL_C_INCLUDES += 	$(JNI_H_INCLUDE) 	$(LOCAL_PATH)/android/graphics 	$(call include-path-for, bluedroid) 	$(call include-path-for, libhardware)/hardware 	$(call include-path-for, libhardware_legacy)/hardware_legacy 	$(LOCAL_PATH)/../../include/ui 	$(LOCAL_PATH)/../../include/utils 	external/skia/include/core 	external/skia/include/effects 	external/skia/include/images 	external/skia/src/ports 	external/skia/include/utils 	external/sqlite/dist 	external/sqlite/android 	external/expat/lib 	external/openssl/include 	external/tremor/Tremor 	external/icu4c/i18n 	external/icu4c/common 	external/jpeg 	frameworks/opt/emoji

LOCAL_SHARED_LIBRARIES := 	libexpat 	libnativehelper 	libcutils 	libutils 	libbinder 	libnetutils 	libui 	libgui 	libsurfaceflinger_client 	libcamera_client 	libskiagl 	libskia 	libsqlite 	libdvm 	libEGL 	libGLESv1_CM 	libGLESv2 	libETC1 	libhardware 	libhardware_legacy 	libsonivox 	libcrypto 	libssl 	libicuuc 	libicui18n 	libmedia 	libwpa_client 	libjpeg 	libnfc_ndef

ifeq ($(BOARD_HAVE_BLUETOOTH),true)
LOCAL_C_INCLUDES += 	external/dbus 	system/bluetooth/bluez-clean-headers
LOCAL_CFLAGS += -DHAVE_BLUETOOTH
LOCAL_SHARED_LIBRARIES += libbluedroid libdbus
endif

ifneq ($(TARGET_SIMULATOR),true)
LOCAL_SHARED_LIBRARIES += 	libdl
  # we need to access the private Bionic header
  # <bionic_tls.h> in com_google_android_gles_jni_GLImpl.cpp
  LOCAL_CFLAGS += -I$(LOCAL_PATH)/../../../../bionic/libc/private
endif

LOCAL_LDLIBS += -lpthread -ldl

ifeq ($(TARGET_SIMULATOR),true)
ifeq ($(TARGET_OS)-$(TARGET_ARCH),linux-x86)
LOCAL_LDLIBS += -lrt
endif
endif

ifeq ($(WITH_MALLOC_LEAK_CHECK),true)
	LOCAL_CFLAGS += -DMALLOC_LEAK_CHECK
endif

LOCAL_MODULE:= libandroid_runtime

include $(BUILD_SHARED_LIBRARY)

include $(call all-makefiles-under,$(LOCAL_PATH))

      首先必须定义好LOCAL_PATH变量。然后清除所有LOCAL_XX变量的值,当然LOCAL_PATH的值除外,然后定义源文件,头文件,接着是编译选项参数,紧接着是编译生成的文件名,最后是生成的文件类型。


三:Android.mk的变量定义

 3.1:LOCAL_PATH 
一个Android.mk file首先必须定义好LOCAL_PATH变量。它用于在开发树中查找源文件。例如: 
LOCAL_PATH:= $(call my-dir)   宏函数’my-dir’, 由编译系统提供,用于返回当前路径(即包含Android.mk file文件的目录) 


3.2:include $( CLEAR_VARS) 
宏CLEAR_VARS 由编译系统提供,指定让GNU MAKEFILE为你清除许多LOCAL_XXX变量(例如 LOCAL_MODULE, LOCAL_SRC_FILES,LOCAL_STATIC_LIBRARIES, 等等...),除LOCAL_PATH 。这是必要的,因为所有的编译控制文件都在同一个GNU MAKE执行环境中,所有的变量都是全局的。 


3.3:LOCAL_SRC_FILES 
本次需要编译的源文件,如下所示:

LOCAL_SRC_FILES:= \
ActivityManager.cpp \
AndroidRuntime.cpp \
CursorWindow.cpp \
Time.cpp \ 

        以下省略.....

     
3.4:LOCAL_SHARED_LIBRARIES 
本次编译需要链接的动态链接库文件,即.so文件 


3.5:LOCAL_STATIC_LIBRARIES 
静态链接库. 


3.6:LOCAL_C_INCLUDES 
    本次编译需要包含的头文件,一个相对于当前目录可选的路径名单,当编译所有的源文件(C,C++和汇编)时,它将被添加进include搜索路径,如下所示: 
     LOCAL_C_INCLUDES := include/ui 
     或者甚至: 
     LOCAL_C_INCLUDES := $(LOCAL_PATH)/../ui 

     具体的写法 如下所示:

LOCAL_C_INCLUDES += \
$(JNI_H_INCLUDE) \
$(LOCAL_PATH)/android/graphics \
$(call include-path-for, bluedroid) \
$(call include-path-for, libhardware)/hardware \
$(call include-path-for, libhardware_legacy)/hardware_legacy \
$(LOCAL_PATH)/../../include/ui \
$(LOCAL_PATH)/../../include/utils \
external/skia/include/core \
external/skia/include/effects \
external/skia/include/images \
external/skia/src/ports \
external/skia/include/utils \
external/sqlite/dist \
external/sqlite/android \
external/expat/lib \
external/openssl/include \
external/tremor/Tremor \
external/icu4c/i18n \
external/icu4c/common \
external/jpeg \
frameworks/opt/emoji

        以下省略.....


3.6:LOCAL_LDLIBS 
本次编译的链接选项,相当于gcc -l后的参数 


3.7:LOCAL_CFLAGS 
同样是编译选项,相当于gcc -O后面的参数 


3.8:LOCAL_MODULE 
生成的模块名,这个变量必须定义,表示make后将要生成的文件的名字 

四: Android.mk文件中include用法 

     include可Android多以这样的形式出现,如:include $( CLEAR_VARS),include $(BUILD_SHARED_LIBRARY).其实这个include可以理解成"执行"的意思,那么执行什么呢?当然是看后边的宏了。 宏CLEAR_VARS已经在3.2节中介绍过了,表示清除一些变量. 宏BUILD_SHARED_LIBRARY表示生成共享库,即生成.so文件,同样类型的宏如下: 
CLEAR_VARS                                     清除LOCAL_xxx变量 
BUILD_SHARED_LIBRARY              在/system/lib/目录下生成lib$(LOCAL_MOUDULE).so文件 
BUILD_STATIC_LIBRARY                生成lib$(LOCAL_MOUDULE).a文件 
BUILD_EXECUTABLE                       在/system/bin/目录下生成可执行文件 
BUILD_PACKAGE                              编译成一个apk文件


五:总结

      简单的介绍了Android.mk的用法,还在熟悉的过程中,或多或少会出现学习过程中的错误。


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。