在Android开发中咱们常常要把一些比较看重安全或者计算效率的东西经过JNI调用C/C++代码来实现,若是须要实现的功能简单或者你的C/C++代码能力比较强,可是目前仍是有不少功能强大的第三方库的,好比openssl、FFmpeg等,调用这些第三方实现显然比重复造轮子实际的多。html
本教程适合将原始的动态库(.so),即没有包含JNI方法于是java没法直接调用的库连接到本身的C/C++代码中,而后提供调用。java
# 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 )
复制代码
## libpng动态库的设置
add_library( # Sets the name of the library.
png
# Sets the library as ashared library.
STATIC
# Provides a relative pathto your source file(s).
IMPORTED)
set_target_properties(
png
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libpng.a)
复制代码
## 引入libssl动态库
add_library(ssl SHARED IMPORTED)
set_target_properties(
ssl
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libssl.so)
复制代码
添加连接外部静态库和动态库的流程差很少,用STATIC
和SHARED
来区分android
${CMAKE_SOURCE_DIR}
是CMake 中预约义的常量,指当前工程的 CMake 文件所在路径,其余比较有用的常量:git
CMAKE_CURRENT_SOURCE_DIR
: 指当前 CMake 文件所在的文件夹路径github
CMAKE_CURRENT_LIST_FILE
: 指当前 CMake 文件的完整路径算法
PROJECT_SOURCE_DIR
:指当前工程的路径安全
最后将全部库连接起来就好了:bash
target_link_libraries( # Specifies the target library.
native-lib
png
openssl
ssl
android
# Links the target library to the log library
# included in the NDK.
${log-lib} )
复制代码
openssl是一款强大的加解密库,提供了RSA、AES、MD5等经常使用的加密算法,网上也有不少编译openssl动态库和静态库的方法。 项目的文件结构目录以下:app
# 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)
#配置加载头文件
include_directories(./src/main/cpp/include)
file(GLOB mian_src "src/main/cpp/*.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.
add_library( # Sets the name of the library.
cipher
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
${mian_src} )
#动态方式加载
add_library(openssl SHARED IMPORTED )
add_library(ssl SHARED IMPORTED )
#引入第三方.so库
set_target_properties(openssl PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libcrypto.so)
set_target_properties(ssl PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libssl.so)
# 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.
cipher
openssl
ssl
android
# Links the target library to the log library
# included in the NDK.
${log-lib} )
复制代码
extern "C"
JNIEXPORT jstring JNICALL Java_org_hik_arraytest_MainActivity_md5(JNIEnv *env, jobject instance, jbyteArray src_) {
Log_d("MD5->信息摘要算法第五版");
jbyte *src = env->GetByteArrayElements(src_, NULL);
jsize src_Len = env->GetArrayLength(src_);
char buff[3] = {'\0'};
char hex[33] = {'\0'};
unsigned char digest[MD5_DIGEST_LENGTH];
MD5_CTX ctx;
MD5_Init(&ctx);
Log_d("MD5->进行MD5信息摘要运算");
MD5_Update(&ctx, src, src_Len);
MD5_Final(digest, &ctx);
strcpy(hex, "");
Log_d("MD5->把哈希值按%%02x格式定向到缓冲区");
for (int i = 0; i != sizeof(digest); i++) {
sprintf(buff, "%02x", digest[i]);
strcat(hex, buff);
}
Log_d("MD5->%s", hex);
Log_d("MD5->从jni释放数据指针");
env->ReleaseByteArrayElements(src_, src, 0);
return env->NewStringUTF(hex);
}
复制代码
public class MainActivity extends AppCompatActivity {
String TAG = "my_openssl";
static {
System.loadLibrary("cipher");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "MD5信息摘要->" + md5("1111".getBytes()).toUpperCase());
}
/**
* MD5编码
*/
public native String md5(byte[] src);
}
复制代码
结果以下:ide
java.lang.UnsatisfiedLinkError:
dlopen failed: library "/system/lib64/libcrypto.so" needed or dlopened by "/system/lib64/libnativeloader.so"
is not accessible for the namespace "classloader-namespace"
复制代码
java.lang.UnsatisfiedLinkError: dlopen failed: library "libcrypto.so" not found
复制代码
在app的build.gradle
中指定第三方so文件目录,否则生成apk文件的时候不会包含这些so文件。
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
复制代码