关于android:Android开发实践Androidmk模板

48次阅读

共计 2216 个字符,预计需要花费 6 分钟才能阅读完成。

对于 Android NDK 开发的文章曾经比拟多了,我的博客中也分享了很多 NDK 开发相干教训和技巧,明天简略写了一个 Android.mk 的示例模板,供初学者参考。

本模板次要给大家示例 Android NDK 开发中的如下几个问题:

如何主动增加须要编译的源文件列表

如何增加第三方动态库、动静库的依赖

如何结构一个残缺的 NDK 工程框架

假如咱们的我的项目依赖 libmath.a, libjson.a, libffmpeg.so 这几个第三方库文件,我的项目蕴含如下几个模块:algorithm,core,network,utils,tests,每个模块都有若干个.c/.h 文件,咱们要把整个工程编译为动静库,并通过 native_sdk.c 中的函数向 Java 层提供调用接口。

那么,咱们能够对 Android 工程的 jni 目录,做如下划分:

对应的 Android.mk 文件的写法示例如下

LOCAL\_PATH := $(call my-dir)
3RD\_INC\_DIR = $(LOCAL\_PATH)/3rd/inc
3RD\_LIB\_DIR = $(LOCAL\_PATH)/3rd/libs

Prebuild the 3rd libraries

include $(CLEAR\_VARS)
LOCAL\_MODULE := math
LOCAL\_SRC\_FILES := $(3RD\_LIB\_DIR)/libmath.a
include $(PREBUILT\_STATIC\_LIBRARY)

include $(CLEAR\_VARS)
LOCAL\_MODULE := json
LOCAL\_SRC\_FILES := $(3RD\_LIB\_DIR)/libjson.a
include $(PREBUILT\_STATIC\_LIBRARY)

include $(CLEAR\_VARS)
LOCAL\_MODULE := ffmpeg
LOCAL\_SRC\_FILES := $(3RD\_LIB\_DIR)/libffmpeg.so
include $(PREBUILT\_SHARED\_LIBRARY)

Build native sdk

include $(CLEAR\_VARS)

LOCAL\_MODULE := native\_sdk

LOCAL\_SRC\_FILES := \

$(subst $(LOCAL\_PATH)/,,$(wildcard $(LOCAL\_PATH)/src/algorithm/\*.c))  \\
$(subst $(LOCAL\_PATH)/,,$(wildcard $(LOCAL\_PATH)/src/core/\*.c))  \\
$(subst $(LOCAL\_PATH)/,,$(wildcard $(LOCAL\_PATH)/src/network/\*.c)) \\
$(subst $(LOCAL\_PATH)/,,$(wildcard $(LOCAL\_PATH)/src/utils/\*.c)) \\
$(subst $(LOCAL\_PATH)/,,$(wildcard $(LOCAL\_PATH)/src/\*.c))  

LOCAL\_C\_INCLUDES := $(3RD\_INC\_DIR)
LOCAL\_C\_INCLUDES := $(LOCAL\_PATH)/src
LOCAL\_C\_INCLUDES := $(LOCAL\_PATH)/src/algorithm
LOCAL\_C\_INCLUDES += $(LOCAL\_PATH)/src/core
LOCAL\_C\_INCLUDES += $(LOCAL\_PATH)/src/network
LOCAL\_C\_INCLUDES += $(LOCAL\_PATH)/src/utils

LOCAL\_CFLAGS := -DANDROID
LOCAL\_LDLIBS := -llog

LOCAL\_STATIC\_LIBRARIES := math json
LOCAL\_SHARED\_LIBRARIES := ffmpeg

include $(BUILD\_SHARED\_LIBRARY)

Build tests

include $(CLEAR\_VARS)
LOCAL\_MODULE := test.out
LOCAL\_SRC\_FILES := $(subst $(LOCAL\_PATH)/,,$(wildcard $(LOCAL\_PATH)/tests/*.c))
LOCAL\_CFLAGS := -DANDROID
LOCAL\_C\_INCLUDES := $(LOCAL\_PATH)/src
LOCAL\_LDLIBS := -llog -fPIE -pie
LOCAL\_SHARED\_LIBRARIES:= native\_sdk
include $(BUILD\_EXECUTABLE)
对于 Android.mk 而言,如果要依赖第三方库文件,则必须增加 PREBUILT_XXXX_LIBRARY 进行预编译,LOCAL_SRC_FILES 宏给出了须要编译的源文件列表,这里我通过 subst 和 wildcard 这两个函数,实现了主动扫描指定目录下的源文件,最初,通过 BUILD_EXECUTABLE 编译出能够运行于 adb shell 命令行的可执行程序,用于做代码的单元测试。

相干材料:Android 根底教学视频

本文转自 https://blog.51cto.com/tickti…,如有侵权,请分割删除。

正文完
 0