gpt4 book ai didi

android-ndk - 在 native android 项目中包含 openssl 库

转载 作者:行者123 更新时间:2023-12-02 00:16:13 26 4
gpt4 key购买 nike

我想为 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.

请帮助我理解我做错了什么

最佳答案

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

关于android-ndk - 在 native android 项目中包含 openssl 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56876159/

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