Android下openssl编译和使用(二)

博客承接上篇openssl编译内容,介绍其使用方法。包括新建测试工程并勾选C++支持,复制openssl相关目录和库文件,修改CMakeList.txt文件,修改native - lib.cpp返回版本信息,启动app查看结果,还给出代码地址和参考链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上篇我们介绍如何编译了openssl:https://blog.csdn.net/zoujin6649/article/details/89891720

接下来我们看看如何使用

1、新建一个测试工程,并勾选include C++ support

2、把openssl-1.1.1b/include下的openssl目录,整个复制到项目src下的cpp子目录

3、把lib下的libcryto.alibssl.a复制到src/cpp/libs子目录

4、修改CMakeList.txt文件,在cmake_minimum_required(VERSION 3.4.1)后面添加:

#表示把src/main/cpp加入到include目录,这样在代码中使用:#include <...>就能访问到头文件
include_directories(src/main/cpp)

#添加两个预编译库
add_library(# Sets the name of the library.
        openssl-crypto
        # Sets the library as a static library.
        STATIC
        IMPORTED)

set_target_properties(
        # Specifies the target library.
        openssl-crypto
        # Specifies the parameter you want to define.
        PROPERTIES IMPORTED_LOCATION
        # Provides the path to the library you want to import.
        ${CMAKE_SOURCE_DIR}/src/main/cpp/libs/${ANDROID_ABI}/libcrypto.a)

add_library(# Sets the name of the library.
        openssl-ssl
        # Sets the library as a static library.
        STATIC
        IMPORTED)

set_target_properties(
        # Specifies the target library.
        openssl-ssl
        # Specifies the parameter you want to define.
        PROPERTIES IMPORTED_LOCATION
        # Provides the path to the library you want to import.
        ${CMAKE_SOURCE_DIR}/src/main/cpp/libs/${ANDROID_ABI}/libssl.a)

5、修改CMakeList.txt文件最后的target_link_libraries如下:

target_link_libraries( # Specifies the target library.
        native-lib

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib} openssl-ssl openssl-crypto)

6、修改native-lib.cpp返回openssl版本信息

7、启动app可以看到如下显示结果:

代码地址:https://github.com/Claire6649/openssl_test

参考:https://www.jianshu.com/p/b790c548e787

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值