Android NDK:使用其他静态库构建静态库

时间:2017-11-09 11:53:36

标签: android android-ndk

我想做的很简单:

我已经编译了openssl for android,我有一些自定义代码,我想构建一个包含openssl预构建库的静态库。 这就是我设置Android.mk文件的方式:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := a-ssl
LOCAL_SRC_FILES := {PATH_TO_PROJECT}/vendor/android-openssl/prebuilt/$(TARGET_ARCH_ABI)/libcrypto.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := a-crypto
LOCAL_SRC_FILES := {PATH_TO_PROJECT}/vendor/android-openssl/prebuilt/$(TARGET_ARCH_ABI)/libssl.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_STATIC_LIBRARIES := a-ssl a-crypto
LOCAL_C_INCLUDES += {PATH_TO_PROJECT}/vendor/android-openssl/openssl/include
LOCAL_MODULE := project-alib
LOCAL_SRC_FILES := ../../source.c
include $(BUILD_STATIC_LIBRARY)

现在这可行,但它根本不包括openssl库。如果我将$(BUILD_STATIC_LIBRARY更改为$(BUILD_SHARED_LIBRARY),那么看起来所有内容都会正确地合并到该共享库中。

但这不符合我的项目规范。

也许我做错了什么?

解决方案编辑

实际上找到了可能的解决方案。在构建静态库时,不要添加静态库,并在构建实际的Android包装器时添加静态库,如下所示:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := project-alib
LOCAL_SRC_FILES := {PATH_TO_PROJECT}/project-alib/$(TARGET_ARCH_ABI)/libproject-alib.a
LOCAL_EXPORT_C_INCLUDES := ../../includes
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := a-crypto
LOCAL_SRC_FILES := {PATH_TO_PROJECT}/vendor/android-openssl/prebuilt/$(TARGET_ARCH_ABI)/libcrypto.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := a-ssl
LOCAL_SRC_FILES := {PATH_TO_PROJECT}/vendor/android-openssl/prebuilt/$(TARGET_ARCH_ABI)/libssl.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := project-alib-wrapper
LOCAL_C_INCLUDES += {PATH_TO_PROJECT}/includes
LOCAL_SRC_FILES := wrapper.c
LOCAL_STATIC_LIBRARIES := project-alib a-ssl a-crypto
include $(BUILD_SHARED_LIBRARY)

它会起作用。 希望其他任何做出错误假设的人都应该轻松地使用其他静态库构建静态库,这样就会发现这个问题。

1 个答案:

答案 0 :(得分:0)

NDK拥有您需要的命令,它被称为LOCAL_EXPORT_STATIC_LIBRARIES

include $(CLEAR_VARS)
LOCAL_EXPORT_STATIC_LIBRARIES := a-ssl a-crypto
LOCAL_C_INCLUDES += $(PATH_TO_PROJECT)/vendor/android-openssl/openssl/include
LOCAL_MODULE := project-alib
LOCAL_SRC_FILES := ../../source.c
include $(BUILD_STATIC_LIBRARY)

您还可以从 a-ssl 继承openssl包含路径。最后,为了清理这一点,请考虑更改 Android.mk 中模块的顺序:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_EXPORT_STATIC_LIBRARIES := a-ssl a-crypto
LOCAL_MODULE := project-alib
LOCAL_SRC_FILES := ../../source.c
include $(BUILD_STATIC_LIBRARY)

LOCAL_PATH := $(PATH_TO_PROJECT)/vendor/android-openssl

include $(CLEAR_VARS)
LOCAL_MODULE := a-ssl
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/openssl/include
LOCAL_SRC_FILES := $(LOCAL_PATH)/prebuilt/$(TARGET_ARCH_ABI)/libcrypto.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := a-crypto
LOCAL_SRC_FILES := $(LOCAL_PATH)/prebuilt/$(TARGET_ARCH_ABI)/libssl.a
include $(PREBUILT_STATIC_LIBRARY)