在原生 Android 项目上包含 openssl 库

问题描述 投票:0回答:2

我想为 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. 我把整个文件夹放在这里:
    项目/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-ndk openssl
2个回答
6
投票

您为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'
        }
    }
}

0
投票

我没有尝试过这个,只是提一下它的存在 - https://android-developers.googleblog.com/2020/02/native-dependency-in-android-studio-40.html

示例: 1.https://github.com/android/ndk-samples/tree/main/prefab/prefab-dependency 2.https://github.com/android/ndk-samples/tree/main/prefab/curl-ssl

© www.soinside.com 2019 - 2024. All rights reserved.