You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过CMake为Android C/C++ JNI项目添加OpenSSL 3外部库

问题:Android Studio手动引入OpenSSL 3库失败

我正在Android Studio中开发Android C库项目,需要手动添加C语言外部库(不通过Android Studio Gradle依赖)。已通过Homebrew安装OpenSSL 3,并在.zprofile中配置了OPENSSL_ROOT_DIR,但构建项目时出现如下错误:

C++ build system [configure] failed while executing:
    /opt/homebrew/bin/cmake \
      -H/Users/mba/AndroidStudioProjects/MyApplication/app/src/main/cpp \
      -DCMAKE_SYSTEM_NAME=Android \
      -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
      -DCMAKE_SYSTEM_VERSION=24 \
      -DANDROID_PLATFORM=android-24 \
      -DANDROID_ABI=arm64-v8a \
      -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \
      -DANDROID_NDK=/Users/mba/Library/Android/sdk/ndk/25.1.8937393 \
      -DCMAKE_ANDROID_NDK=/Users/mba/Library/Android/sdk/ndk/25.1.8937393 \
      -DCMAKE_TOOLCHAIN_FILE=/Users/mba/Library/Android/sdk/ndk/25.1.8937393/build/cmake/android.toolchain.cmake \
      -DCMAKE_MAKE_PROGRAM=/Users/mba/Library/Android/sdk/cmake/3.22.1/bin/ninja \
      -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/Users/mba/AndroidStudioProjects/MyApplication/app/build/intermediates/cxx/Debug/444d406q/obj/arm64-v8a \
      -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=/Users/mba/AndroidStudioProjects/MyApplication/app/build/intermediates/cxx/Debug/444d406q/obj/arm64-v8a \
      -DCMAKE_BUILD_TYPE=Debug \
      -DCMAKE_FIND_ROOT_PATH=/Users/mba/AndroidStudioProjects/MyApplication/app/.cxx/Debug/444d406q/prefab/arm64-v8a/prefab \
      -B/Users/mba/AndroidStudioProjects/MyApplication/app/.cxx/Debug/444d406q/arm64-v8a \
      -GNinja \
      -DANDROID_STL=c++_shared
  from /Users/mba/AndroidStudioProjects/MyApplication/app
CMake Error at /opt/homebrew/Cellar/cmake/3.29.2/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the
  system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY
  OPENSSL_INCLUDE_DIR)

我的CMakeLists.txt内容如下:

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html.
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.

# Sets the minimum CMake version required for this project.
cmake_minimum_required(VERSION 3.22.1)

project("nfcNative")

find_package(OpenSSL REQUIRED)
message(STATUS "OPENSSL_LIBRARIES:${OPENSSL_LIBRARIES}")

add_library(${CMAKE_PROJECT_NAME} SHARED
        nfcNative.c
        commands.c
        rsa.c
        )

include_directories(src/main/cpp/include/ ${OPENSSL_INCLUDE_DIRS})
target_link_libraries(${CMAKE_PROJECT_NAME}
        android
        log
        OpenSSL::Crypto
        OpenSSL::Ssl
        )

我已在Android Studio的环境变量设置中配置了OPENSSL_ROOT_DIR:
Android Studio环境变量设置界面

补充说明:通过Android Studio依赖添加的com.android.ndk.thirdparty:openssl:1.1.1q-beta-1包,使用find_package(openssl REQUIRED CONFIG)可以正常找到,但该包仅为1.1.1版本,我的项目需要更新的3.x版本。

内容的提问来源于stack exchange,提问作者Lilvinco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 19:47:06