1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #include <jni.h>
#define LOG_TAG "DemoInfo" #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
jint add(JNINev *nev , jobject jobj , jint a , jint b){ return a+b; }
static JNINativeMethod getMethods[] = { {"add", "(II)I", (void *)add}, }
static int registerNativesMethods(JNIEnv* env, const char* classname, JNINativeMethod* getmethods, int methodnum){ jclass clazz = env->FindClass(classname); if (clazz == NULL){ return JNI_FALSE; } if (env->RegisterNatives(clazz,getmethods,methodnum) < 0){ return JNI_FALSE; } return JNI_TRUE; }
static int registerNatives(JNIEnv *env){ const char* className = "com/example/helloworld/MainActivity"; return registerNativesMethods(env,className,getMethods,sizeof (getMethods) / sizeof(getMethods[0])); }
JNIEXPORT jint JNI_onLoad(javaVM *vm, void *reserved){ JNIEnv* env = NULL; if (vm->GetEnv((void**)(&env),JNI_VERSION_1_6) != JNI_OK){ return -1; } assert(env != NULL); if (!registerNatives(env)){ return -1; } return JNI_VERSION_1_6; }
|