Compiling the Latest Openssl for Android

How to compile OpenSSL 1.1.1 for Android

Updated for OpenSSL 1.1.1b / Android NDK R19C:

#!/bin/sh

ANDROID_NDK=~/android-ndk-r19c
OPENSSL_VERSION=1.1.1b

API_LEVEL=23

BUILD_DIR=/tmp/openssl_android_build
OUT_DIR=/tmp/openssl_android

BUILD_TARGETS="armeabi armeabi-v7a arm64-v8a x86 x86_64"

if [ ! -d openssl-${OPENSSL_VERSION} ]
then
if [ ! -f openssl-${OPENSSL_VERSION}.tar.gz ]
then
wget https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz || exit 128
fi
tar xzf openssl-${OPENSSL_VERSION}.tar.gz || exit 128
fi

cd openssl-${OPENSSL_VERSION} || exit 128

##### Prepare Files #####
sed -i 's/.*-mandroid.*//' Configurations/15-android.conf
patch -p1 -N <<EOP
--- old/Configurations/unix-Makefile.tmpl 2018-09-11 14:48:19.000000000 +0200
+++ new/Configurations/unix-Makefile.tmpl 2018-10-18 09:06:27.282007245 +0200
@@ -43,12 +43,17 @@
# will return the name from shlib(\$libname) with any SO version number
# removed. On some systems, they may therefore return the exact same
# string.
- sub shlib {
+ sub shlib_simple {
my \$lib = shift;
return () if \$disabled{shared} || \$lib =~ /\\.a$/;
- return \$unified_info{sharednames}->{\$lib}. \$shlibvariant. '\$(SHLIB_EXT)';
+
+ if (windowsdll()) {
+ return \$lib . '\$(SHLIB_EXT_IMPORT)';
+ }
+ return \$lib . '\$(SHLIB_EXT_SIMPLE)';
}
- sub shlib_simple {
+
+ sub shlib {
my \$lib = shift;
return () if \$disabled{shared} || \$lib =~ /\\.a$/;

EOP

##### remove output-directory #####
rm -rf $OUT_DIR

##### export ndk directory. Required by openssl-build-scripts #####
export ANDROID_NDK

##### build-function #####
build_the_thing() {
TOOLCHAIN=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64
export PATH=$TOOLCHAIN/$TRIBLE/bin:$TOOLCHAIN/bin:"$PATH"
echo $PATH
make clean
#./Configure $SSL_TARGET $OPTIONS -fuse-ld="$TOOLCHAIN/$TRIBLE/bin/ld" "-gcc-toolchain $TOOLCHAIN" && \
./Configure $SSL_TARGET $OPTIONS -fuse-ld="$TOOLCHAIN/$TRIBLE/bin/ld" && \
make && \
make install DESTDIR=$DESTDIR || exit 128
}

##### set variables according to build-tagret #####
for build_target in $BUILD_TARGETS
do
case $build_target in
armeabi)
TRIBLE="arm-linux-androideabi"
TC_NAME="arm-linux-androideabi-4.9"
#OPTIONS="--target=armv5te-linux-androideabi -mthumb -fPIC -latomic -D__ANDROID_API__=$API_LEVEL"
OPTIONS="--target=armv5te-linux-androideabi -mthumb -fPIC -latomic -D__ANDROID_API__=$API_LEVEL"
DESTDIR="/tmp/$BUILD_DIR/armeabi"
SSL_TARGET="android-arm"
;;
armeabi-v7a)
TRIBLE="arm-linux-androideabi"
TC_NAME="arm-linux-androideabi-4.9"
OPTIONS="--target=armv7a-linux-androideabi -Wl,--fix-cortex-a8 -fPIC -D__ANDROID_API__=$API_LEVEL"
DESTDIR="/tmp/$BUILD_DIR/armeabi-v7a"
SSL_TARGET="android-arm"
;;
x86)
TRIBLE="i686-linux-android"
TC_NAME="x86-4.9"
OPTIONS="-fPIC -D__ANDROID_API__=${API_LEVEL}"
DESTDIR="/tmp/$BUILD_DIR/x86"
SSL_TARGET="android-x86"
;;
x86_64)
TRIBLE="x86_64-linux-android"
TC_NAME="x86_64-4.9"
OPTIONS="-fPIC -D__ANDROID_API__=${API_LEVEL}"
DESTDIR="/tmp/$BUILD_DIR/x86_64"
SSL_TARGET="android-x86_64"
;;
arm64-v8a)
TRIBLE="aarch64-linux-android"
TC_NAME="aarch64-linux-android-4.9"
OPTIONS="-fPIC -D__ANDROID_API__=${API_LEVEL}"
DESTDIR="/tmp/$BUILD_DIR/arm64-v8a"
SSL_TARGET="android-arm64"
;;
esac

rm -rf $DESTDIR
build_the_thing
#### copy libraries and includes to output-directory #####
mkdir -p $OUT_DIR/inc/$build_target
cp -R $DESTDIR/usr/local/include/* $OUT_DIR/inc/$build_target
mkdir -p $OUT_DIR/lib/$build_target
cp -R $DESTDIR/usr/local/lib/*.so $OUT_DIR/lib/$build_target
done

echo Success

How to cross compile OpenSSL for 64 bit Android on OSX Darwin using NDK 19

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.

Compiling openssl for android - warning: shared library text segment not shareable

I was able to get rid of this issue by using the following scripts for building openssl:

https://github.com/xvtom/build-openssl-android

Also refer this:

https://wiki.openssl.org/index.php/Android

How to build OpenSSL 1.1.1 for 64 bit Android using NDK 17

The scripts and examples on OpenSSL Wiki are badly outdated. The specific build script does not support arm64.

Luckily, GitHub comes to rescue: try https://github.com/noloader/Android-PRNG/blob/master/setenv-android.sh.



Related Topics



Leave a reply



Submit