以下載鏈接 https://www.openssl.org/source/openssl-1.1.1o.tar.gz 為例
下載解壓之后,查看根目錄的 NOTES.Android,其中有
export ANDROID_NDK_HOME=/home/whoever/Android/android-sdk/ndk/20.0.5594570
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 android-arm64 -D__ANDROID_API__=29
make
這里就是最直接的編譯方式了。但是這樣編譯之后,會有一個坑
lrwxrwxrwx 1 ubuntu ubuntu 16 Jun 9 21:23 libcrypto.so -> libcrypto.so.1.1
-rwxrwxr-x 1 ubuntu ubuntu 2754216 Jun 9 21:23 libcrypto.so.1.1
lrwxrwxrwx 1 ubuntu ubuntu 13 Jun 9 21:23 libssl.so -> libssl.so.1.1
-rwxrwxr-x 1 ubuntu ubuntu 596600 Jun 9 21:23 libssl.so.1.1
編譯時嘗試鏈接 libssl.so ,實際上會鏈接到 libssl.so.1.1 ,但是當你嘗試將 libssl.so.1.1 集成到 Android studio 工程內時,會發現 libssl.so.1.1 這種命名格式so,Android studio 是不會自動將其打包到 apk 內的,即使將 libssl.so.1.1 改名為 libssl.so 可以集成到 apk ,App運行時仍然會去找 libssl.so.1.1。因為 so 內部有其文件名信息
$objdump -p libssl.so | grep SONAME
SONAME libssl.so.1.1
對于Android來說,我們更希望編譯之后 so 本身名字就叫 libssl.so。
具體做法是解壓 openssl-1.1.1o.tar.gz 之后,修改 15-android.conf
大概 193 行
my %targets = (
"android" => {
inherit_from => [ "linux-generic32" ],
template => 1,
################################################################
# Special note about -pie. The underlying reason is that
# Lollipop refuses to run non-PIE. But what about older systems
# and NDKs? -fPIC was never problem, so the only concern is -pie.
# Older toolchains, e.g. r4, appear to handle it and binaries
# turn out mostly functional. "Mostly" means that oldest
# Androids, such as Froyo, fail to handle executable, but newer
# systems are perfectly capable of executing binaries targeting
# Froyo. Keep in mind that in the nutshell Android builds are
# about JNI, i.e. shared libraries, not applications.
cflags => add(sub { android_ndk()->{cflags} }),
cppflags => add(sub { android_ndk()->{cppflags} }),
cxxflags => add(sub { android_ndk()->{cflags} }),
bn_ops => sub { android_ndk()->{bn_ops} },
bin_cflags => "-pie",
enable => [ ],
shared_extension => ".so", ### 這一行是新加的
},
這樣可以編譯出來不帶版本后綴的 libssl.so 和 libcrypto.so
可以參考下邊的懶人腳本來自行編譯
#!/bin/bash
function buildopenssl()
{
androidarch=$1
toolchain=$2
if [ ! -f openssl-1.1.1o.tar.gz ]; then
wget https://www.openssl.org/source/openssl-1.1.1o.tar.gz
fi
if [ !-d openssl-1.1.1o ]; then
tar -xf openssl-1.1.1o.tar.gz
fi
if [ -z $ANDROID_NDK_HOME ]; then
echo "missing ANDROID_NDK_HOME"
exit
fi
pushd openssl-1.1.1o
PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin:$ANDROID_NDK_HOME/toolchains/${toolchain}-linux-androideabi-4.9/prebuilt/linux-x86_64/bin:$PATH
./Configure android-$androidarch -D__ANDROID_API__=26
make clean
make
popd
}
function backuplibs()
{
androidabi=$1
mkdir -p jnilibs/$androidabi
cp -f openssl-1.1.1o/libssl.so jnilibs/$androidabi
cp -f openssl-1.1.1o/libssl.a jnilibs/$androidabi
cp -f openssl-1.1.1o/libcrypto.so jnilibs/$androidabi
cp -f openssl-1.1.1o/libcrypto.a jnilibs/$androidabi
}
buildopenssl arm64 aarch64
backuplibs arm64-v8a
buildopenssl arm arm
backuplibs armeabi-v7a
buildopenssl x86 x86
backuplibs x86
buildopenssl x86_64 x86_64
backuplibs x86_64
rm -rf jnilibs/include
cp -rf openssl-1.1.1o/include jnilibs
使用時,將 NDK_HOME傳入,例如,腳本保存為 build.sh,則執行命令
ANDROID_NDK_HOME=/home/whoever/android-ndk-r21e ./build_android_openssl.sh
想為 Android (arm/x86/x86_64)編譯時,修改腳本末尾的 buildopenssl 注釋 作者:BDZNH https://www.bilibili.com/read/cv17026966 出處:bilibili