【问题标题】:How to cross compile OpenSSL for 64 bit Android on OSX Darwin using NDK 19如何使用 NDK 19 在 OSX Darwin 上为 64 位 Android 交叉编译 OpenSSL
【发布时间】:2019-07-06 11:45:52
【问题描述】:

我之前问过基本相同的问题(不同的 NDK)here,并认为我正确构建了 openssl,但一旦我尝试将其链接到我的应用程序,我发现我没有正确构建它。

  1. 如果我从@AlexCohn here 得到答案,我会从setenv_android.sh 脚本开始。

  2. 我修改脚本以设置THE_ARCH=arm64-v8a 尝试针对 64 位 android 架构。

  3. 当我运行脚本时,有些东西找不到:

    ERROR: Failed to find Android cpp. Please edit this script.
    ERROR: Failed to find Android gcc. Please edit this script.
    ERROR: Failed to find Android g++. Please edit this script.
    ERROR: AOSP_STL_INC is not valid. Please edit this script.
    ERROR: AOSP_STL_LIB is not valid. Please edit this script.
    ANDROID_NDK_ROOT: /Users/spartygw/android-ndk-r19/
    AOSP_TOOLCHAIN_PATH: /Users/spartygw/android-ndk-r19//toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/bin
    AOSP_ABI: arm64-v8a
    AOSP_API: android-21
    AOSP_SYSROOT: /Users/spartygw/android-ndk-r19//platforms/android-21/arch-arm64
    AOSP_FLAGS: -funwind-tables -fexceptions -frtti
    AOSP_STL_INC: /Users/spartygw/android-ndk-r19//sources/cxx-stl/stlport/stlport/
    AOSP_STL_LIB: /Users/spartygw/android-ndk-r19//sources/cxx-stl/stlport/libs/arm64-v8a/libstlport_shared.so
    
  4. 当我查看/Users/spartygw/android-ndk-r19//toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/bin 时,没有aarch64-linux-android-cpp-gcc-g++,正如脚本输出所说:

    $ ls -1 ~/android-ndk-r19//toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/bin
    ./
    ../
    aarch64-linux-android-addr2line
    aarch64-linux-android-ar
    aarch64-linux-android-as
    aarch64-linux-android-c++filt
    aarch64-linux-android-dwp
    aarch64-linux-android-elfedit
    aarch64-linux-android-gprof
    aarch64-linux-android-ld
    aarch64-linux-android-ld.bfd
    aarch64-linux-android-ld.gold
    aarch64-linux-android-nm
    aarch64-linux-android-objcopy
    aarch64-linux-android-objdump
    aarch64-linux-android-ranlib
    aarch64-linux-android-readelf
    aarch64-linux-android-size
    aarch64-linux-android-strings
    aarch64-linux-android-strip
    

这就是我认为我上次惹上麻烦的地方。我开始修改脚本以获得似乎可以工作的东西,我确定我现在做的事情是错误的。

我真的不明白这个过程,所以我希望得到帮助。有人成功构建了 OpenSLL 的 arm64-v8a 版本吗?

【问题讨论】:

    标签: android android-ndk openssl


    【解决方案1】:

    我能够使用 NDK r19 为 android arm64 构建、链接和运行 openssl。但我不得不使用从 android-ndk-r19 生成的独立工具链。

    $ cd /path/to/android-ndk-r19
    $ ./build/tools/make-standalone-toolchain.sh               \
                --toolchain=aarch64-linux-android
    

    这将在您的 tmp 目录中生成一个名为 aarch64-linux-android 的目录,将其 bin 目录放在您的路径中。此外,将您的 ANDROID_NDK_HOME 设置为该位置。

    $ export PATH=/path/to/aarch64-linux-android/bin:${PATH}
    $ export ANDROID_NDK_HOME=/path/to/aarch64-linux-android
    

    然后运行 ​​openssl 的 Configure and make。

    $ cd /path/to/openssl1.1.1
    $ ./Configure android-arm64
    $ make
    

    ./Configure 的输出如下:

    $ ./Configure android-arm64
    Configuring OpenSSL version 1.1.1b-dev (0x10101020L) for android-arm64
    Using os-specific seed configuration
    Creating configdata.pm
    Creating Makefile
    
    **********************************************************************
    ***                                                                ***
    ***   OpenSSL has been successfully configured                     ***
    ***                                                                ***
    ***   If you encounter a problem while building, please open an    ***
    ***   issue on GitHub <https://github.com/openssl/openssl/issues>  ***
    ***   and include the output from the following command:           ***
    ***                                                                ***
    ***       perl configdata.pm --dump                                ***
    ***                                                                ***
    ***   (If you are new to OpenSSL, you might want to consult the    ***
    ***   'Troubleshooting' section in the INSTALL file first)         ***
    ***                                                                ***
    **********************************************************************
    $ 
    

    最后我创建了一个新的 Android Studio 项目,它具有 c++11、异常和 rtti 支持(通过新项目向导),并在构建的输出中链接了一个 CMakeLists.txt,该文件稍微修改了一个由 Android Studio 创建:

    cmake_minimum_required(VERSION 3.4.1)
    
    # Creates and names a library, sets it as either STATIC
    # or SHARED, and provides the relative paths to its source code.
    # You can define multiple libraries, and CMake builds them for you.
    # Gradle automatically packages shared libraries with your APK.
    
    add_library( # Sets the name of the library.
                 native-lib
    
                 # Sets the library as a shared library.
                 SHARED
    
                 # Provides a relative path to your source file(s).
                 src/main/cpp/native-lib.cpp )
    
    # Searches for a specified prebuilt library and stores the path as a
    # variable. Because CMake includes system libraries in the search path by
    # default, you only need to specify the name of the public NDK library
    # you want to add. CMake verifies that the library exists before
    # completing its build.
    
    find_library( # Sets the name of the path variable.
                  log-lib
    
                  # Specifies the name of the NDK library that
                  # you want CMake to locate.
                  log )
    
    # HERE ARE THE PARTS I EDITED:
    # NOTE FOR THE COMMANDS ABOVE, THIS IS JUST THE OPENSSL SOURCE DIR.
    set (SSL_PATH /path/to/ssl/build/outputs/)
    include_directories(${SSL_PATH}/include)
    set (open-ssl-libs ${SSL_PATH}/libssl.a ${SSL_PATH}/libcrypto.a)
    
    
    # Specifies libraries CMake should link to your target library. You
    # can link multiple libraries, such as libraries you define in this
    # build script, prebuilt third-party libraries, or system libraries.
    
    target_link_libraries( # Specifies the target library.
                           native-lib
                           # LINK SSL AND CRYPTO HERE:
                            ${open-ssl-libs}
                           # Links the target library to the log library
                           # included in the NDK.
                           ${log-lib} )
    

    这足以表明它是链接的,但我在 Android Studio 生成的样板 c++ 代码中添加了一个对 libssl.a 的小引用:

    #include <jni.h>
    #include <string>
    #include <openssl/ssl.h>
    
    extern "C" JNIEXPORT jstring JNICALL
    Java_com_vernier_android_test_1ssl_MainActivity_stringFromJNI(
            JNIEnv* env,
            jobject /* this */) {
    
        SSL* ssl = SSL_new(nullptr);
        std::string hello = "Hello from C++";
        return env->NewStringUTF(hello.c_str());
    }
    

    我成功运行了应用程序。

    【讨论】:

    • 感谢您的详细回复!当我尝试复制您所做的事情时,我几乎立即遇到了./configure 部分的问题。 openssl 目录中没有./configure,只有./Configure./config。当我运行./Configure android-arm64 时,它告诉我选择操作系统/编译器。如果我运行./config android-arm64,它会警告我,然后失败并显示target already defined - darwin-i386-cc (offending arg: android-arm64)
    • 抱歉,mac 不区分大小写,“./Configure android-arm64”是我使用的。也许您的源版本的操作系统/编译器对略有不同?请检查项目ANDROID.NOTES中的文件,它特别说明要以这种方式使用Configure。只要允许我编辑它,我就会从上面的 ./Configure 添加我的输出:)
    • 我能够为 android-ndk-r19 构建。请参阅我对上面详细答案的更新。
    • openssl 的配置依赖于 sysroot 位于旧的 aarch64 位置,而 bin 目录现在位于 llvm sysroot 中,并且没有 gcc。太多的黑客攻击无法让它为我工作。独立的工具链可以完成这项工作!
    • 同意,需要太多的黑客攻击。独立的工具链就是门票!
    【解决方案2】:

    我在添加 SSL_PATH 时遇到了麻烦。解决它

    set (SSL_PATH ${CMAKE_SOURCE_DIR}/src/main/cpp/openssl)
    

    CMAKE_SOURCE_DIR 已在 Cmake 中声明。

    【讨论】:

      【解决方案3】:

      假设您已经有一个 NDK,cd 到该文件夹​​,输入 /build/tools 并运行以下命令。将 API 替换为您需要的任何内容,并指定将放置独立工具链的文件夹。

      python make_standalone_toolchain.py --install-dir=&lt;standalone_toolchain_folder&gt; --arch=arm64 --api=22

      使用您之前使用的路径,运行以下命令。

      export ANDROID_NDK_HOME=&lt;standalone_toolchain_folder&gt;

      PATH=$ANDROID_NDK_HOME/bin:$PATH

      假设您已经下载并解压了 OpenSSL,请 cd 进入该文件夹并运行以下命令。

      ./Configure android-arm64 no-asm

      make clean

      make

      您会发现libcryptolibssl.a.so 文件已放置在OpenSSL 文件夹中。如果需要,请从 .so 文件中删除版本后缀。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-28
        • 2013-06-11
        • 2019-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-23
        相关资源
        最近更新 更多