首页 > 解决方案 > 在原生 android 项目中包含 openssl 库

问题描述

我想为 android 的 openssl 库创建包装器模块,它将使用大多数主要功能来保证安全。

我知道有人说他们已经这样做了,并在 github 上发布了他们的工作,但出于安全原因,我们不能使用它。

我将 openssl 库编译为 android 并添加到 CMakeList.txt 中,如下所述: https ://developer.android.com/studio/projects/configure-cmake#add-other-library

我所做的步骤: 1. 我从这里下载:https : //www.openssl.org/source/ 版本 1.1.1c
2. 成功编译:

export ANDROID_NDK_HOME=/home/user/Android/Sdk/ndk-bundle
PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH
./Configure android-arm64
make
  1. 我把整个文件夹放在这里:
    Project/app/src/main/cpp/openssl-1.1.1c

4.这就是 CMakeList 的样子:

cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE ON)
add_library(openssl-wrapper-lib
        SHARED
        openssl-c-wrapper.cpp)

#===v===v===v===v===v===v===openssl-include===v===v===v===v===v===v===
add_library(
        crypto
        SHARED
        IMPORTED
)

set_target_properties(
        crypto
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_CURRENT_SOURCE_DIR}/openssl-1.1.1c/libcrypto.so
)

include_directories(openssl-1.1.1c/include)
#===^===^===^===^===^===^===openssl-include===^===^===^===^===^===^===

find_library(log-lib
        log)

target_link_libraries( # Specifies the target library.
        openssl-wrapper-lib
        crypto
        ${log-lib})

我构建时的错误是这样的:

Error while executing process /home/user/Android/Sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /home/user/AndroidStudioProjects/EncryptionModule/app/.externalNativeBuild/cmake/debug/x86_64 --target openssl-wrapper-lib}
[1/1] Linking CXX shared library /home/user/AndroidStudioProjects/EncryptionModule/app/build/intermediates/cmake/debug/obj/x86_64/libopenssl-wrapper-lib.so
FAILED: : && /home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++  --target=x86_64-none-linux-android21 --gcc-toolchain=/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security  -v -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -shared -Wl,-soname,libopenssl-wrapper-lib.so -o /home/user/AndroidStudioProjects/EncryptionModule/app/build/intermediates/cmake/debug/obj/x86_64/libopenssl-wrapper-lib.so CMakeFiles/openssl-wrapper-lib.dir/openssl-c-wrapper.cpp.o  /home/user/AndroidStudioProjects/EncryptionModule/app/src/main/cpp/openssl-1.1.1c/libcrypto.so /home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/x86_64-linux-android/21/liblog.so -latomic -lm && :
Android (5220042 based on r346389c) clang version 8.0.7 (https://android.googlesource.com/toolchain/clang b55f2d4ebfd35bf643d27dbca1bb228957008617) (https://android.googlesource.com/toolchain/llvm 3c393fe7a7e13b0fba4ac75a01aa683d7a5b11cd) (based on LLVM 8.0.7svn)
Target: x86_64-none-linux-android21
Thread model: posix
InstalledDir: /home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin
Found candidate GCC installation: /home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/i686-linux-android/4.9.x
Found candidate GCC installation: /home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x
Selected GCC installation: /home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x
Candidate multilib: .;@m64
Selected multilib: .;@m64
 "/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld" --sysroot=/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot -z now -z relro --hash-style=gnu --hash-style=both --enable-new-dtags --eh-frame-hdr -m elf_x86_64 -shared -o /home/user/AndroidStudioProjects/EncryptionModule/app/build/intermediates/cmake/debug/obj/x86_64/libopenssl-wrapper-lib.so /home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/x86_64-linux-android/21/crtbegin_so.o -L/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/8.0.7/lib/linux/x86_64 -L/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x -L/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/lib/../lib64 -L/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/x86_64-linux-android/21 -L/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/x86_64-linux-android -L/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/lib -L/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib --exclude-libs libgcc.a --exclude-libs libatomic.a --build-id --warn-shared-textrel --fatal-warnings --no-undefined -z noexecstack -soname libopenssl-wrapper-lib.so CMakeFiles/openssl-wrapper-lib.dir/openssl-c-wrapper.cpp.o /home/user/AndroidStudioProjects/EncryptionModule/app/src/main/cpp/openssl-1.1.1c/libcrypto.so /home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/x86_64-linux-android/21/liblog.so -latomic -lm -Bstatic -lc++ -Bdynamic -lm -lgcc -ldl -lc -lgcc -ldl /home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/x86_64-linux-android/21/crtend_so.o
/home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: /home/user/AndroidStudioProjects/EncryptionModule/app/src/main/cpp/openssl-1.1.1c/libcrypto.so: incompatible target
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

请帮我理解我做错了什么

标签: android-ndkopenssl

解决方案


您为 arm64 构建了库,但您也在为 Android 的其他 ABI 构建库。您需要为应用程序支持的每种架构构建这些库。即,重复您的初始步骤:

./Configure android-arm
make
./Configure android-arm64
make
./Configure android-x86
make
./Configure android-x86_64
make

(请注意,您可能需要在单独的目录中运行每个构建,除非您使用的构建脚本将为您独立安装每个拱门的库。)

然后,您需要执行以下操作(根据您的实际安装路径根据需要进行调整)将库导入 CMake:

set_target_properties(
        crypto
        PROPERTIES IMPORTED_LOCATION
        # The change happens here: each of the architectures gets its own subdirectory.
        ${CMAKE_CURRENT_SOURCE_DIR}/openssl-1.1.1c/${ANDROID_ABI}/libcrypto.so
)

另一种选择,如果您只关心支持 64 位 ARM,将禁用 build.gradle 中的其他架构,如https://google.github.io/android-gradle-dsl/current/com所述.android.build.gradle.internal.dsl.NdkOptions.html

android {
    // Similar to other properties in the defaultConfig block, you can override
    // these properties for each product flavor in your build configuration.
    defaultConfig {
        ndk {
            // Tells Gradle to build outputs for the following ABIs and package
            // them into your APK.
            abiFilters 'arm64-v8a'
        }
    }
}

推荐阅读