cpr
cpr copied to clipboard
NDK and CPR
I am trying to use CPR in android. This is my src/main/cpp
directory's structure:
cpr/
native-lib.cpp
CMakeLists.txt
note: cpr/ directory is generated by running this command git clone --recurse-submodules https://github.com/whoshuu/cpr.git
This is the content of CMakeLists.txt in src/main/cpp directory:
cmake_minimum_required(VERSION 3.4.1)
add_library(
native-lib
SHARED
native-lib.cpp
)
find_library(
log-lib
log
)
add_subdirectory(cpr)
target_link_libraries(
native-lib
cpr
${log-lib}
)
But I get this error when I build and run the code:
Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the
system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY
OPENSSL_INCLUDE_DIR)
EDIT I have found the answer soon I will be posting the answer
Hi @sadeghhosseini Can you post your answer ? I try to use Android Studio to build Static Library (.a) or Shared Library (.so) for another android app using , Do you know is it possible ? Thanks
Ping @kingkili.
Is OPENSSL for Android installed?
Hi, sorry for the delay, I was really busy that's why I couldn't answer sooner. The thing I did is, I used the prebuilt libcurl(as far as I could remember, I think I myself built them) library and use them in my project like this:
My cpp folder, directory structure:
-cpp
--/jni/
--/lib/
---/cpr/
---/curl/arm64-v8a/libcurl.a
---/curl/armeabi-v7a/libcurl.a
---/curl/x86/libcurl.a
---/curl/x86_64/libcurl.a
--/my-project-cpp-files
--/my-project-cpp-files/CMakeLists.txt
--CMakeLists.txt
The content of --CMakeLists.txt
:
cmake_minimum_required(VERSION 3.4.1)#very important, changing the version number might cause error
#adding lib.curl library
add_library(curl STATIC IMPORTED)
set_target_properties(
curl
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/lib/curl/${CMAKE_ANDROID_ARCH_ABI}/libcurl.a
)
include_directories(${CMAKE_SOURCE_DIR}/lib/curl/include)
#adding lib.cpr library
add_subdirectory(${CMAKE_SOURCE_DIR}/lib/cpr ${CMAKE_BINARY_DIR}/cpr)
#add_library(lib.cpr STATIC IMPORTED)
#set_target_properties(lib.cpr PROPERTIES IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/lib.cpr/${ANDROID_ABI}/lib.cpr.a)
include_directories(${CMAKE_SOURCE_DIR}/lib/cpr/include)
#other project specific cmake commands
target_link_libraries( # Specifies the target library.
android_cpp_connector
my_code
cpr
curl
${zlib}
${log-lib}
)
The content of /my-project-cpp-files/CMakeLists.txt
is:
cmake_minimum_required(VERSION 3.4.1)#the version is very very important changing the version might cause error
set(MYCODE_SDK_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
project(myProject)
#my other cmake commands
target_link_libraries(
${PROJECT_NAME}
${CPR_LIBRARIES}
)
install(
TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
so the bottom line is you must build the libraries you want to work with separately or find the prebuilt libraries for the platforms you need and the use those. I hope this is helpful to you.
Okay, can you please upload the output from the CMake configure run.
But from what I see I would assume that cpr does not find the prebuild libcurl, and therefore it will try to build libcurl itself.
You can also try to add before add_subdirectory(${CMAKE_SOURCE_DIR}/lib/cpr ${CMAKE_BINARY_DIR}/cpr)
,
set(USE_SYSTEM_CURL ON CACHE BOOL "Use precompiled curl" FORCE)
No response for over one month. I'm closing this. Feel free to reopen if the problem still exists.
@KingKili @COM8 : Hi I am also facing this issue of " No valid SSL backend found! Please install OpenSSL, Mbed TLS or disable SSL by setting CPR_ENABLE_SSL to OFF." I created a simple HTTPS API call through
#include <jni.h>
#include <string>
#include <cpr/cpr.h>
#include <iostream>
#include <android/log.h>
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_nativesample_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
cpr::Response r = cpr::Get(cpr::Url{"https://jsonplaceholder.typicode.com/posts"});
r.status_code; // 200
r.header["content-type"]; // application/json; charset=utf-8
r.text;
__android_log_print(ANDROID_LOG_VERBOSE, "MYTEST " , "%ld", r.status_code);
__android_log_print(ANDROID_LOG_DEBUG, "MYTEST " , "%s", r.text.c_str());
return env->NewStringUTF(r.text.c_str());
}
Note : For HTTP request I am getting the response (when setting set(CPR_ENABLE_SSL OFF) in CMakeLists.txt), but when I am trying for HTTPS URL I am getting the above mention error. Here is my complete error log
[CXX1405] error when building with cmake using /Users/abhishek4.bhatt/AndroidStudioProjects/NativeSample/app/src/main/cpp/CMakeLists.txt: -- Android: Targeting API '21' with architecture 'arm64', ABI 'arm64-v8a', and processor 'aarch64'
-- Android: Selected unified Clang toolchain
-- The C compiler identification is Clang 12.0.8
-- The CXX compiler identification is Clang 12.0.8
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Users/abhishek4.bhatt/Library/Android/sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Users/abhishek4.bhatt/Library/Android/sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- C++ Requests CMake Options
-- =======================================================
-- CPR_GENERATE_COVERAGE: OFF
-- CPR_CURL_NOSIGNAL: OFF
-- CPR_USE_SYSTEM_GTEST: OFF
-- CPR_FORCE_USE_SYSTEM_CURL: OFF
-- CPR_ENABLE_SSL: ON
-- CPR_FORCE_OPENSSL_BACKEND: OFF
-- CPR_FORCE_WINSSL_BACKEND: OFF
-- CPR_FORCE_DARWINSSL_BACKEND: OFF
-- CPR_FORCE_MBEDTLS_BACKEND: OFF
-- CPR_ENABLE_LINTING: OFF
-- CPR_ENABLE_CPPCHECK: OFF
-- CPR_BUILD_TESTS: OFF
-- CPR_BUILD_TESTS_SSL: OFF
-- =======================================================
-- Automatically detecting SSL backend.
-- Detecting SSL backend...
-- Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY OPENSSL_INCLUDE_DIR)
-- Could NOT find MbedTLS (missing: MBEDTLS_INCLUDE_DIRS MBEDTLS_LIBRARY MBEDX509_LIBRARY MBEDCRYPTO_LIBRARY)
-- Configuring incomplete, errors occurred!
See also "/Users/abhishek4.bhatt/AndroidStudioProjects/NativeSample/app/.cxx/Debug/594s5v1g/arm64-v8a/CMakeFiles/CMakeOutput.log".
C++ build system [configure] failed while executing:
/Users/abhishek4.bhatt/Library/Android/sdk/cmake/3.22.1/bin/cmake \
-H/Users/abhishek4.bhatt/AndroidStudioProjects/NativeSample/app/src/main/cpp \
-DCMAKE_SYSTEM_NAME=Android \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-DCMAKE_SYSTEM_VERSION=21 \
-DANDROID_PLATFORM=android-21 \
-DANDROID_ABI=arm64-v8a \
-DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \
-DANDROID_NDK=/Users/abhishek4.bhatt/Library/Android/sdk/ndk/23.1.7779620 \
-DCMAKE_ANDROID_NDK=/Users/abhishek4.bhatt/Library/Android/sdk/ndk/23.1.7779620 \
-DCMAKE_TOOLCHAIN_FILE=/Users/abhishek4.bhatt/Library/Android/sdk/ndk/23.1.7779620/build/cmake/android.toolchain.cmake \
-DCMAKE_MAKE_PROGRAM=/Users/abhishek4.bhatt/Library/Android/sdk/cmake/3.22.1/bin/ninja \
-DCMAKE_CXX_FLAGS=-std=c++17 \
-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/Users/abhishek4.bhatt/AndroidStudioProjects/NativeSample/app/build/intermediates/cxx/Debug/594s5v1g/obj/arm64-v8a \
-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=/Users/abhishek4.bhatt/AndroidStudioProjects/NativeSample/app/build/intermediates/cxx/Debug/594s5v1g/obj/arm64-v8a \
-DCMAKE_BUILD_TYPE=Debug \
-B/Users/abhishek4.bhatt/AndroidStudioProjects/NativeSample/app/.cxx/Debug/594s5v1g/arm64-v8a \
-GNinja
from /Users/abhishek4.bhatt/AndroidStudioProjects/NativeSample/app
CMake Error at /Users/abhishek4.bhatt/AndroidStudioProjects/NativeSample/app/.cxx/Debug/594s5v1g/arm64-v8a/_deps/cpr-src/CMakeLists.txt:112 (message):
No valid SSL backend found! Please install OpenSSL, Mbed TLS or disable SSL
by setting CPR_ENABLE_SSL to OFF.
"
And my CMakeLists.txt
cmake_minimum_required(VERSION 3.22.1)
project("nativesample")
add_library( # Sets the name of the library.
nativesample
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp)
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log)
#set(CPR_ENABLE_SSL ON)
include(FetchContent)
FetchContent_Declare(cpr GIT_REPOSITORY https://github.com/libcpr/cpr.git
GIT_TAG 871ed52d350214a034f6ef8a3b8f51c5ce1bd400) # The commit hash for 1.9.0. Replace with the latest from: https://github.com/libcpr/cpr/releases
FetchContent_MakeAvailable(cpr)
target_link_libraries(nativesample PRIVATE cpr::cpr ${log-lib} ${log})
I am using Mac OS M1. I installed openSSL also but still not working for me.
Here you can find information and an example for how to get cpr to compile with OpenSSL on MacOS: https://github.com/libcpr/cpr/blob/master/.github/workflows/ci.yml#L529-L559
You have to set environment variables for it
@COM8 : First of all thanks a lot for the quick response. Let me check with this.
@COM8 @KingKili : I tried with the above steps but still getting the same issue of openssl. Can you please assist me to resolve this issue.
Reopening since it seams to be not working.
I have never been using Android so far. Can anyone provide us a minimal project so we can debug this?
Maybe this can help installing OpenSSL for your android toolchain. https://wiki.openssl.org/index.php/Android
Yes, sure give me few minutes I am providing you one minimal project.
Maybe this can help installing OpenSSL for your android toolchain. https://wiki.openssl.org/index.php/Android
Let me check this as well
Maybe this can help installing OpenSSL for your android toolchain. https://wiki.openssl.org/index.php/Android
Let me check this as well
I already installed with the steps mention in the thread. but still getting the same issue. It is working fine in case of HTTP request.
@KingKili Please find the link for one sample Android project[ ]https://github.com/abhishekbhatt85/NdkDemo
In my eyes this is not a cpr issue and more a system configuration issue. The problem is, Android (CMake) can not find a valid OpenSSL version for the target you are compiling for. This can be solved by the following steps:
# Download the latest version of OpenSSL from here: https://www.openssl.org/source/
# Extract it and open a terminal inside it
# Export the android NDK home:
export ANDROID_NDK_HOME=/home/fabian/Android/Sdk/ndk/23.1.7779620/
# Add the required compiler to path:
PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin:$ANDROID_NDK_HOME/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin:$PATH
# Configure for the desired API:
./Configure android-arm64 -D__ANDROID_API__=32
# Build
make
Repeat this for all configurations you want to build for. Instructions taken from the NOTES.ANDROID
file inside the OpenSSL root.
Then for debugging you can do something like this in your root CMakeLists.txt:
set(OPENSSL_ROOT_DIR "/home/fabian/Downloads/openssl-1.1.1q")
set(OPENSSL_LIBRARIES "/home/fabian/Downloads/openssl-1.1.1q")
list(APPEND CMAKE_FIND_ROOT_PATH "${OPENSSL_ROOT_DIR}")
find_package(OpenSSL REQUIRED)
Once this succeeds, you know you have the correct paths.
Remove the find_package(OpenSSL REQUIRED)
again and replace it with the cpr fetch content stuff.
In my eyes this is not a cpr issue and more a system configuration issue. The problem is, Android (CMake) can not find a valid OpenSSL version for the target you are compiling for. This can be solved by the following steps:
# Download the latest version of OpenSSL from here: https://www.openssl.org/source/ # Extract it and open a terminal inside it # Export the android NDK home: export ANDROID_NDK_HOME=/home/fabian/Android/Sdk/ndk/23.1.7779620/ # Add the required compiler to path: PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin:$ANDROID_NDK_HOME/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin:$PATH # Configure for the desired API: ./Configure android-arm64 -D__ANDROID_API__=32 # Build make
Repeat this for all configurations you want to build for. Instructions taken from the
NOTES.ANDROID
file inside the OpenSSL root.Then for debugging you can do something like this in your root CMakeLists.txt:
set(OPENSSL_ROOT_DIR "/home/fabian/Downloads/openssl-1.1.1q") set(OPENSSL_LIBRARIES "/home/fabian/Downloads/openssl-1.1.1q") list(APPEND CMAKE_FIND_ROOT_PATH "${OPENSSL_ROOT_DIR}") find_package(OpenSSL REQUIRED)
Once this succeeds, you know you have the correct paths. Remove the
find_package(OpenSSL REQUIRED)
again and replace it with the cpr fetch content stuff.
Let me check, I will get back to you.
Hi @COM8 I tried the steps mention in the above suggestion and configured my environment as you suggested. But this time I getting find Package error `[CXX1405] error when building with cmake using /Users/abhishek4.bhatt/AndroidStudioProjects/NdkDemo/app/src/main/cpp/CMakeLists.txt: -- Android: Targeting API '21' with architecture 'x86', ABI 'x86', and processor 'i686' -- Android: Selected unified Clang toolchain -- The C compiler identification is Clang 12.0.8 -- The CXX compiler identification is Clang 12.0.8 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /Users/abhishek4.bhatt/Library/Android/sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /Users/abhishek4.bhatt/Library/Android/sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring incomplete, errors occurred! See also "/Users/abhishek4.bhatt/AndroidStudioProjects/NdkDemo/app/.cxx/Debug/o9x1l2f2/x86/CMakeFiles/CMakeOutput.log".
C++ build system [configure] failed while executing:
/Users/abhishek4.bhatt/Library/Android/sdk/cmake/3.22.1/bin/cmake
-H/Users/abhishek4.bhatt/AndroidStudioProjects/NdkDemo/app/src/main/cpp
-DCMAKE_SYSTEM_NAME=Android
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
-DCMAKE_SYSTEM_VERSION=21
-DANDROID_PLATFORM=android-21
-DANDROID_ABI=x86
-DCMAKE_ANDROID_ARCH_ABI=x86
-DANDROID_NDK=/Users/abhishek4.bhatt/Library/Android/sdk/ndk/23.1.7779620
-DCMAKE_ANDROID_NDK=/Users/abhishek4.bhatt/Library/Android/sdk/ndk/23.1.7779620
-DCMAKE_TOOLCHAIN_FILE=/Users/abhishek4.bhatt/Library/Android/sdk/ndk/23.1.7779620/build/cmake/android.toolchain.cmake
-DCMAKE_MAKE_PROGRAM=/Users/abhishek4.bhatt/Library/Android/sdk/cmake/3.22.1/bin/ninja
-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/Users/abhishek4.bhatt/AndroidStudioProjects/NdkDemo/app/build/intermediates/cxx/Debug/o9x1l2f2/obj/x86
-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=/Users/abhishek4.bhatt/AndroidStudioProjects/NdkDemo/app/build/intermediates/cxx/Debug/o9x1l2f2/obj/x86
-DCMAKE_BUILD_TYPE=Debug
-B/Users/abhishek4.bhatt/AndroidStudioProjects/NdkDemo/app/.cxx/Debug/o9x1l2f2/x86
-GNinja
from /Users/abhishek4.bhatt/AndroidStudioProjects/NdkDemo/app
CMake Error at /Users/abhishek4.bhatt/Library/Android/sdk/cmake/3.22.1/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the
system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY) (found
version "1.1.1q")
Call Stack (most recent call first):
/Users/abhishek4.bhatt/Library/Android/sdk/cmake/3.22.1/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
/Users/abhishek4.bhatt/Library/Android/sdk/cmake/3.22.1/share/cmake-3.22/Modules/FindOpenSSL.cmake:574 (find_package_handle_standard_args)
CMakeLists.txt:76 (find_package)
Affected Modules: app
``
On https://developer.android.google.cn/ndk/guides/cpp-support?hl=en has an note:
It maybe mean on android app client,can not load any c or c++ lib from system.So must be build Openssl lib into your application.