简体   繁体   中英

How to link OpenSSL libraries in CMake Qt Android app?

I have tried several options but still didn't link the libraries successfully. I have Qt Android CMake application and I need to link the libssl and libcrypto libraries. I have placed them in the poject /android_openssl/ANDROID_ABI/lib/ and for each ABI i have this:

在此处输入图像描述

And also the include folder is next to the abi folders. I have tried this solutions but I end up without this libraries to be linked:

I tried this possible solution and I got error:

add_library(ssl STATIC IMPORTED)
add_library(crypto STATIC IMPORTED)


    set_target_properties(ssl PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/android_openssl/${ANDROID_ABI}/lib/libssl.a)
    set_target_properties(crypto PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/android_openssl/${ANDROID_ABI}/lib/libcrypto.a)

    add_library(MyApp SHARED
        ${PROJECT_SOURCES}
        ${PROJECT_HEADERS}
        ${PROJECT_RESOURCES}
        )

target_link_libraries(MyApp
  PRIVATE
  ssl
  crypto
  Qt5::Core
  Qt5::Quick
  Qt5::QuickControls2
  Qt5::Sql
  Qt5::Network
  Qt5::Svg
)

Error:

E linker  : library "/system/lib/libcrypto.so" ("/system/lib/libcrypto.so") needed or dlopened by "/data/app/~~Xp7o_GB4MNRnsRrBx8X1Pw==/org.qtproject.example-VZ3Vyyfp8FGD1_RYb2Soug==/lib/x86/libQt5Core_x86.so" is not accessible for the namespace: [name="classloader-namespace", ld_library_paths="", default_library_paths="/data/app/~~Xp7o_GB4MNRnsRrBx8X1Pw==/org.qtproject.example-VZ3Vyyfp8FGD1_RYb2Soug==/lib/x86:/data/app/~~Xp7o_GB4MNRnsRrBx8X1Pw==/org.qtproject.example-VZ3Vyyfp8FGD1_RYb2Soug==/base.apk!/lib/x86", permitted_paths="/data:/mnt/expand:/data/user/0/org.qtproject.example"]
W libMyApp_x86.so: qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed

Can you please tell me what I am doing wrong? I have found so much resources on this topic but still I am doing something wrong.

Also, I see that here I have static libraries for each abi .a and also I see .so.1.1 . So which one I should link?

By looking at the error it seems that "libQt5Core_x86.so" is looking for "libcrypto.so" and cannot find it. That is expected since OpenSSL builds ".so.1.1" library files.

To solve this there you should:

1 - Rebuild OpenSSL for Android without.1.1. There are some answers on Stack Overflow and scripts on Github to do this but running make SHLIB_VERSION_NUMBER= SHLIB_EXT=.so on newer versions of OpenSSL should do the trick. Any doubts for other android related stuff use this guide: https://doc.qt.io/qt-6/android-openssl-support.html#building-openssl-for-android

2 - Use Qt creator to get the OpenSSL handy include project as this screenshot shows. (This is a mac screenshot, but in Linux look for Tools > Options > Devices > Android > Android OpenSSL Settings

3 - Finally follow change CMakeLists.txt to use the project above and add the OpenSSL library files using QT_EXTRA_LIBS. Use this guide: https://doc.qt.io/qt-6/android-openssl-support.html#using-openssl-libraries-with-qt-for-android

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