Android库链接和LOCAL_SRC_FILES指向丢失的文件

时间:2017-08-13 14:32:36

标签: windows android-ndk openssl cocos2d-x linker-errors

我正在尝试使用OpenSSL将我的Cocos2d-x项目编译到Android。我可以在Visual Studio Community 2013上正确运行该项目,但无法使用cocos compile -p android --android-studio在命令行上编译它。

我将OpenSSL安装到路径项目\ cocos2d \ external \ OpenSSL-Win32并将OpenSSL包含路径添加到Android.mk文件中:

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../proj.win32 \
                    $(LOCAL_PATH)/../../../cocos2d/external/OpenSSL-Win32/include

我跟着Undefined reference to libssl function with Android NDK,但它并没有真正帮助。我的OpenSSL文件夹中没有共享库,只有静态库。我现在能想出的最好的定义是:

include $(CLEAR_VARS)
LOCAL_MODULE    := crypto
LOCAL_SRC_FILES := $(LOCAL_PATH)/../../../cocos2d/external/OpenSSL-Win32/lib/MinGW/libcrypto-1_1.a
include $(PREBUILT_STATIC_LIBRARY)

这给了我以下错误:

Android NDK: ERROR:jni/Android.mk:crypto: LOCAL_SRC_FILES points to a missing file
Android NDK: Check that jni/jni/../../../cocos2d/external/OpenSSL-Win32/lib/MinGW/libcrypto-1_1.a exists or that its path is correct

文件 libcrypto-1_1.a 位于该文件夹中。

修改

我从这里下载了Android的预建OpenSSL库(包括共享库):https://github.com/r4sas/OpenSSL-1.1-Android-Prebuilt

此软件包包含一个Android.mk文件,其中包含共享库的定义:

include $(CLEAR_VARS)
LOCAL_MODULE    := opencrypto
LOCAL_SRC_FILES := libcrypto.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    := openssl
LOCAL_SRC_FILES := libssl.so
include $(PREBUILT_SHARED_LIBRARY)

我将这些定义添加到文件夹cocos2dx-project \ proj.android-studio \ app \ jni中的Android.mk文件中。

它还包含 libcrypto.so libssl.so 文件。我将这些文件复制到cocos2dx-project \ proj.android-studio \ app \ jni并尝试编译项目,但出现以下错误:

Android NDK: Trying to define local module 'openssl' in jni/Android.mk.
Android NDK: But this module was already defined by jni/Android.mk.

修改2

我想我已经取得了一些进展。

我的Android.mk现在看起来像这样:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE    := crypto
LOCAL_SRC_FILES := libcrypto.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    := openssl
LOCAL_SRC_FILES := libssl.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d)
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/external)
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/cocos)
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/cocos/audio/include)

LOCAL_MODULE := MyLib_shared

LOCAL_MODULE_FILENAME := libMyLib

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   ...

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../proj.win32 \
                    $(LOCAL_PATH)/../../../cocos2d/external/openssl/include

# _COCOS_HEADER_ANDROID_BEGIN
# _COCOS_HEADER_ANDROID_END

LOCAL_STATIC_LIBRARIES := cocos2dx_static

# _COCOS_LIB_ANDROID_BEGIN
# _COCOS_LIB_ANDROID_END

# _COCOS_LIB_IMPORT_ANDROID_BEGIN
# _COCOS_LIB_IMPORT_ANDROID_END

include $(BUILD_SHARED_LIBRARY)
$(call import-module,.)

现在这些共享库已正确构建,但我收到了未定义的引用错误:

[armeabi] Install        : libcrypto.so => libs/armeabi/libcrypto.so
[armeabi] Install        : libssl.so => libs/armeabi/libssl.so
[armeabi] SharedLibrary  : libMyLib.so
jni/../../../proj.win32/EncryptionHelper.cpp:61: error: undefined reference to 'EVP_CIPHER_CTX_new'

1 个答案:

答案 0 :(得分:0)

我没有尝试使用共享库来完成这项工作,而是使用静态库。

将此添加到Android.mk:

include $(CLEAR_VARS)
LOCAL_MODULE    := crypto_static
LOCAL_SRC_FILES := libcrypto.a
include $(PREBUILT_STATIC_LIBRARY)
...
LOCAL_STATIC_LIBRARIES := cocos2dx_static \ crypto_static

现在正确编译。

感谢jww所有有用的评论,并指出我正确的方向!