为 Android 编译最新的 OpenSSL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11929773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:11:55  来源:igfitidea点击:

Compiling the latest OpenSSL for Android

androidandroid-ndkopenssl

提问by Suman

I am trying to generate the shared library for the (.so) files of the OpenSSL1.0.1c for the Android. I found that they have added three options for compiling for the Android in the android script.

我正在尝试为 Android 的 OpenSSL1.0.1c 的 (.so) 文件生成共享库。我发现他们在android脚本中添加了三个用于编译Android的选项。

./Configure android-armv7  (or)
./Configure android-x86    (or)
./Configure android

once I configured for the OS and then try to compile, its throwing errors. Currently I am working x86 windows7 and installed Cygwin, Android sdk R20, Android NDK r8

一旦我为操作系统配置好然后尝试编译,它就会抛出错误。目前我正在使用 x86 windows7 并安装了 Cygwin、Android sdk R20、Android NDK r8

sh-4.1$ make
making all in crypto...
make[1]: Entering directory `/cygdrive/d/SourceCodes/OpenSSL/openssl-1.0.1c/crypto'
gcc -I. -I.. -I../include  -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -march=armv7-a -mandroid -I/include -B/lib -O3 -fomit-frame-pointer -Wall -DOPENSSL_BN_ASM_MONT -DOP
ENSSL_BN_ASM_GF2m -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DAES_ASM -DGHASH_ASM   -c -o cryptlib.o cryptlib.c
cc1: error: unrecognized command line option "-mandroid"
cryptlib.c:1:0: error: bad value (armv7-a) for -march= switch
<builtin>: recipe for target `cryptlib.o' failed
make[1]: *** [cryptlib.o] Error 1
make[1]: Leaving directory `/cygdrive/d/SourceCodes/OpenSSL/openssl-1.0.1c/crypto'
Makefile:278: recipe for target `build_crypto' failed
make: *** [build_crypto] Error 1
sh-4.1$

Please let me know if anyone faced the similar issue and got the solution for resolving the same.

请让我知道是否有人遇到过类似的问题并获得了解决相同问题的解决方案。

回答by mchiasson

I would seriously not advise to grab anything outside of the official OpenSSL web site. You cannot take a chance when dealing with cryptography and security.

我真的不建议在官方 OpenSSL 网站之外抓取任何东西。在处理密码学和安全性时,您不能冒险。

The only problem that I see is that you are using your host's gcc rather than using android's cross compilers.

我看到的唯一问题是您使用的是主机的 gcc 而不是 android 的交叉编译器。

Here is how I would compile the official OpenSSL on Ubuntu 14.04LTS (this works with OpenSSL 1.0.1g)

这是我在 Ubuntu 14.04LTS 上编译官方 OpenSSL 的方法(这适用于 OpenSSL 1.0.1g)

From your home folder, run the following commands:

从您的主文件夹,运行以下命令:

tar xzvf ~/Downloads/openssl-1.0.1g.tar.gz
cd openssl-1.0.1g
export NDK=~/android-ndk-r9d
$NDK/build/tools/make-standalone-toolchain.sh --platform=android-9 --toolchain=arm-linux-androideabi-4.6 --install-dir=`pwd`/android-toolchain-arm
export TOOLCHAIN_PATH=`pwd`/android-toolchain-arm/bin
export TOOL=arm-linux-androideabi
export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
export CC=$NDK_TOOLCHAIN_BASENAME-gcc
export CXX=$NDK_TOOLCHAIN_BASENAME-g++
export LINK=${CXX}
export LD=$NDK_TOOLCHAIN_BASENAME-ld
export AR=$NDK_TOOLCHAIN_BASENAME-ar
export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
export ARCH_FLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16"
export ARCH_LINK="-march=armv7-a -Wl,--fix-cortex-a8"
export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
export LDFLAGS=" ${ARCH_LINK} "

And then run your configure script:

然后运行您的配置脚本:

./Configure android-armv7

And then build

然后构建

PATH=$TOOLCHAIN_PATH:$PATH make

You should see that it is using arm-linux-androideabi-gccinstead of gcc

你应该看到它正在使用arm-linux-androideabi-gcc而不是gcc

To build for the old armeabi:

为旧的 armeabi 构建:

tar xzvf ~/Downloads/openssl-1.0.1g.tar.gz
cd openssl-1.0.1g
export NDK=~/android-ndk-r9d
$NDK/build/tools/make-standalone-toolchain.sh --platform=android-9 --toolchain=arm-linux-androideabi-4.6 --install-dir=`pwd`/android-toolchain-arm
export TOOLCHAIN_PATH=`pwd`/android-toolchain-arm/bin
export TOOL=arm-linux-androideabi
export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
export CC=$NDK_TOOLCHAIN_BASENAME-gcc
export CXX=$NDK_TOOLCHAIN_BASENAME-g++
export LINK=${CXX}
export LD=$NDK_TOOLCHAIN_BASENAME-ld
export AR=$NDK_TOOLCHAIN_BASENAME-ar
export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
export ARCH_FLAGS="-mthumb"
export ARCH_LINK=
export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
export LDFLAGS=" ${ARCH_LINK} "
./Configure android
make clean
PATH=$TOOLCHAIN_PATH:$PATH make

to build for x86 :

为 x86 构建:

tar xzvf ~/Downloads/openssl-1.0.1g.tar.gz
cd openssl-1.0.1g
export NDK=~/android-ndk-r9d
$NDK/build/tools/make-standalone-toolchain.sh --platform=android-9 --toolchain=x86-4.6 --install-dir=`pwd`/android-toolchain-x86
export TOOLCHAIN_PATH=`pwd`/android-toolchain-x86/bin
export TOOL=i686-linux-android
export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
export CC=$NDK_TOOLCHAIN_BASENAME-gcc
export CXX=$NDK_TOOLCHAIN_BASENAME-g++
export LINK=${CXX}
export LD=$NDK_TOOLCHAIN_BASENAME-ld
export AR=$NDK_TOOLCHAIN_BASENAME-ar
export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
export ARCH_FLAGS="-march=i686 -msse3 -mstackrealign -mfpmath=sse"
export ARCH_LINK=
export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
export LDFLAGS=" ${ARCH_LINK} "
./Configure android-x86
make clean
PATH=$TOOLCHAIN_PATH:$PATH make

回答by James Moore

In OpenSSL 1.0.1e, all I need to do is:

在 OpenSSL 1.0.1e 中,我需要做的就是:

CC=~/android-ndk-r9/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-gcc ./Configure android-armv7
ANDROID_DEV=~/android-ndk-r9//platforms/android-8/arch-arm/usr make build_libs

回答by Alex Gotev

Thanks to the instructions posted here, plus some other additions, I've made an automated script which compiles the latest OpenSSL library for Android with support for: armeabi, armeabi-v7a, x86, x86_64 and arm64-v8a:

多亏了这里发布的说明以及其他一些补充,我制作了一个自动化脚本,它可以编译最新的 Android OpenSSL 库,并支持:armeabi、armeabi-v7a、x86、x86_64 和 arm64-v8a:

#!/bin/bash -e
#@author Aleksandar Gotev ([email protected])
#Hints and code taken also from http://stackoverflow.com/questions/11929773/compiling-the-latest-openssl-for-android

if [ "$#" -ne 6 ]
then
    echo "Usage:"
    echo "./openssl-build <ANDROID_NDK_PATH> <OPENSSL_SOURCES_PATH> <ANDROID_TARGET_API> \"
    echo "                <ANDROID_TARGET_ABI> <GCC_VERSION> <OUTPUT_PATH>"
    echo
    echo "Supported target ABIs: armeabi, armeabi-v7a, x86, x86_64, arm64-v8a"
    echo
    echo "Example using GCC 4.8, NDK 10e, OpenSSL 1.0.2d and Android API 21 for armeabi-v7a."
    echo "./openssl-build /home/user/android-ndk-r10e \"
    echo "                /home/user/openssl-1.0.2d \"
    echo "                21 \"
    echo "                armeabi-v7a \"
    echo "                4.8 \"
    echo "                /home/user/output/armeabi-v7a"
    exit 1
fi

NDK_DIR=
OPENSSL_BASE_FOLDER=
OPENSSL_TARGET_API=
OPENSSL_TARGET_ABI=
OPENSSL_GCC_VERSION=
OPENSSL_OUTPUT_PATH=

NDK_MAKE_TOOLCHAIN="$NDK_DIR/build/tools/make-standalone-toolchain.sh"
OPENSSL_TMP_FOLDER="/tmp/openssl"
rm -rf "$OPENSSL_TMP_FOLDER"
mkdir -p "$OPENSSL_TMP_FOLDER"
cp -r ${OPENSSL_BASE_FOLDER} ${OPENSSL_TMP_FOLDER}

function build_library {
    mkdir -p ${OPENSSL_OUTPUT_PATH}
    export PATH=$TOOLCHAIN_PATH:$PATH
    make && make install
    rm -rf ${OPENSSL_TMP_FOLDER}
    echo "Build completed! Check output libraries in ${OPENSSL_OUTPUT_PATH}"
}

if [ "$OPENSSL_TARGET_ABI" == "armeabi-v7a" ]
then
    ${NDK_MAKE_TOOLCHAIN} --platform=android-${OPENSSL_TARGET_API} \
                          --toolchain=arm-linux-androideabi-${OPENSSL_GCC_VERSION} \
                          --install-dir="${OPENSSL_TMP_FOLDER}/android-toolchain-arm"
    export TOOLCHAIN_PATH="${OPENSSL_TMP_FOLDER}/android-toolchain-arm/bin"
    export TOOL=arm-linux-androideabi
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=$NDK_TOOLCHAIN_BASENAME-gcc
    export CXX=$NDK_TOOLCHAIN_BASENAME-g++
    export LINK=${CXX}
    export LD=$NDK_TOOLCHAIN_BASENAME-ld
    export AR=$NDK_TOOLCHAIN_BASENAME-ar
    export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
    export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
    export ARCH_FLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16"
    export ARCH_LINK="-march=armv7-a -Wl,--fix-cortex-a8"
    export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
    export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export LDFLAGS=" ${ARCH_LINK} "
    cd ${OPENSSL_TMP_FOLDER}
    ./Configure android-armv7 --openssldir=${OPENSSL_OUTPUT_PATH}
    build_library

elif [ "$OPENSSL_TARGET_ABI" == "arm64-v8a" ]
then
    ${NDK_MAKE_TOOLCHAIN} --platform=android-${OPENSSL_TARGET_API} \
                          --toolchain=aarch64-linux-android-4.9 \
                          --install-dir="${OPENSSL_TMP_FOLDER}/android-toolchain-arm64"
    export TOOLCHAIN_PATH="${OPENSSL_TMP_FOLDER}/android-toolchain-arm64/bin"
    export TOOL=aarch64-linux-android
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=$NDK_TOOLCHAIN_BASENAME-gcc
    export CXX=$NDK_TOOLCHAIN_BASENAME-g++
    export LINK=${CXX}
    export LD=$NDK_TOOLCHAIN_BASENAME-ld
    export AR=$NDK_TOOLCHAIN_BASENAME-ar
    export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
    export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
    export ARCH_FLAGS=
    export ARCH_LINK=
    export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
    export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export LDFLAGS=" ${ARCH_LINK} "
    cd ${OPENSSL_TMP_FOLDER}
    ./Configure android --openssldir=${OPENSSL_OUTPUT_PATH}
    build_library

elif [ "$OPENSSL_TARGET_ABI" == "armeabi" ]
then
    ${NDK_MAKE_TOOLCHAIN} --platform=android-${OPENSSL_TARGET_API} \
                          --toolchain=arm-linux-androideabi-${OPENSSL_GCC_VERSION} \
                          --install-dir="${OPENSSL_TMP_FOLDER}/android-toolchain-arm"
    export TOOLCHAIN_PATH="${OPENSSL_TMP_FOLDER}/android-toolchain-arm/bin"
    export TOOL=arm-linux-androideabi
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=$NDK_TOOLCHAIN_BASENAME-gcc
    export CXX=$NDK_TOOLCHAIN_BASENAME-g++
    export LINK=${CXX}
    export LD=$NDK_TOOLCHAIN_BASENAME-ld
    export AR=$NDK_TOOLCHAIN_BASENAME-ar
    export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
    export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
    export ARCH_FLAGS="-mthumb"
    export ARCH_LINK=
    export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
    export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export LDFLAGS=" ${ARCH_LINK} "
    cd ${OPENSSL_TMP_FOLDER}
    ./Configure android --openssldir=${OPENSSL_OUTPUT_PATH}
    build_library

elif [ "$OPENSSL_TARGET_ABI" == "x86" ]
then
    ${NDK_MAKE_TOOLCHAIN} --platform=android-${OPENSSL_TARGET_API} \
                          --toolchain=x86-${OPENSSL_GCC_VERSION} \
                          --install-dir="${OPENSSL_TMP_FOLDER}/android-toolchain-x86"
    export TOOLCHAIN_PATH="${OPENSSL_TMP_FOLDER}/android-toolchain-x86/bin"
    export TOOL=i686-linux-android
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=$NDK_TOOLCHAIN_BASENAME-gcc
    export CXX=$NDK_TOOLCHAIN_BASENAME-g++
    export LINK=${CXX}
    export LD=$NDK_TOOLCHAIN_BASENAME-ld
    export AR=$NDK_TOOLCHAIN_BASENAME-ar
    export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
    export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
    export ARCH_FLAGS="-march=i686 -msse3 -mstackrealign -mfpmath=sse"
    export ARCH_LINK=
    export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
    export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export LDFLAGS=" ${ARCH_LINK} "
    cd ${OPENSSL_TMP_FOLDER}
    ./Configure android-x86 --openssldir=${OPENSSL_OUTPUT_PATH}
    build_library

elif [ "$OPENSSL_TARGET_ABI" == "x86_64" ]
then
    ${NDK_MAKE_TOOLCHAIN} --platform=android-${OPENSSL_TARGET_API} \
                          --toolchain=x86_64-4.9 \
                          --install-dir="${OPENSSL_TMP_FOLDER}/android-toolchain-x86_64"
    export TOOLCHAIN_PATH="${OPENSSL_TMP_FOLDER}/android-toolchain-x86_64/bin"
    export TOOL=x86_64-linux-android
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=$NDK_TOOLCHAIN_BASENAME-gcc
    export CXX=$NDK_TOOLCHAIN_BASENAME-g++
    export LINK=${CXX}
    export LD=$NDK_TOOLCHAIN_BASENAME-ld
    export AR=$NDK_TOOLCHAIN_BASENAME-ar
    export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
    export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
    export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
    export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export LDFLAGS=" ${ARCH_LINK} "
    cd ${OPENSSL_TMP_FOLDER}
    ./Configure linux-x86_64 --openssldir=${OPENSSL_OUTPUT_PATH}
    build_library

else
    echo "Unsupported target ABI: $OPENSSL_TARGET_ABI"
    exit 1
fi

Script docs: https://github.com/alexbbb/pjsip-android-builder#build-only-openssl

脚本文档:https: //github.com/alexbbb/pjsip-android-builder#build-only-openssl

For the last version check: https://github.com/alexbbb/pjsip-android-builder/blob/master/openssl-build

最后一个版本检查:https: //github.com/alexbbb/pjsip-android-builder/blob/master/openssl-build

回答by ph4r05

I glued together some useful advices here to a build environment for OpenSSL for Android which works for me.

我在这里将一些有用的建议粘在一起,以构建适合我的 OpenSSL for Android 构建环境。

  • Supports build for multiple architectures - ARM, ARMv7, X86
  • Uses OpenSSL source codes
  • Integrated with Android.mk build
  • Contains pre-compiled OpenSSL 1.0.2h (use if you want or compile your own)
  • 支持构建多种架构 - ARM、ARMv7、X86
  • 使用 OpenSSL 源代码
  • 与 Android.mk 构建集成
  • 包含预编译的 OpenSSL 1.0.2h(如果需要,请使用或编译自己的)

https://github.com/ph4r05/android-openssl

https://github.com/ph4r05/android-openssl

回答by R. Zagórski

In case someone encounters the problem of using vulnerable version of OpenSSL (< 1.0.2f/1.0.1r) in one of the native libraries, I add some more details and instructions.

如果有人遇到< 1.0.2f/1.0.1r在其中一个本机库中使用易受攻击的 OpenSSL ( )版本的问题,我会添加更多详细信息和说明。

Preconditions: Android NDK need to be configured first.

前提条件:首先需要配置Android NDK。

  1. First of all, download the OpenSSLcompatible version (> 1.0.2f/1.0.1r).
  2. Download two scripts from this link. In case someone wonders what they do (and you should - it is a cryptographic library!!!): They build the OpenSSLlibrary for every android ABI processor architecture (armeabi, x86, mips, etc...)

  3. Modify setenv-android-mod.sh -> line 18with the ndk version

  4. Modify setenv-android-mod.sh -> line 40with the Android API version

  5. Modify build-all-arch.sh -> line 7with the folder name of the OpenSSLlibrary (in my case it was openssl-1.0.1t)

  6. After successful build, inside the folder distthe libraries will be present

  1. 首先,下载OpenSSL兼容版本(> 1.0.2f/1.0.1r)。
  2. 从此链接下载两个脚本。万一有人奇观他们做什么(你应该-这是一个密码库!!!):他们建立OpenSSL的每一个Android ABI的处理器架构库(armeabix86mips,等...)

  3. 修改 setenv-android-mod.sh ->18与 ndk 版本一致

  4. 修改setenv-android-mod.sh ->40与Android API 版本一致

  5. 7使用OpenSSL库的文件夹名称修改 build-all-arch.sh -> 行(在我的情况下是openssl-1.0.1t

  6. 成功构建后,文件夹内dist将出现库

To add the openSSLto project as prebuilt static libraries, create:

要将openSSL项目添加为预构建的静态库,请创建:

  1. opensslfolder under jnidirectory containing lib/(which contain the .afiles for the supported architectures),
  2. include/which has the necessary includes (you can find that under the openssl version you downloaded, be aware that some of the header files are symbolic links)
  3. Modify Android.mkinside jnifolder adding the following:

    include $(CLEAR_VARS) 
    LOCAL_MODULE := libssl
    LOCAL_SRC_FILES := lib/$(TARGET_ARCH_ABI)/libssl.a
    include $(PREBUILT_STATIC_LIBRARY)
    include $(CLEAR_VARS)
    LOCAL_MODULE := libcrypto
    LOCAL_SRC_FILES := lib/$(TARGET_ARCH_ABI)/libcrypto.a
    include $(PREBUILT_STATIC_LIBRARY)
    
  1. openssljni包含目录下的文件夹lib/(包含.a支持架构的文件),
  2. include/其中有必要的includes(你可以在你下载的openssl版本下找到,注意一些头文件是符号链接)
  3. 修改Android.mk内部jni文件夹添加以下内容:

    include $(CLEAR_VARS) 
    LOCAL_MODULE := libssl
    LOCAL_SRC_FILES := lib/$(TARGET_ARCH_ABI)/libssl.a
    include $(PREBUILT_STATIC_LIBRARY)
    include $(CLEAR_VARS)
    LOCAL_MODULE := libcrypto
    LOCAL_SRC_FILES := lib/$(TARGET_ARCH_ABI)/libcrypto.a
    include $(PREBUILT_STATIC_LIBRARY)
    

Then, to use the library within another jnimodule add the following to it's Android.mkfile:

然后,要在另一个jni模块中使用该库,请将以下内容添加到它的Android.mk文件中:

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../openssl/include
LOCAL_STATIC_LIBRARIES := libssl libcrypto

回答by peceps

Here is how to build OpenSSL on Windows using Cygwin and Android NDK

以下是如何使用 Cygwin 和 Android NDK 在 Windows 上构建 OpenSSL

  1. Download and extract OpenSSL source
  2. Download script from https://github.com/pstanoev/OpenSSL-For-Android
  3. Run cmdwith Administrator user and execute

    SET CYGWIN=winsymlinks:nativestrict

  4. In same cmd window, open Cygwin:

    cygwin.bat

  5. Make script executable:

    chmod +x *.sh

  6. In Cygwin execute:

    /cygdrive/c/dev/openssl-source/openssl-build.sh /cygdrive/c/dev/android-ndk-r12b /cygdrive/c/dev/openssl-source/openssl-1.0.2j 15 armeabi-v7a 4.9 /home/user/openssl

    • Modify for your locations of Android NDK and sources
  7. Copy libcrypto.so and include/* files to this module. Use cp -rfor copy to follow links.

  1. 下载并解压 OpenSSL 源代码
  2. https://github.com/pstanoev/OpenSSL-For-Android下载脚本
  3. cmd以管理员用户运行并执行

    SET CYGWIN=winsymlinks:nativestrict

  4. 在同一个 cmd 窗口中,打开 Cygwin:

    cygwin.bat

  5. 使脚本可执行:

    chmod +x *.sh

  6. 在 Cygwin 中执行:

    /cygdrive/c/dev/openssl-source/openssl-build.sh /cygdrive/c/dev/android-ndk-r12b /cygdrive/c/dev/openssl-source/openssl-1.0.2j 15 armeabi-v7a 4.9 /home/user/openssl

    • 修改您的 Android NDK 和源位置
  7. 将 libcrypto.so 和 include/* 文件复制到此模块。使用cp -r拷贝到跟踪链接。

回答by Cristy

This doesn't solve your problem, but perhaps it will help. A google groups post where they have successfully compiled OpenSSL 1.0.1 beta2 for Android.

这不能解决您的问题,但也许会有所帮助。一个 google 组帖子,他们成功地为 Android 编译了 OpenSSL 1.0.1 beta2。

https://groups.google.com/forum/?fromgroups#!topic/android-security-discuss/5-_gCAmEo-M

https://groups.google.com/forum/?fromgroups#!topic/android-security-discuss/5-_gCAmEo-M

This might also help, an open source project that builds 1.0.0a for Android:

这也可能有帮助,一个为 Android 构建 1.0.0a 的开源项目:

https://github.com/eighthave/openssl-android

https://github.com/eighthave/openssl-android

回答by mangu23

This is how I did it for openssl-1.0.2g:

这是我为 openssl-1.0.2g 所做的:

$ rm -rf openssl-1.0.2g/
$ tar xzf openssl-1.0.2g.tar.gz
$ chmod a+x setenv-android.sh
$ . ./setenv-android.sh ---> Note: make sure in the same folder of your openssl-1.0.2g
$ cd openssl-1.0.2g/

$ perl -pi -e 's/install: all install_docs install_sw/install: install_docs install_sw/g' Makefile.org

$ ./config shared no-ssl2 no-ssl3 no-comp no-hw no-engine --openssldir=<Path of your OpenSSL> 

$ make depend
$ make clean
$ make all
before make install, ---Delete the "include" folder (path/of/your/openssl-1.0.2g/include)  or you may move it to another directory for safe keeping. 
$ make install 

Make sure that you input the right NDK paths into your setenv-android.sh or else you will have errors.

确保在 setenv-android.sh 中输入正确的 NDK 路径,否则会出错。

Example for this build I used Android NDK vr10b (http://dl.google.com/android/ndk/android-ndk32-r10b-darwin-x86.tar.bz2) and used the ff path values inside my setenv-android.sh file:

此构建的示例我使用了 Android NDK vr10b(http://dl.google.com/android/ndk/android-ndk32-r10b-darwin-x86.tar.bz2)并在我的 setenv-android 中使用了 ff 路径值。 .sh 文件:

_ANDROID_NDK="android-ndk-r10b" (Line 12)
_ANDROID_EABI="arm-linux-androideabi-4.8"(Line 16)
_ANDROID_API="android-19"(Line 24)

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

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

Note --->I created a github commit to answer this question, please see: https://github.com/rjmangubat23/OpenSSL

注意--->我创建了一个github提交来回答这个问题,请参阅:https: //github.com/rjmangubat23/OpenSSL

I uploaded the different scripts for setenv-android.sh onto my github, cause you will need different type of scripts for different architectures:

我将 setenv-android.sh 的不同脚本上传到了我的 github 上,因为不同的架构需要不同类型的脚本:

For x86: https://github.com/rjmangubat23/OpenSSL/tree/master/x86

对于 x86:https: //github.com/rjmangubat23/OpenSSL/tree/master/x86

For ArmV7: https://github.com/rjmangubat23/OpenSSL/tree/master/ArmV7

对于 ArmV7:https: //github.com/rjmangubat23/OpenSSL/tree/master/ArmV7

Download Openssl here: ftp://ftp.openssl.org/source

在此处下载 Openssl:ftp: //ftp.openssl.org/source

Download Complete list of Android NDK files here: https://github.com/taka-no-me/android-cmake/blob/master/ndk_links.md

在此处下载完整的 Android NDK 文件列表:https: //github.com/taka-no-me/android-cmake/blob/master/ndk_links.md

回答by vadim_hr

You can use this scriptto compile openssl under Windows 7 + Cygwin. Everything you need is only change location of ndk and choose android api version.

您可以使用此脚本在 Windows 7 + Cygwin 下编译 openssl。您需要的只是更改 ndk 的位置并选择 android api 版本。

My way step by step (Win 7 x64 + Cygwin + ndk r10c)

我的方式一步一步(Win 7 x64 + Cygwin + ndk r10c)

  1. Copy file setenv-android-mod.shand build-all-arch.shto your openssl directory.

  2. Modify file build-all-arch.sh

    • cd openssl-1.0.1jto #cd openssl-1.0.1j(line 56)

    • cd ..to #cd ..(line 80)

  3. Modify file setend-android-mod.sh

    • _ANDROID_NDK="android-ndk-r10c-x64"change to your ndk version (line 18)
    • _ANDROID_API="android-19"change to your api version (line 40)
    • ANDROID_NDK_ROOT="d:/work/android/android-ndk-r10c-x64"specify your dir (line 42)
    • ANDROID_TOOLCHAIN="d:/work/android/android-ndk-r10c-x64/toolchains"specify your dir (line 43)
    • export CROSS_COMPILE="aarch64-linux-android-"to export CROSS_COMPILE="$ANDROID_TOOLCHAIN/aarch64-linux-android-"(line 219), change same lines 225, 231, 237, 243 - just add $ANDROID_TOOLCHAIN/to the cross compile path.
  1. 复制文件setenv-android-mod.shbuild-all-arch.sh你的OpenSSL目录。

  2. 修改文件 build-all-arch.sh

    • cd openssl-1.0.1j#cd openssl-1.0.1j(第 56 行)

    • cd ..#cd ..(第 80 行)

  3. 修改文件 setend-android-mod.sh

    • _ANDROID_NDK="android-ndk-r10c-x64"更改为您的 ndk 版本(第 18 行)
    • _ANDROID_API="android-19"更改为您的 api 版本(第 40 行)
    • ANDROID_NDK_ROOT="d:/work/android/android-ndk-r10c-x64"指定您的目录(第 42 行)
    • ANDROID_TOOLCHAIN="d:/work/android/android-ndk-r10c-x64/toolchains"指定您的目录(第 43 行)
    • export CROSS_COMPILE="aarch64-linux-android-"export CROSS_COMPILE="$ANDROID_TOOLCHAIN/aarch64-linux-android-"(第 219 行),更改相同的第 225、231、237、243 行 - 只需添加$ANDROID_TOOLCHAIN/到交叉编译路径。

Run build-all-arch.sh.

运行build-all-arch.sh

All libraries (*.so) will be located in /prebuilt/dir.

所有库 ( *.so) 将位于/prebuilt/dir 中。

P.S. I had a few more errors because of wrong symlinks, but everything was fine after executing the following command export CYGWIN="winsymlinks:native"see herefor details.

PS 由于错误的符号链接,我还有一些错误,但执行以下命令后一切正常,export CYGWIN="winsymlinks:native"请参阅此处了解详细信息。

回答by Alexp

This problem and many others like it caused me a week or so of mucking about, but I finally cracked it so I thought I'd share my solution. OpenSSL can be compiled for 21+ and work on Android 4.03 devices if you are willing to hack the code. We use libcurl, so wanted to be up to date. The steps are a bit complex:

这个问题和许多其他类似的问题让我花了一周左右的时间,但我终于破解了它,所以我想我会分享我的解决方案。如果您愿意破解代码,OpenSSL 可以编译为 21+ 并在 Android 4.03 设备上工作。我们使用 libcurl,所以想保持最新状态。步骤有点复杂:

First up ensure you have a buildable version of libcurl. I recommend as a good starting point https://github.com/cocos2d/cocos2d-x-3rd-party-libs-src

首先确保你有一个可构建的 libcurl 版本。我推荐一个很好的起点https://github.com/cocos2d/cocos2d-x-3rd-party-libs-src

They keep up to date build scripts.

他们保持最新的构建脚本。

Firstly hack the android.ini in "build" to version 21

首先将“build”中的 android.ini 破解到版本 21

Next up I had to add the following stub functions to the jni project somewhere:

接下来,我必须将以下存根函数添加到 jni 项目的某处:

// SPECIAL API 21+ functions required for android to build on newer platform targets.

float strtof (const char* str, char** endptr)
{
    return (float)strtod(str, endptr);
}

int __isnanf(float x)
{
    return (x != x);
}

double atof (const char* s)
{
    double rez = 0, fact = 1;
    if (*s == '-'){
        s++;
        fact = -1;
    };
    for (int point_seen = 0; *s; s++){
        if (*s == '.'){
            point_seen = 1;
            continue;
        };
        int d = *s - '0';
        if (d >= 0 && d <= 9){
            if (point_seen) fact /= 10.0f;
            rez = rez * 10.0f + (float)d;
        };
    };
    return rez * fact;
}

void srandom(unsigned int seed)
{
    std::srand(seed);
}

static unsigned long _next = 1;

void srand(unsigned int seed)
{
    _next = seed;
}

long random()
{
    return rand();
}

int rand()
{
    _next = _next * 1103515245 + 12345;
    return (unsigned int)(_next/65536) % 32768;
}

/* the default is bsd */
__sighandler_t signal(int s, __sighandler_t f)
{
    return 0;
}

Note the signal function could be better, but for us it isn't important. This stops the dreaded "cannot locate symbol" errors on 4.03 devices caused by changes to the headers in 5+ (https://groups.google.com/forum/#!topic/android-ndk/RjO9WmG9pfE).

注意信号函数可能会更好,但对我们来说并不重要。这会阻止 4.03 设备上由 5+ 中的标头更改引起的可怕的“无法定位符号”错误(https://groups.google.com/forum/#!topic/android-ndk/RjO9WmG9pfE)。

Next up download the tar.gz for the version of openssl you want to build and unpack it somewhere. Edit crypto/ui/ui_openssl.c and crypto/des/read_pwd.c and ensure that the #defines for tcsetattr are not used. I used a brute force #if 0 and #if 1 - note this should be possible by twiddling the preprocessor, but I gave up at this point. If anyone feels like sharing the correct magic please do!

接下来下载要构建的 openssl 版本的 tar.gz 并将其解压缩到某处。编辑 crypto/ui/ui_openssl.c 和 crypto/des/read_pwd.c 并确保未使用 tcsetattr 的 #defines。我使用了蛮力 #if 0 和 #if 1 - 请注意,这应该可以通过调整预处理器来实现,但此时我放弃了。如果有人想分享正确的魔法,请做!

Now you need to re-tar up the file into the tarballs file (making sure you get it root folder in there:

现在您需要将文件重新 tar 压缩到 tarball 文件中(确保将其放在根文件夹中:

and run (in my case)

并运行(在我的情况下)

shasum -a 512 curl-7.50.0.tar.gz > ../src/curl/SHA512SUMS

shasum -a 512 curl-7.50.0.tar.gz > ../src/curl/SHA512SUMS

This will allow the cocos2d ./build.sh to run. Something like:

这将允许 cocos2d ./build.sh 运行。就像是:

./build.sh -p=android --libs=openssl,curl --arch=armv7 --mode=release

./build.sh -p=android --libs=openssl,curl --arch=armv7 --mode=release

Finally - and this caught me out in terms of making a universal .a file, but is not directly part of the question, ensure you use a version that doesn't use NEON instructions. Tegra 2 devices apparently have FPU but no NEON. I think this can be done using -mfpu=vfp as a compiler flag, but I chose to just use the armv5 build instead, as performance is not really important to me in this area (and I have had enough nasty real world surprises from this already).

最后 - 这在制作通用 .a 文件方面让我感到困惑,但不是问题的直接一部分,请确保您使用不使用 NEON 指令的版本。Tegra 2 设备显然有 FPU 但没有 NEON。我认为这可以使用 -mfpu=vfp 作为编译器标志来完成,但我选择只使用 armv5 构建,因为在这方面性能对我来说并不重要(而且我已经有足够多令人讨厌的现实世界惊喜了)已经)。

In the end you should get a nice .a that works everywhere, and can be used in projects targeting the latest and greatest Android. Now if only someone from the OpenSSL project can read this and fix the project so it understands android stuff released 2 years ago!

最后你应该得到一个很好的 .a ,它可以在任何地方使用,并且可以用于针对最新和最好的 Android 的项目。现在,如果只有 OpenSSL 项目中的某个人可以阅读此内容并修复该项目,以便它了解 2 年前发布的 android 内容!

Good luck!

祝你好运!