自从NDK的r19开始,由于gcc兼容clang的编译方式有问题,该版本已经移除了相关gcc文件,所以用老方法交叉编译Openssl的时候,会提示找不到gcc文件。
实际现在是直接使用clang编译即可,具体方法如下:
1.下载NDK
https://developer.android.com/ndk/downloads/index.html
2.下载openssl
https://www.openssl.org/source/
3.设置相关环境变量
# Set directory
SCRIPTPATH=`realpath .`
export ANDROID_NDK_HOME=$SCRIPTPATH/android-ndk-r20
OPENSSL_DIR=$SCRIPTPATH/openssl-1.1.1c
4.建立toolchains_path.py来获取设置toolchains_path
#!/usr/bin/env python
"""
Get the toolchains path
"""
import argparse
import atexit
import inspect
import os
import shutil
import stat
import sys
import textwrap
def get_host_tag_or_die():
"""Return the host tag for this platform. Die if not supported."""
if sys.platform.startswith('linux'):
return 'linux-x86_64'
elif sys.platform == 'darwin':
return 'darwin-x86_64'
elif sys.platform == 'win32' or sys.platform == 'cygwin':
host_tag = 'windows-x86_64'
if not os.path.exists(os.path.join(NDK_DIR, 'prebuilt', host_tag)):
host_tag = 'windows'
return host_tag
sys.exit('Unsupported platform: ' + sys.platform)
def get_toolchain_path_or_die(ndk, host_tag):
"""Return the toolchain path or die."""
toolchain_path = os.path.join(ndk, 'toolchains/llvm/prebuilt',
host_tag)
if not os.path.exists(toolchain_path):
sys.exit('Could not find toolchain: {}'.format(toolchain_path))
return toolchain_path
def main():
"""Program entry point."""
parser = argparse.ArgumentParser(description='Optional app description')
parser.add_argument('--ndk', required=True,
help='The NDK Home directory')
args = parser.parse_args()
host_tag = get_host_tag_or_die()
toolchain_path = get_toolchain_path_or_die(args.ndk, host_tag)
print toolchain_path
if __name__ == '__main__':
main()
5.设置toolchains_path
toolchains_path=$(python toolchains_path.py --ndk ${ANDROID_NDK_HOME})
6.进行OPENSSL环境配置
# Set compiler clang, instead of gcc by default
CC=clang# Add toolchains bin directory to PATH
PATH=$toolchains_path/bin:$PATH# Set the Android API levels
ANDROID_API=21# Set the target architecture
# Can be android-arm, android-arm64, android-x86, android-x86 etc
architecture=android-arm
7.make file
# Create the make file
cd ${OPENSSL_DIR}
./Configure ${architecture} -D__ANDROID_API__=$ANDROID_API
8.build
make
9.拷贝到对应目录
# Copy the outputs
OUTPUT_INCLUDE=$SCRIPTPATH/output/include
OUTPUT_LIB=$SCRIPTPATH/output/lib/${architecture}
mkdir -p $OUTPUT_INCLUDE
mkdir -p $OUTPUT_LIB
cp -RL include/openssl $OUTPUT_INCLUDE
cp libcrypto.so $OUTPUT_LIB
cp libcrypto.a $OUTPUT_LIB
cp libssl.so $OUTPUT_LIB
10.以上的代码整合到一个脚本如下:
#!/bin/bash
set -e
set -x
# Set directory
SCRIPTPATH=`realpath .`
export ANDROID_NDK_HOME=$SCRIPTPATH/android-ndk-r20
OPENSSL_DIR=$SCRIPTPATH/openssl-1.1.1c
# Find the toolchain for your build machine
toolchains_path=$(python toolchains_path.py --ndk ${ANDROID_NDK_HOME})
# Configure the OpenSSL environment, refer to NOTES.ANDROID in OPENSSL_DIR
# Set compiler clang, instead of gcc by default
CC=clang
# Add toolchains bin directory to PATH
PATH=$toolchains_path/bin:$PATH
# Set the Android API levels
ANDROID_API=21
# Set the target architecture
# Can be android-arm, android-arm64, android-x86, android-x86 etc
architecture=android-arm
# Create the make file
cd ${OPENSSL_DIR}
./Configure ${architecture} -D__ANDROID_API__=$ANDROID_API
# Build
make
# Copy the outputs
OUTPUT_INCLUDE=$SCRIPTPATH/output/include
OUTPUT_LIB=$SCRIPTPATH/output/lib/${architecture}
mkdir -p $OUTPUT_INCLUDE
mkdir -p $OUTPUT_LIB
cp -RL include/openssl $OUTPUT_INCLUDE
cp libcrypto.so $OUTPUT_LIB
cp libcrypto.a $OUTPUT_LIB
cp libssl.so $OUTPUT_LIB
cp libssl.a $OUTPUT_LIB
具体参考Tutorial: Compile OpenSSL 1.1.1 for Android application