之前沒有使用過 JNI 或 NDK 開發(fā),最近了解到把項目中的秘鑰放到 .so 文件中能起到較好的安全性作用傲绣,因此花了個早上入坑
首先配置 CMake 環(huán)境
在 module 工程文件的 build.gradle
文件中做以下配置
defaultConfig
的配置
externalNativeBuild {
cmake {
cppFlags ""
//生成多個版本的so文件
abiFilters 'arm64-v8a','armeabi-v7a','x86','x86_64'
}
}
android
的配置
externalNativeBuild {
cmake {
path "CMakeLists.txt" // C 文件編輯的信息
}
}
關(guān)于 CMakeLists.txt
文件掠哥,跟 build.gradle
同層級
# 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.
#CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
# 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.
add_library( # Sets the name of the library.
# 設(shè)置so文件名稱.
nativejni
# Sets the library as a shared library.
SHARED
# 設(shè)置這個so文件為共享.
# Provides a relative path to your source file(s).
# 設(shè)置這個so文件為共享.指向創(chuàng)建的 c 文件
src/main/jni/nativejni.c)
# 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.
# 制定目標(biāo)庫.
nativejni
# Links the target library to the log library
# included in the NDK.
${log-lib} )
把上面的代碼復(fù)制到 CMakeLists.txt
文件中即可,上面的 nativejni.c
文件后面提到,先執(zhí)行上訴步驟會報錯秃诵,CMakeLists.txt
文件中的代碼執(zhí)行錯誤龙致,暫時別管,因為需要結(jié)合下面步驟一起顷链,等 .so 文件創(chuàng)建完就沒事了
編寫 .so 文件
- 首先創(chuàng)建一個
NativeUtils
文件
public class NativeUtils {
static {
//這是加載 .so 文件的方法,這里的 nativejni 就是 so 文件名
System.loadLibrary("nativejni");
}
public static native String getStringFromNative();
}
-
然后 ReBuild 下項目
生成頭文件
在Terminal
中調(diào)用javah -d jni -classpath 上一步生成的 build 的 NativeUtils 路徑
javah -d jni -classpath /Users/mac/Documents/workspace/JNIDemo/app/build/intermediates/classes/debug com.feng.jnidemo.NativeUtils
jni 包中就會生成 com_feng_jnidemo_NativeUtils.h
頭文件
- 在 jni 包中創(chuàng)建
nativejni.c
c 文件
//引用頭文件
#include "com_feng_jnidemo_NativeUtils.h"
//方法名:Java+頭文件名+方法名
JNIEXPORT jstring JNICALL Java_com_feng_jnidemo_NativeUtils_getStringFromNative
(JNIEnv *env, jobject obj) {
return (*env)->NewStringUTF(env, "this is secret key");
}
最后 Rebuild 一下發(fā)現(xiàn)生成 .so 文件
調(diào)用方法
String str=NativeUtils.getStringFromNative();
Log.e(TAG, "onCreate: str="+str );