• 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 目录,做如下划分:

     

      wKioL1aSJN3zxpn0AABXdRGkz8k276.png

      对应的 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命令行的可执行程序,用于做代码的单元测试。

  • 相关阅读:
    数据包发送
    linux 进程调度3
    linux 进程调度2
    linux 进程调度1
    进程间通信:信号
    fork vfork clone学习
    跳表
    【转】Linux内存管理综述
    如何优雅的写出链表代码
    This function or variable may be unsafe Consider using xxx instead
  • 原文地址:https://www.cnblogs.com/oneasdf/p/9487325.html
Copyright © 2020-2023  润新知