Android studio 添加openssl库使用AES加密方法

这是项目中的一部分,写下来自己有空翻一翻方便记忆,也供有需要的人查找借鉴。因为我不是搞安卓的,有不对的地方自己看着办。

1、首先肯定是创建一个Android工程,支持C++打钩就行,其它没什么说的

2、编译openssl,最后结果是下图这样的,里面有两个.so,可以自己编译也可以直接下载拿来用,后面有链接,本次记录主要记录如何添加,编译可自行查找其它博客



3、将这4个文件夹放入项目libs目录下。如图

将编译出来的include复制到src/main下,也可自行放在其它位置

4、基本动作完成了,打开src目录下的bulid.gradle,写入

apply plugin: 'com.android.application'  android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "xiaohym.xiaohym.com.ddwater"  minSdkVersion 23
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"  testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"  ndk{
            moduleName "aesencode-lib"  }
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11 -frtti -fexceptions"  abiFilters  'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'  }
        }
    }
    buildTypes {
        release {
            minifyEnabled false  proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'  }
    }
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"  }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'  implementation 'com.android.support.constraint:constraint-layout:1.1.1'  testImplementation 'junit:junit:4.12'  androidTestImplementation 'com.android.support.test:runner:1.0.2'  androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }
5、打开src目录下CMakeLists.txt,写入

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)
#C++编译
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
include_directories(D:/xiao/android/ddwater/app/src/main/include)
#动态方式加载
add_library(openssl SHARED IMPORTED )
add_library(ssl SHARED IMPORTED )
#引入第三方.so库
set_target_properties(openssl PROPERTIES IMPORTED_LOCATION D:/xiao/android/ddwater/app/libs/${ANDROID_ABI}/libcrypto.so)
set_target_properties(ssl PROPERTIES IMPORTED_LOCATION D:/xiao/android/ddwater/app/libs/${ANDROID_ABI}/libssl.so)
add_library( # Sets the name of the library.
             aesencode-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/jni/xiaohym_xiaohym_com_ddwater_AesEncodes.cpp )


# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.


# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

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 )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       aesencode-lib
                       openssl
                       ssl
                       android

                       # Links the target library to the log library
                       # included in the NDK.

${log-lib} )

 
xiaohym_xiaohym_com_ddwater_AesEncodes.cpp是我自己的C文件,Android通过jni调用这里面的函数,
附上参考链接https://www.jianshu.com/p/07df7626b4ee