ndk注册native方法关联

来源:互联网 发布:linux循环 编辑:程序博客网 时间:2024/05/21 18:44

java 代码

package com.jni;public class JavaHello {public static native String hello();static {// load library: libtest.sotry {System.loadLibrary("test");} catch (UnsatisfiedLinkError ule) {System.err.println("WARNING: Could not load library!");}}public static void main(String[] args) {String s = new JavaHello().hello();System.out.println(s);}}

c代码

#include <stdlib.h>#include <string.h>#include <stdio.h>#include <jni.h>#include <assert.h>JNIEXPORT jstring JNICALL native_hello(JNIEnv *env, jclass clazz){printf("hello in c native code./n");return (*env)->NewStringUTF(env, "hello world returned.");}#define JNIREG_CLASS "com/jni/JavaHello"//指定要注册的类/*** Table of methods associated with a single class.*/static JNINativeMethod gMethods[] = {{ "hello", "()Ljava/lang/String;", (void*)native_hello },//绑定};/** Register several native methods for one class.*/static int registerNativeMethods(JNIEnv* env, const char* className,        JNINativeMethod* gMethods, int numMethods){jclass clazz;clazz = (*env)->FindClass(env, className);if (clazz == NULL) {return JNI_FALSE;}if ((*env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0) {return JNI_FALSE;}return JNI_TRUE;}/** Register native methods for all classes we know about.*/static int registerNatives(JNIEnv* env){if (!registerNativeMethods(env, JNIREG_CLASS, gMethods,                                  sizeof(gMethods) / sizeof(gMethods[0])))return JNI_FALSE;return JNI_TRUE;}/** Set some test stuff up.** Returns the JNI version on success, -1 on failure.*/JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved){JNIEnv* env = NULL;jint result = -1;if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {return -1;}assert(env != NULL);if (!registerNatives(env)) {//注册return -1;}/* success -- return valid version number */result = JNI_VERSION_1_4;return result;}


0 0