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