主项目源树外的ndk构建库

时间:2012-09-23 11:29:04

标签: android-ndk

我想使用ndk构建第三方库avahi。 avahi已经有了android端口(有效的Android.mk)。

我做了什么: 我已经成功创建了一个项目并将所有源代码复制到jni /文件夹并运行ndk-build。它构建了libavahi.so

我想做什么: 我想将它存储在项目源代码树之外的文件夹中,而不是将所有源代码复制到jni /文件夹中。我该怎么办?我查看了NDK文档/导入模块,但没有类似于我的情况。

ndk中的新手,欢迎提出任何建议。

1 个答案:

答案 0 :(得分:5)

你是对的,这不是导入模块的情况。您从本机代码中引用 avihi 库的方式仍为 LOCAL_SHARED_LIBRARIES (请参阅NDK示例 module-exports )。但是在jni/Android.mk文件中,您可以使用include命令到另一个文件。此命令与 C 中的#include语句非常相似。此文件不必位于项目树中。采用相同的样本,以下是它的工作原理:

原始Android.mk from samples/module-exports/jni

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := foo
LOCAL_SRC_FILES := foo/foo.c
LOCAL_CFLAGS := -DFOO=2
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/foo
LOCAL_EXPORT_CFLAGS := -DFOO=1
LOCAL_EXPORT_LDLIBS := -llog
include $(BUILD_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := bar
LOCAL_SRC_FILES := bar/bar.c
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/bar
LOCAL_STATIC_LIBRARIES := foo
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := zoo
LOCAL_SRC_FILES := zoo/zoo.c
LOCAL_SHARED_LIBRARIES := bar
include $(BUILD_SHARED_LIBRARY)

更改后的文件如下所示:

ZOO_LOCAL_PATH := $(call my-dir)
include ~/projects/bar/jni/Android.mk
LOCAL_PATH := $(ZOO_LOCAL_PATH)

include $(CLEAR_VARS)
LOCAL_MODULE := zoo
LOCAL_SRC_FILES := zoo/zoo.c
LOCAL_SHARED_LIBRARIES := bar
include $(BUILD_SHARED_LIBRARY)

外部 bar / jni / Android.mk如下:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := foo
LOCAL_SRC_FILES := foo/foo.c
LOCAL_CFLAGS := -DFOO=2
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/foo
LOCAL_EXPORT_CFLAGS := -DFOO=1
LOCAL_EXPORT_LDLIBS := -llog
include $(BUILD_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := bar
LOCAL_SRC_FILES := bar/bar.c
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/bar
LOCAL_STATIC_LIBRARIES := foo
include $(BUILD_SHARED_LIBRARY)

现在,bar.c和foo.c两个文件都可以保存在动物园项目的树之外!