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

在我的Android项目中,我想将srtp.h包含在我的/ jni /文件夹中的c文件中。我试图将.a文件添加到/ jni文件夹中,但是比起它说“ srtp.h没有这样的文件或目录”。如果我将整个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)


任何帮助是极大的赞赏!非常感谢你。

最佳答案

您需要从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)

08-18 12:53