Android JNI Dynamic method

java file:

View Code
package com.dynajni;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class DynaJNI extends Activity {
    int mCount = 0;
    
    static {
        System.loadLibrary("hello-jni");
    }
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        native_dynamicFunc();
        Log.d("JniTest","mCount:" + String.valueOf(mCount));
        //Log.d("JniTest", native_stringFromJNI());
        
        Log.d("JniTest", native_stringFromJNI());
    }
    
    public native String  native_stringFromJNI();
    public native void  native_dynamicFunc();
}

jni file:

View Code
#include <string.h>
#include <jni.h>
#include <android/log.h>
#include <assert.h>

#define LOG_TAG "LOG_JNI"
#define LOGD(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)


/////////动态方法/////////////////////////////////////////////////////////
static const char* const kClassPathName = "com/dynajni/DynaJNI";

struct fields_t {
    jfieldID    context;
};

static struct fields_t fields;

//返回字符串
jstring com_jnitest_stringFromJNI(JNIEnv* env, jobject thiz) {
    return env->NewStringUTF("Hello from c++, JNI !");
}

void com_jnitest_dynamicFromJNI(JNIEnv* env, jobject thiz) {
    int count = 9;
    jclass clazz;

    clazz = env->FindClass(kClassPathName);
    if (clazz == NULL) {
        LOGE("Can't find com/dynajni/DynaJNI");
        return;
    }

    fields.context = env->GetFieldID(clazz, "mCount", "I");
    if (fields.context == NULL) {
        LOGE("Can't find DynaJNI.mCount");
        return;
    }

    env->SetIntField(thiz, fields.context, (int)count);
}
// ------注册-----------------------------------------------------------------
static JNINativeMethod gMethods[] = {
    {"native_dynamicFunc",     "()V",                      (void *)com_jnitest_dynamicFromJNI},
    {"native_stringFromJNI",   "()Ljava/lang/String;",      (void *)com_jnitest_stringFromJNI},
};

static int registerNativeMethods(JNIEnv* env, const char* className,
                                JNINativeMethod* gMethods, int numMethods)
{
    jclass clazz;
    clazz = env->FindClass(className);
    if (clazz == NULL) {
        //fprintf(stderr, "Native registration unable to find class '%s'", className);
        return JNI_FALSE;
    }
    if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
        //fprintf(stderr, "RegisterNatives failed for '%s'", className);
        return JNI_FALSE;
    }

    return JNI_TRUE;
}

int register_com_jnitest_function(JNIEnv *env)
{
    return registerNativeMethods(env, kClassPathName, gMethods, sizeof(gMethods)/sizeof(gMethods[0]));
}

jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
    JNIEnv* env = NULL;
    jint result = -1;

    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
        LOGE("ERROR: GetEnv failed\n");
        goto bail;
    }
    assert(env != NULL);

    if (register_com_jnitest_function(env) < 0) {
        LOGE("ERROR: jnitest native registration failed\n");
        goto bail;
    }

    result = JNI_VERSION_1_4;

bail:
    return result;
}

make file:

View Code
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := hello-jni
LOCAL_SRC_FILES := hello-jni.cpp

LOCAL_LDLIBS += -llog

include $(BUILD_SHARED_LIBRARY)

 

ANDROID JNINativeMethodhttp://blog.csdn.net/sepnic/article/details/6657900

JNI C:http://my.unix-center.net/~Simon_fu/?p=833

jni C++:http://my.unix-center.net/~Simon_fu/?p=836

C/C++调用Java类或方法http://www.cppblog.com/kongque/archive/2010/11/24/134522.aspx

Java与C\C++之间传递中文字符串http://www.cnblogs.com/rookieport/archive/2005/10/18/257154.html 

C++和JNI的数据转换http://www.cnblogs.com/daniel-shen/archive/2006/10/16/530587.html

 

posted @ 2012-04-13 17:20  cornellbox  阅读(296)  评论(0编辑  收藏  举报