6

I previously asked essentially the same question (different NDK) here and thought I correctly built openssl but once I got around to trying to link it to my app I discovered I didn't build it correctly.

  1. If I bridge of the answer from @AlexCohn here I start with the setenv_android.sh script.

  2. I modify the script to set THE_ARCH=arm64-v8a trying to target 64bit android architecture.

  3. When I run the script there some things it fails to find:

    ERROR: Failed to find Android cpp. Please edit this script. ERROR: Failed to find Android gcc. Please edit this script. ERROR: Failed to find Android g++. Please edit this script. ERROR: AOSP_STL_INC is not valid. Please edit this script. ERROR: AOSP_STL_LIB is not valid. Please edit this script. ANDROID_NDK_ROOT: /Users/spartygw/android-ndk-r19/ AOSP_TOOLCHAIN_PATH: /Users/spartygw/android-ndk-r19//toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/bin AOSP_ABI: arm64-v8a AOSP_API: android-21 AOSP_SYSROOT: /Users/spartygw/android-ndk-r19//platforms/android-21/arch-arm64 AOSP_FLAGS: -funwind-tables -fexceptions -frtti AOSP_STL_INC: /Users/spartygw/android-ndk-r19//sources/cxx-stl/stlport/stlport/ AOSP_STL_LIB: /Users/spartygw/android-ndk-r19//sources/cxx-stl/stlport/libs/arm64-v8a/libstlport_shared.so 
  4. When I look in /Users/spartygw/android-ndk-r19//toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/bin there is no aarch64-linux-android-cpp or -gcc or -g++ just as the script output says:

    $ ls -1 ~/android-ndk-r19//toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/bin ./ ../ aarch64-linux-android-addr2line aarch64-linux-android-ar aarch64-linux-android-as aarch64-linux-android-c++filt aarch64-linux-android-dwp aarch64-linux-android-elfedit aarch64-linux-android-gprof aarch64-linux-android-ld aarch64-linux-android-ld.bfd aarch64-linux-android-ld.gold aarch64-linux-android-nm aarch64-linux-android-objcopy aarch64-linux-android-objdump aarch64-linux-android-ranlib aarch64-linux-android-readelf aarch64-linux-android-size aarch64-linux-android-strings aarch64-linux-android-strip 

This is where I think I got myself into trouble last time. I started hacking the script to get something that seemed to work and I'm sure what I did was wrong now.

I really don't understand the process so I'm hoping for help. Is anyone building arm64-v8a versions of OpenSLL successfully?

3 Answers 3

6

I was able to build, link, and run openssl for android arm64 with NDK r19. But I had to use the standalone toolchain generated from android-ndk-r19.

$ cd /path/to/android-ndk-r19 $ ./build/tools/make-standalone-toolchain.sh \ --toolchain=aarch64-linux-android 

this will generate a dir called aarch64-linux-android in your tmp dir, put its bin directory in your path. Also, set your ANDROID_NDK_HOME to this location.

$ export PATH=/path/to/aarch64-linux-android/bin:${PATH} $ export ANDROID_NDK_HOME=/path/to/aarch64-linux-android 

then just run openssl's Configure and make.

$ cd /path/to/openssl1.1.1 $ ./Configure android-arm64 $ make 

./Configure's output was as follows:

$ ./Configure android-arm64 Configuring OpenSSL version 1.1.1b-dev (0x10101020L) for android-arm64 Using os-specific seed configuration Creating configdata.pm Creating Makefile ********************************************************************** *** *** *** OpenSSL has been successfully configured *** *** *** *** If you encounter a problem while building, please open an *** *** issue on GitHub <https://github.com/openssl/openssl/issues> *** *** and include the output from the following command: *** *** *** *** perl configdata.pm --dump *** *** *** *** (If you are new to OpenSSL, you might want to consult the *** *** 'Troubleshooting' section in the INSTALL file first) *** *** *** ********************************************************************** $ 

and finally I created a new Android Studio project, with c++11, exceptions, and rtti support (through the new project wizard), and linked in the output of the build with a CMakeLists.txt slightly modified from the one created by Android Studio:

cmake_minimum_required(VERSION 3.4.1) # Creates and names a library, sets it as either STATIC # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds them for you. # Gradle automatically packages shared libraries with your APK. add_library( # Sets the name of the library. native-lib # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). src/main/cpp/native-lib.cpp ) # Searches for a specified prebuilt library and stores the path as a # variable. Because CMake includes system libraries in the search path by # default, you only need to specify the name of the public NDK library # you want to add. CMake verifies that the library exists before # completing its build. 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 ) # HERE ARE THE PARTS I EDITED: # NOTE FOR THE COMMANDS ABOVE, THIS IS JUST THE OPENSSL SOURCE DIR. set (SSL_PATH /path/to/ssl/build/outputs/) include_directories(${SSL_PATH}/include) set (open-ssl-libs ${SSL_PATH}/libssl.a ${SSL_PATH}/libcrypto.a) # Specifies libraries CMake should link to your target library. You # can link multiple libraries, such as libraries you define in this # build script, prebuilt third-party libraries, or system libraries. target_link_libraries( # Specifies the target library. native-lib # LINK SSL AND CRYPTO HERE: ${open-ssl-libs} # Links the target library to the log library # included in the NDK. ${log-lib} ) 

this is enough to show that it links, but I added one small reference to libssl.a in the boilerplate c++ code generated by Android Studio:

#include <jni.h> #include <string> #include <openssl/ssl.h> extern "C" JNIEXPORT jstring JNICALL Java_com_vernier_android_test_1ssl_MainActivity_stringFromJNI( JNIEnv* env, jobject /* this */) { SSL* ssl = SSL_new(nullptr); std::string hello = "Hello from C++"; return env->NewStringUTF(hello.c_str()); } 

and I ran the app successfully.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for the detailed response! When I try to duplicate what you've done I run into a problem almost immediately with the ./configure part. There is no ./configure, just a ./Configure and a ./config in the openssl directory. When I run ./Configure android-arm64 it tells me to pick the os/compiler. If I run ./config android-arm64 it warns me and then fails with target already defined - darwin-i386-cc (offending arg: android-arm64)
sorry about that, the mac is case agnostic, "./Configure android-arm64" is what I used. Perhaps the os/compiler pairs are slightly different for your version of the source? Please check the file in the project ANDROID.NOTES, it specifically says to use Configure in this way. I will add my output from ./Configure above as soon as SO allows me to edit it :)
I was able to build for android-ndk-r19. See my update to the detailed answer above.
openssl's COnfigure depends on sysroot being in the old aarch64 location, while the bin directory is now in the llvm sysroot, and it doesn't have gcc. Too much hacking to get it to work for me. Standalone toolchain does the job!
Agreed, way too much hacking required. The standalone toolchain is the ticket!
|
0

I was in trouble for addind SSL_PATH. Solve it with

set (SSL_PATH ${CMAKE_SOURCE_DIR}/src/main/cpp/openssl) 

CMAKE_SOURCE_DIR is already declared in Cmake.

Comments

0

Assuming you already have an NDK, cd to the folder, enter /build/tools and run the following command. Replace the API with whatever you need and specify the folder where the standalone toolchain will be placed.

python make_standalone_toolchain.py --install-dir=<standalone_toolchain_folder> --arch=arm64 --api=22

With the path you used earlier, run the following.

export ANDROID_NDK_HOME=<standalone_toolchain_folder>

PATH=$ANDROID_NDK_HOME/bin:$PATH

Assuming you have already downloaded and extracted OpenSSL, cd into the folder and run the following commands.

./Configure android-arm64 no-asm

make clean

make

You will find that the .a and .so files for libcrypto and libssl have been placed in the OpenSSL folder. Remove the version suffix from the .so files if needed.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.