如何使用NDK将静态c-library(libsrtp)添加到Android-Project

时间:2014-10-06 09:55:52

标签: android c android-ndk

因此我对Android-NDK相对较新,并试图将未编译的c库添加到android。 我已从here下载了库的源代码,并按照these说明使用ndk-build从下载的文件中创建静态库(.a-File)。
所以现在我有一个文件夹结构,如下所示:
-srtp
--include(srtp.h,crypto.h)
--srtp(srtp.c ......)
--obj
---当地
---- armeabi
-----的 libsrtp_static.a

在我的Android项目中,我想在我的/ jni /文件夹中的c文件中包含srtp.h。我试图将.a文件添加到/ jni文件夹中,但是它没有说" srtp.h没有这样的文件或者直接使用#34;。如果我将整个srtp文件夹添加到项目中并将标题包含在" srtp / include / srtp.h"它编译但无法链接源,因为我收到错误"未定义的引用 srtp-function-call "

这是我的Android.mk(在下载的源代码中从Android.mk复制的部分内容):

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_LDLIBS := -llog
LOCAL_MODULE    := ndk1 #name of my project
LOCAL_SRC_FILES := native.c #name of the c-source file

include $(BUILD_SHARED_LIBRARY)


LOCAL_PATH:= /home/dev/programing/srtp
common_SRC_FILES := \
    srtp/srtp.c \
    srtp/ekt.c \
    crypto/cipher/cipher.c \
    #...more files like that
common_CFLAGS := \
    -DPOSIX -iquote$(LOCAL_PATH)/crypto/include \
    -Werror \
    -Wno-ignored-qualifiers \
    -Wno-sign-compare \
    -Wno-missing-field-initializers
common_C_INCLUDES = $(LOCAL_PATH)/include
# For the device
# =====================================================
# Device static library
include $(CLEAR_VARS)
ifneq ($(TARGET_ARCH),x86)
    LOCAL_NDK_VERSION := 5
    LOCAL_SDK_VERSION := 9
endif
    LOCAL_SRC_FILES := libsrtp_static.a
    LOCAL_CFLAGS += $(common_CFLAGS)
    LOCAL_C_INCLUDES += /home/dev/programing/srtp/include
    LOCAL_MODULE:= libsrtp_static
    LOCAL_MODULE_TAGS := optional
include $(BUILD_STATIC_LIBRARY)

非常感谢任何帮助!非常感谢你。

1 个答案:

答案 0 :(得分:0)

您需要从lib(ndk1)声明中添加对libsrtp_static的引用:

LOCAL_STATIC_LIBRARIES := libsrtp_static

此外,您不需要将所有strp源复制到项目中,只需将其添加到静态预构建库定义中

LOCAL_EXPORT_C_INCLUDES += /home/dev/programing/srtp/include

然后,此路径将自动添加到使用该库的库的LOCAL_C_INCLUDES

您是在构建libsrtp还是仅使用预构建的静态库?如果您使用的是预建(lib * .a),请将BUILD_STATIC_LIBRARY替换为PREBUILT_STATIC_LIBRARY,并将.a设置为SRC_FILE:

总之,一旦构建了 libstrp_static.a ,这应该可行:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_LDLIBS := -llog
LOCAL_MODULE    := ndk1 #name of my project
LOCAL_SRC_FILES := native.c #name of the c-source file
LOCAL_STATIC_LIBRARIES := libsrtp_static
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
    LOCAL_SRC_FILES := /home/dev/programing/srtp/libsrtp_static.a #check path to libsrtp_static.a 
    LOCAL_EXPORT_C_INCLUDES += /home/dev/programing/srtp/include
    LOCAL_MODULE:= libsrtp_static
    LOCAL_MODULE_TAGS := optional
include $(BUILD_STATIC_LIBRARY)