[转]

使用RegisterNatives注册原生代码

 分类:

Android开发本地代码时,有两种方式,一种是使用javah生成头文件,然后编辑源代码,另一种不用生成头文件,直接编辑代码后,使用RegisterNatives方法进行注册,下面是一个Demo:

Java代码:

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.example.jnitest;  
  2.   
  3. public class TestJni {  
  4.     static {  
  5.         System.loadLibrary("Hello");  
  6.     }  
  7.     // static method  
  8.     public static native String test();  
  9.     // member method  
  10.     public native String test2();  
  11. }  

这里定义了两个native方法,test是静态方法,test2是成员方法。

 

使用C++实现两个方法:

 

[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. static jstring test(JNIEnv *env, jclass clz)  
  2. {  
  3.     LOGD("Hello1");  
  4.     return env->NewStringUTF("Hello1");  
  5. }  
  6.   
  7. static jstring test2(JNIEnv *env, jobject obj)  
  8. {  
  9.     LOGD("Hello2");  
  10.     return env->NewStringUTF("Hello2");  
  11. }  

实现了之后,需要注册两个方法

 

第一步:定义数据结构

 

[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. // register methods:  
  2. static JNINativeMethod methods[] = {  
  3.         {"test", "()Ljava/lang/String;", (void*) &test},  
  4.         {"test2", "()Ljava/lang/String;", (void*) &test2}  
  5. };  

第二步:在JNI_OnLoad方法中,对方法进行注册:

 

 

[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. int jniRegisterNativeMethods(JNIEnv* env,  
  2.                              const char* className,  
  3.                              const JNINativeMethod* gMethods,  
  4.                              int numMethods)  
  5. {  
  6.     jclass clazz;  
  7.     int tmp;  
  8.   
  9.     LOGD("Registering %s natives\n", className);  
  10.     clazz = env->FindClass(className);  
  11.     if (clazz == NULL) {  
  12.         LOGD("Native registration unable to find class '%s'\n", className);  
  13.         return -1;  
  14.     }  
  15.     if ((tmp=env->RegisterNatives(clazz, gMethods, numMethods)) < 0) {  
  16.         LOGD("RegisterNatives failed for '%s', %d\n", className, tmp);  
  17.         return -1;  
  18.     }  
  19.     return 0;  
  20. }  
  21.   
  22. int registerNativeMethods(JNIEnv *env) {  
  23.     return jniRegisterNativeMethods(env, "com/example/jnitest/TestJni", methods, sizeof(methods) / sizeof(methods[0]));  
  24. }  
  25.   
  26. JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {  
  27.   
  28.     JNIEnv* env = NULL;  
  29.     jint result = JNI_ERR;  
  30.   
  31.     if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {  
  32.         return result;  
  33.     }  
  34.   
  35.     if (registerNativeMethods(env) != JNI_OK) {  
  36.         return -1;  
  37.     }  
  38.   
  39.   
  40.     result = JNI_VERSION_1_4;  
  41.     LOGD("jni load start: %d", result);  
  42.   
  43.     return result;  
  44. }  

 

Android.mk:

 

[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. LOCAL_PATH := $(call my-dir)  
  2. include $(CLEAR_VARS)  
  3. LOCAL_CFLAGS := -D__STDC_CONSTANT_MACROS  
  4. LOCAL_LDLIBS += -lc -lm -llog   
  5. LOCAL_SHARED_LIBRARIES += liblog libcutils libnativehelper  
  6. LOCAL_MODULE := Hello  
  7. LOCAL_SRC_FILES := com_example_jnitest_TestJni.cpp  
  8. include $(BUILD_SHARED_LIBRARY)  

posted on 2017-04-06 11:47  Crysaty  阅读(121)  评论(0编辑  收藏  举报

导航