Android集成OpenSSL实现加解密-集成

  1. 导入so

    将编译生成的 OpenSSL 动态库文件(.so 文件)复制到你的 Android 项目的 libs 目录中

  2. 导入头文件

    将编译生成的include文件夹导入到项目中

  3. build.gradle添加配置

    复制代码
       defaultConfig {
            ......
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            externalNativeBuild {
                cmake {
                    cppFlags "-std=c++11 -frtti -fexceptions"
                    //目标平台,若需要更多平台的请自行配置
                    abiFilters 'arm64-v8a'
                }
            }
        }
    
       ......
        sourceSets {
            main {
                jniLibs.srcDirs = ['src/main/jniLibs']
            }
        }
        externalNativeBuild {
            cmake {
                path file('src/main/cpp/CMakeLists.txt')
                version '3.22.1'
            }
        }
  4. 修改CMakeLists.txt

    复制代码
    cmake_minimum_required(VERSION 3.22.1)
    
    project("jnitest")
    
    set(JNI_LIBS_DIR ${CMAKE_SOURCES_DIR}/src/main/jniLibs)
    
    add_library( # Sets the name of the library.
            jnitest
    
            # Sets the library as a shared library.
            SHARED
    
            # Provides a relative path to your source file(s).
            native-lib.cpp)
    
    # 1、添加导入库,类似给每个库起个名字
    add_library(
            openssl # 模块名称,随意
            SHARED # 表示共享库
            IMPORTED # 表示当前是导入库
    )
    add_library(
            ssl # 模块名称,随意
            SHARED # 表示共享库
            IMPORTED # 表示当前是导入库
    ) 
    # 2、将上一步声明的库名字,和具体的库(so文件)关联到一起
    se t_target_properties(
             openssl # 库的名称
             PROPERTIES IMPORTED_LOCATION # 表示当前库是导入的方式
             ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libcrypto.so # so 动态库的具体路径 
    )
    set _target_properties(
            ssl # 库的名称
             PROPERTIES IMPORTED_LOCATION # 表示当前库是导入的方式
            ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libssl.so # so 动态库的具体路径 
    ) 
    
    # 3 、如果第三方库有头文件的,为了在编码的时候可以有提示,使用下面的指令进行指定
    # 指定头文件目录,指定后编译器会扫描这个目录,编码时可以进行提示
    include_directories( ${CMAKE_SOURCE_DIR}/include)
    
    find_library( # Sets the name of the path variable.
            log-lib
    
            # Specifies the name of the NDK library that
            # you want CMake to locate.
            log)
    
    target_link_libraries(jnitest openssl ssl  ${log-lib})
相关推荐
天空之城--7 分钟前
Android Launcher 性能优化完全指南:从启动到滑动,构建极致流畅的桌面体验
android·性能优化
hunterandroid39 分钟前
Android ANR 排查实战:从线上告警到主线程卡点定位
android·前端
TimeFine1 小时前
智能眼镜开发:图片翻译与EXIF的重要性
android
律宏阔1 小时前
Android 车载蓝牙开发笔记:HFP、A2DP、AVRCP、PBAP、MAP、BLE 与系统 API
android·蓝牙
@Adzc1 小时前
Android 16 省电模式 Tile 点击导致状态栏收起
android
杉氧1 小时前
打破边界(二):在 React Native 中嵌入原生 UI 组件 (Native UI Components)
android·前端·react native
古法安卓2 小时前
Android-切换白天黑夜内存不足问题排查
android·java·android studio
聚美智数2 小时前
快递拦截-物流在途拦截-在途拦截API接口介绍
android
flower_drop2 小时前
从 QtScrcpy 到 TabQA:如何在浏览器侧边栏搞定 Android 投屏与测试提单?
android
脚踏实地,坚持不懈!3 小时前
Android 系统工程师(性能/功耗/稳定性)岗位问题深度解析:从内核源码到实战排查(完善版)
android·linux·运维·服务器