1、下载NDK开发工具
打开SDK Manager并下载如下工具
2、在Android studio中配置环境变量
3、在java中声明native方法
4、在main中和java同级创建jni文件夹,并将生成的头文件拷贝到jni文件夹中
5、创建c文件并实现声明的native方法
6、在app的build.gradle中,android下的defaultConfig模块和android模块下分别添加如下的代码
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.dongnaoedu.nkdfilecrypt"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//使用Cmake工具
externalNativeBuild {
cmake {
cppFlags ""
abiFilters 'arm64-v8a','armeabi-v7a','x86'
}
}
}
sourceSets.main {
jni.srcDirs = []
jniLibs.srcDir 'src/main/libs'
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
7、在src目录下创建名为CMakeLists.txt的文件夹,并修改如下所示的三处
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
# 设置so文件名称.
ndk_file_crypt
# Sets the library as a shared library.
SHARED
# 设置这个so文件为共享.
# Provides a relative path to your source file(s).
# 设置这个so文件为共享.
src/main/jni/ndk_file_crypt.c)
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( # Specifies the target library.
# 制定目标库.
ndk_file_crypt
# Links the target library to the log library
# included in the NDK.
${log-lib} )
8、点击Build->Make Project ,运行完成后会在app/build/intermediates中自动生成文件夹cmake,里面包含生成的.so文件
8、在代码中直接调用方法