This is how I build openssl version 1.0.2k for Android with NDK 15 for Android Oreo. The target device is the Pixel C, which is an arm64 architecture.
source this:
# Pixel C/dragon/ryu
export ANDROID_HOME=$HOME/android-sdk
export ANDROID_NDK_ROOT=$HOME/android-ndk-r15
export ANDROID_NDK_API=26
export ANDROID_NDK_ARCH=arm64
export ANDROID_NDK_HOST=aarch64-linux-android
export ANDROID_NDK_ABI=arm64-v8a
bash script:
echo "Checking for Android Standalone NDK..."
if [ ! -e $ANDROID_NDK_ROOT-$ANDROID_NDK_ARCH-$ANDROID_NDK_API ]; then
echo "Creating the Android Standalone NDK..."
if [ ! -e $ANDROID_NDK_ROOT ]; then
echo "The NDK directory $ANDROID_NDK_ROOT does not exist, aborting!"
exit 1
fi
$ANDROID_NDK_ROOT/build/tools/make_standalone_toolchain.py --arch=$ANDROID_NDK_ARCH --api=$ANDROID_NDK_API --install-dir=$ANDROID_NDK_ROOT-$ANDROID_NDK_ARCH-$ANDROID_NDK_API --stl=libc++
fi
EXTRA=`pwd`
SOURCE=$EXTRA/source
AUTOTOOLS=$EXTRA/data/autotools
PATCHES=$EXTRA/patches
SYSROOT=$EXTRA/sysroot
DOWNLOAD=$EXTRA/download
JNILIBS=$EXTRA/jniLibs/$ANDROID_NDK_ABI
SHARED_LIB=`find $ANDROID_NDK_ROOT-$ANDROID_NDK_ARCH-$ANDROID_NDK_API -name "*shared.so" | head -1`
mkdir -p $SOURCE $SYSROOT $DOWNLOAD $JNILIBS
echo "Copying shared library to jniLibs..."
cp -f $SHARED_LIB $JNILIBS
echo "Checking for openssl..."
OPENSSL_VERSION=1_0_2k
if [ ! -e $SYSROOT/lib/libssl.so ] || [ ! -e $SYSROOT/lib/libcrypto.so ] || [ ! -e $SYSROOT/include/openssl ]; then
if [ ! -e $SOURCE/openssl-OpenSSL_$OPENSSL_VERSION ]; then
echo "Building openssl..."
if [ ! -e $DOWNLOAD/OpenSSL_$OPENSSL_VERSION.tar.gz ]; then
echo "Downloading openssl..."
pushd $DOWNLOAD
popd
fi
pushd $SOURCE
tar -xvf $DOWNLOAD/OpenSSL_$OPENSSL_VERSION.tar.gz
popd
if [ "$ANDROID_NDK_ARCH" == "x86" ]; then
export MACHINE=i686
else
if [ "$ANDROID_NDK_ARCH" == "arm64" -o "$ANDROID_NDK_ARCH" == "aarch64" ]; then
export MACHINE=aarch64
else
export MACHINE=armv7
fi
fi
export ARCH=$ANDROID_NDK_ARCH
export CROSS_COMPILE=$ANDROID_NDK_HOST-
export SYSTEM=android
export ANDROID_DEV=$ANDROID_NDK_ROOT-$ANDROID_NDK_ARCH-$ANDROID_NDK_API/sysroot/usr
pushd $SOURCE/openssl-OpenSSL_$OPENSSL_VERSION
if [ "$ANDROID_NDK_ARCH" == "arm64" -o "$ANDROID_NDK_ARCH" == "aarch64" ]; then
patch < $PATCHES/openssl-config.patch
patch < $PATCHES/openssl-Configure.patch
fi
./config shared no-ssl2 no-ssl3 no-comp no-hw no-engine no-dso no-asm --prefix=$SYSROOT --openssldir=$SYSROOT
patch < $PATCHES/openssl-Makefiles.patch
make clean
make depend
make CALC_VERSIONS="SHLIB_COMPAT=; SHLIB_SOVER=" MAKE="make -e" all
popd
fi
echo "Installing openssl..."
mkdir -p $SYSROOT/lib
pushd $SOURCE/openssl-OpenSSL_$OPENSSL_VERSION
echo "place-holder make target for avoiding symlinks" >> $SYSROOT/lib/link-shared
make SHLIB_EXT=.so install_sw
rm $SYSROOT/lib/link-shared
popd
fi
openssl-config.patch:
--- config 2017-01-26 07:22:36.000000000 -0600
+++ /home/andrewes/openssl_mods/config 2017-05-08 10:08:02.275587001 -0500
@@ -862,6 +862,7 @@
*-*-qnx6) OUT="QNX6" ;;
x86-*-android|i?86-*-android) OUT="android-x86" ;;
armv[7-9]*-*-android) OUT="android-armv7" ;;
+ aarch64-*-android) OUT="android-aarch64" ;;
*) OUT=`echo $GUESSOS | awk -F- '{print $3}'`;;
esac
openssl-Configure.patch
--- Configure 2017-01-26 07:22:36.000000000 -0600
+++ /home/andrewes/openssl_mods/Configure 2017-05-09 11:16:17.585722000 -0500
@@ -474,6 +474,7 @@
"android","gcc:-mandroid -I\$(ANDROID_DEV)/include -B\$(ANDROID_DEV)/lib -O3 -fomit-frame-pointer -Wall::-D_REENTRANT::-ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL BF_PTR:${no_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)",
"android-x86","gcc:-mandroid -I\$(ANDROID_DEV)/include -B\$(ANDROID_DEV)/lib -O3 -fomit-frame-pointer -Wall::-D_REENTRANT::-ldl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:".eval{my $asm=${x86_elf_asm};$asm=~s/:elf/:android/;$asm}.":dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)",
"android-armv7","gcc:-march=armv7-a -mandroid -I\$(ANDROID_DEV)/include -B\$(ANDROID_DEV)/lib -O3 -fomit-frame-pointer -Wall::-D_REENTRANT::-ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL BF_PTR:${armv4_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)",
+"android-aarch64","clang:-I\$(ANDROID_DEV)/include -B\$(ANDROID_DEV)/lib -O3 -fomit-frame-pointer -Wall::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL BF_PTR:${aarch64_asm}:linux64:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)",
"android-mips","gcc:-mandroid -I\$(ANDROID_DEV)/include -B\$(ANDROID_DEV)/lib -O3 -Wall::-D_REENTRANT::-ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL BF_PTR:${mips32_asm}:o32:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)",
#### *BSD [do see comment about ${BSDthreads} above!]
---
I removed references to the part of the build where I rename the library to avoid colliding with the system version of the same library. Other scripts copy the libraries into a jnilibs directory under the Java project app/src/main directory, where the normal APK packaging in a gradle build gets all the JNI libraries and puts them into the .apk.
Note that under Android Oreo, the path under which the libraries are made available to the application contains a hashed directory component. This component changes each time the apk is installed. The system must be queried to find the library path if a dlopen is to be done.