编译生成.so文件
程序员文章站
2022-07-02 23:35:11
1.新建带c++支持的Android工程。demo编译运行无误2.在CMakeLists.txt中添加,生成.so文件可以拷贝出去供其他工程使用# 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 C...
1.新建带c++支持的Android工程。demo编译运行无误
2.在CMakeLists.txt中添加,生成.so文件可以拷贝出去供其他工程使用
# 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)
# 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.
#设置生成的so动态库最后输出的路径
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})
#在add_library之前设置
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp )
3.动态注册native方法名称,这样可以注册其他工程的包路径
// native方法
static jstring JNICALL native_get_String(JNIEnv *env, jobject obj, jobject content) {
}
/**
* 注册的native调用的方法
*/
static const JNINativeMethod gMethods[] = {
{"stringFromJNI", "(Landroid/content/Context;)Ljava/lang/String;", (jstring *) native_get_String}
};
/**
* 注册方法到JVM中
* System.loadLibrary会自动调用JNI_OnLoad,在此进行动态注册
*
* @param jvm
* @param reserved
* @return
*/
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
// 防止动态调试
ptrace(PTRACE_TRACEME, 0, 0, 0);
JNIEnv *env = NULL;//注册时在JNIEnv中实现的,所以必须首先获取它
jint result = JNI_FALSE;
//获取env指针
if (jvm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {//从JavaVM获取JNIEnv,使用1.6的版本
return result;
}
//获取类引用,写类的全路径(包名+类名)
jclass clazz = env->FindClass("com.mobile.encrypt.api.whitebox.EncryptUtils");
if (clazz == NULL) {
return result;
}
//注册方法
if (env->RegisterNatives(clazz, gMethods, sizeof(gMethods) / sizeof(gMethods[0])) < 0) {
return result;
}
//成功
result = JNI_VERSION_1_6;
return result;
}
4.获取方法签名,在build的 /build/intermediates/classes/debug/目录下获取定义native方法名称工具类的class文件。使用javap -s xxx.class获取方法签名
5.CMakeLists.txt添加多c++源文件
6.gradle配置
本文地址:https://blog.csdn.net/MatrixMind/article/details/107990181
上一篇: jQuery实现导航栏
下一篇: 柔性数组成员