NDK编译OpenSSL静态库

openssl 版本 1.1.1h

编译环境:mac os 10.15

编译脚本:

  1. D__ANDROID_API__ 指定最低支持Android版本
  2. darwin-x86_64 为mac版本ndk编译工具路径,需换成对应系统的
  3. MY_OPENSSL_PATH为库的生成路径
#!/bin/bash
set -e
set -x
#
# Copyright 2016 leenjewel
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
 
MY_OPENSSL_PATH=/Users/admin/Downloads/openssl-OpenSSL_1_1_1h/output
func_cmp(){
    make clean &&
    rm -rf $MY_OPENSSL_PATH/OpensslDir/$ARCH &&
    mkdir -p $MY_OPENSSL_PATH/OpensslDir/$ARCH &&
    export PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64/bin:$PATH &&
    ./Configure $ARCH_NAME -D__ANDROID_API__=21 --prefix=$MY_OPENSSL_PATH/OpensslDir/$ARCH &&
    make && make install
}
 
 CC=clang
ARCH_NAME=android-arm
export PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64/bin:$PATH &&
#先生成make一次,为了make clean能够执行
./Configure $ARCH_NAME -D__ANDROID_API__=21 --prefix=$MY_OPENSSL_PATH/OpensslDir/$ARCH
 
 #for ARCH in armeabi-v7a armeabi arm64-v8a x86 x86_64 mips mips64
for ARCH in armeabi-v7a arm64-v8a x86 x86_64
do
    echo $ARCH
    if [ "$ARCH" = "armeabi-v7a" ]; then
        ARCH_NAME=android-arm
    fi
    if [ "$ARCH" = "armeabi" ]; then
        ARCH_NAME=android-arm
    fi
    if [ "$ARCH" = "arm64-v8a" ]; then
        ARCH_NAME=android-arm64
    fi
    if [ "$ARCH" = "mips" ]; then
        ARCH_NAME=android-mips
    fi
    if [ "$ARCH" = "mips64" ]; then
        ARCH_NAME=android-mips64
    fi
    if [ "$ARCH" = "x86" ]; then
        ARCH_NAME=android-x86
    fi
    if [ "$ARCH" = "x86_64" ]; then
        ARCH_NAME=android-x86_64
    fi
    echo $TOOL_CHAIN
    func_cmp
done

注意:
使用的时一定注意库的链接顺序,先 ssl 再 crypto: -lssl -lcrypto,否则会报链接错误:

sl_rsa.c:(.text+0xb0): undefined reference to `X509_get0_pubkey'
ssl_rsa.c:(.text+0xd8): undefined reference to `EVP_PKEY_get0_EC_KEY'
ssl_rsa.c:(.text+0xdc): undefined reference to `EC_KEY_can_sign'
ssl_rsa.c:(.text+0xfc): undefined reference to `EVP_PKEY_copy_parameters'
ssl_rsa.c:(.text+0x100): undefined reference to `ERR_clear_error'
ssl_rsa.c:(.text+0x118): undefined reference to `X509_check_private_key'
ssl_rsa.c:(.text+0x134): undefined reference to `EVP_PKEY_free'
ssl_rsa.c:(.text+0x148): undefined reference to `ERR_clear_error'
ssl_rsa.c:(.text+0x160): undefined reference to `X509_free'
ssl_rsa.c:(.text+0x168): undefined reference to `X509_up_ref'
ssl_rsa.c:(.text+0x1d4): undefined reference to `ERR_put_error'
/Users/admin/Repos/zp-common-sdk/platform/android/jni/../../../../sdk-share-library/libs/android/arm64-v8a/libssl.a(ssl_rsa.o): In function `SSL_use_certificate_file':
ssl_rsa.c:(.text+0x20c): undefined reference to `BIO_s_file'
ssl_rsa.c:(.text+0x210): undefined reference to `BIO_new'
ssl_rsa.c:(.text+0x22c): undefined reference to `BIO_ctrl'
ssl_rsa.c:(.text+0x250): undefined reference to `d2i_X509_bio'
ssl_rsa.c:(.text+0x2dc): undefined reference to `PEM_read_bio_X509'
ssl_rsa.c:(.text+0x31c): undefined reference to `ERR_put_error'

你可能感兴趣的:(NDK编译OpenSSL静态库)