gpt4 book ai didi

android - 如何使用 NDK 19 在 OSX Darwin 上为 64 位 Android 交叉编译 OpenSSL

转载 作者:行者123 更新时间:2023-11-29 18:35:15 25 4
gpt4 key购买 nike

我之前问过基本相同的问题(不同的 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

我认为这是我上次惹麻烦的地方。我开始破解脚本以获得似乎有效的东西,现在我确定我所做的是错误的。

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

最佳答案

我能够使用 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 和 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());
}

我成功运行了该应用程序。

关于android - 如何使用 NDK 19 在 OSX Darwin 上为 64 位 Android 交叉编译 OpenSSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54656695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com