简体   繁体   中英

Android NDK: building static library with other static libraries

What I am trying to do is simple:

I have compiled openssl for android and I have some custom code that I want to build into a static library with openssl pre-built libraries included. This is how I have setup my Android.mk file:

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)

Now this works, however it does not include the openssl libraries at all. If I change $(BUILD_STATIC_LIBRARY to $(BUILD_SHARED_LIBRARY) , then it looks like everything is combined into that shared library correctly.

But it is not what is under my project spec.

Perhaps I am doing something completely wrong?

SOLUTION EDIT

Have actually found the possible solution. Simply don't add the static libraries when building the static library and add them when the actual Android wrapper is being built like so:

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)

And it will work. Hopefully anyone else who makes the same false presumption that you should easily build static libraries with other static libraries will find this question.

NDK has the command you need, it's called 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)

You could also inherit the openssl includes path from a-ssl . FInally, to clean this up a bit more, consider changing the order of modules in your 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM