1. 程式人生 > >ndk的系統構建

ndk的系統構建

hello 包括 新的 包含 res -a swscale 源代碼 add

1構建多個共享庫 為了建立可供住應用程序使用的模塊,必須將該模塊變為共享庫。Android ndk構建系統將BUILD_SHARE_LIBRARY變量設置成 build-shared-library.mk文件位置。該makefile片段包含了將源文件構建和組裝成共享庫的必要過程: include $(BUILD_SHARE_LIBRARY ) 上一篇中,就是一個簡單的模塊;然而,除非模塊需要特殊處理,否則Android.mk文檔將包含一模一樣的流程和指令。 2構建多個共享庫 基於不同的應用程序的體系結構,一個單獨的Android.mk文檔可能產生多個共享庫模塊。為了達到這個目的,需要如下程序清單Android.mk文檔中定義多個模塊。 LOCAL_PATH := $(call my-dir) # #模塊1 # include $(CLEAR_VARS) LOCAL_C_INCLUDES += $(LOCAL_PATH)/include \ $(LOCAL_PATH)/src LOCAL_SRC_FILES := src/hello-jni.c LOCAL_MODULE := NativeLib include $(BUILD_SHARED_LIBRARY) # # 模塊2 # include $(CLEAR_VARS) LOCAL_C_INCLUDES += $(LOCAL_PATH)/include \ $(LOCAL_PATH)/src LOCAL_SRC_FILES := src/hello-jni2.c LOCAL_MODULE := NativeLib2 include $(BUILD_SHARED_LIBRARY) 重新編譯構建文檔之後,Android NDK構建系統會產生兩個定義的共享庫 技術分享
3構建靜態庫 Android NDK構建系統也支持靜態庫。實際的Android應用程序並不直接使用靜態庫,並且應用程序包中也不包含靜態庫。靜態庫可以用來構建共享庫。例如,在將第三方代碼添加到現有的原生項目中,不用直接將第三方源代碼包括在原生項目中,而是將第三方代碼編譯成靜態庫然後並入共享庫,如下面程序。 LOCAL_PATH := $(call my-dir) # # 第三方庫 # include $(CLEAR_VARS) LOCAL_C_INCLUDES += $(LOCAL_PATH)/include \ $(LOCAL_PATH)/src LOCAL_SRC_FILES := src/staticlib.c LOCAL_MODULE := StaticLib include $(BUILD_STATIC_LIBRARY) # # 目標庫 # include $(CLEAR_VARS) LOCAL_C_INCLUDES += $(LOCAL_PATH)/include \ $(LOCAL_PATH)/src LOCAL_SRC_FILES := src/hello-jni.c LOCAL_MODULE := NativeLib LOCAL_STATIC_LIBRARIES := StaticLib include $(BUILD_SHARED_LIBRARY) 將第三方代碼模塊生成模塊靜態庫之後,共享庫就可以通過將它的模塊名稱添加到LOCAL_STATIC_LIBRARIES變量中來使用該模塊 4,用共享庫共享通用模塊 靜態庫可以保證源代碼模塊化;但是當靜態庫與共享庫相連接時,他就變成了共同共享庫的一部分。在多個共享庫的情況下,多個共享庫與同一個靜態庫連接時,需要將通用模塊的多個副本與不同共享庫重復相連,這樣就增加了應用程序的大小,在這種情況下,不同構建靜態庫,而是將通用模塊作為共享庫建立起來,而動態庫連接依賴模塊以便消除重復的副本。 LOCAL_PATH := $(call my-dir) # # 第三方庫 # include $(CLEAR_VARS) LOCAL_C_INCLUDES += $(LOCAL_PATH)/include \ $(LOCAL_PATH)/src LOCAL_SRC_FILES := src/staticlib.c LOCAL_MODULE := StaticLib include $(BUILD_SHARED_LIBRARY) # # 目標庫 # include $(CLEAR_VARS) LOCAL_C_INCLUDES += $(LOCAL_PATH)/include \ $(LOCAL_PATH)/src LOCAL_SRC_FILES := src/hello-jni.c LOCAL_MODULE := NativeLib LOCAL_SHARED_LIBRARIES := StaticLib include $(BUILD_SHARED_LIBRARY) # # 目標庫 # include $(CLEAR_VARS) LOCAL_C_INCLUDES += $(LOCAL_PATH)/include \ $(LOCAL_PATH)/src LOCAL_SRC_FILES := src/hello-jni2.c LOCAL_MODULE := NativeLib2 LOCAL_SHARED_LIBRARIES := StaticLib include $(BUILD_SHARED_LIBRARY) 5.在多個ndk項目間共享模塊 同時使用靜態庫和共享庫時,可以在模塊間共享通用模塊,但要說明的所有的模塊必須數據屬於同一個NDk項目,從R5開始,android NDK 也允許在NDK項目間共享和重建模塊。考慮前面講過的實例,可以通過以下步驟在過多個ndk項目間共享。 作為共享模塊,需要自己的Android.mk文件 LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_C_INCLUDES += $(LOCAL_PATH)/include \ $(LOCAL_PATH)/src LOCAL_SRC_FILES := src/hello-jni.c LOCAL_MODULE := NativeLib #LOCAL_SHARED_LIBRARIES :=StaticLib include $(BUILD_SHARED_LIBRARY) $(call import-module,test/StaticLib) 將原先模塊移動到項目外面,將import-modules函數宏調用放在Android.mk文檔的尾部。 import-modules 函數需要先定位共享模塊,然後再將它導入到NDK項目中,默認情況下,,import-modules函數宏值搜索<Android NDK>/source 目錄中,為了搜索 共享模塊目錄,頂一個名為NDK_MODULE_PATH的新的環境變量並將它設置為共享模塊的根目錄, 6 用prebuild庫 使用共享模塊要求有共享模塊的源代碼,Android NDK構建系統簡單的把這些資源文件包含在NDK項目中並每次構建它們,Android NDK也提供對Prebuild庫的支持。在下面的情況,Prebuild庫是非常有用的
  • 想在不發布源代碼的情況下將模塊發布給別人
  • 想使用共享模塊的預建版來加速構建過程。
盡管已經被編譯了,但預建模塊仍需要一個Android.mk構建文檔,如下面編譯ffmpeg LOCAL_PATH := $(call my-dir) # prepare libX include $(CLEAR_VARS) TARGET_ARCH_ABI := armeabi-v7a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_MODULE := avcodec LOCAL_SRC_FILES := libavcodec.a include $(PREBUILT_STATIC_LIBRARY) # prepare libX include $(CLEAR_VARS) TARGET_ARCH_ABI := armeabi-v7a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_MODULE := avfilter LOCAL_SRC_FILES := libavfilter.a include $(PREBUILT_STATIC_LIBRARY) # prepare libX include $(CLEAR_VARS) TARGET_ARCH_ABI := armeabi-v7a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_MODULE := avformat LOCAL_SRC_FILES := libavformat.a include $(PREBUILT_STATIC_LIBRARY) # prepare libX include $(CLEAR_VARS) TARGET_ARCH_ABI := armeabi-v7a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_MODULE := avutil LOCAL_SRC_FILES := libavutil.a include $(PREBUILT_STATIC_LIBRARY) # prepare libX include $(CLEAR_VARS) TARGET_ARCH_ABI := armeabi-v7a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_MODULE := postproc LOCAL_SRC_FILES := libpostproc.a include $(PREBUILT_STATIC_LIBRARY) # prepare libX include $(CLEAR_VARS) TARGET_ARCH_ABI := armeabi-v7a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_MODULE := swresample LOCAL_SRC_FILES := libswresample.a include $(PREBUILT_STATIC_LIBRARY) # prepare libX include $(CLEAR_VARS) TARGET_ARCH_ABI := armeabi-v7a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_MODULE := swscale LOCAL_SRC_FILES := libswscale.a include $(PREBUILT_STATIC_LIBRARY) # prepare libX include $(CLEAR_VARS) TARGET_ARCH_ABI := armeabi-v7a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_MODULE := x264 LOCAL_SRC_FILES := libx264.a include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) TARGET_ARCH_ABI := armeabi-v7a LOCAL_MODULE := live_jni LOCAL_SRC_FILES := live_jni.c LOCAL_C_INCLUDES += $(LOCAL_PATH)/include LOCAL_CFLAGS := -D__STDC_CONSTANT_MACROS -Wno-sign-compare -Wno-switch -Wno-pointer-sign -DHAVE_NEON=1 -mfpu=neon -mfloat-abi=softfp -fPIC -DANDROID LOCAL_STATIC_LIBRARIES := avfilter avformat avcodec postproc swresample swscale avutil x264 LOCAL_LDLIBS := -L$(NDK_ROOT)/platforms/$(APP_PLATFORM)/arch-arm/usr/lib -L$(LOCAL_PATH) -llog -ljnigraphics -lz -ldl include $(BUILD_SHARED_LIBRARY) LOCAL_SRC_FILES 變量指向的不是源文件,而是實際prebuilt庫相對於LOCAL_PATH的位置。 prebuilt庫定義中不包含任何關於該庫所構建的實際機器體系結構的信息。開發人員需要確保prebuilt庫是為與NDK項目相同的及其體系結構而構建的。 PREBUILT_STATIC_LIBRARY 變量指向prebuilt-shared-library.mk makefile片段。它什麽都沒構建,只是將prebuilt庫復制到NDK項目的libs目錄下。通過使PREBUILT_STATIC_LIBRARY 變量,靜態庫可以像共享庫一樣被用作PreBuil庫,NDK項目可以像普通共享庫一樣使用PreBuilt庫了

ndk的系統構建