Android开发之Android.mk模板的实例详解
android开发之android.mk模板的实例详解
关于android ndk开发的文章已经比较多了,我的博客中也分享了很多ndk开发相关经验和技巧,今天简单写了一个 android.mk 的示例模板,供初学者参考。
本模板主要给大家示例 android ndk 开发中的如下几个问题:
1. 如何自动添加需要编译的源文件列表
2. 如何添加第三方静态库、动态库的依赖
3. 如何构造一个完整的ndk工程框架
假设我们的项目依赖 libmath.a, libjson.a, libffmpeg.so 这几个第三方库文件,项目包含如下几个模块:algorithm,core,network,utils,tests,每个模块都有若干个.c/.h文件,我们要把整个工程编译为动态库,并通过 native_sdk.c 中的函数向java层提供调用接口。
那么,我们可以对 android 工程的 jni 目录,做如下划分:
对应的 android.mk 文件的写法示例如下(注:工程文件可以到我的github(@jhuster/android)中在线查看,也可以在本博文最后的附件中下载):
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命令行的可执行程序,用于做代码的单元测试。
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!