从Native C / C ++代码调用JAVA类成员

时间:2011-09-01 09:29:12

标签: android java-native-interface android-ndk

我正在编写一个OpenGL C / C ++应用程序,我通过Android NDK,JNI支持移植到Android。我在执行从本机发出的JAVA回调代码时遇到了困难。

以下是代码:

class OpenGLSurfaceView extends GLSurfaceView 
{
…
     public OpenGLSurfaceView(Context context, int deviceWidth, int deviceHeight) 
    {
        super(context);
        nativeObj = new NativeLib();
        mRenderer = new OpenGLRenderer(context, nativeObj, deviceWidth, deviceHeight);
        setRenderer(mRenderer);
        setRenderMode(RENDERMODE_WHEN_DIRTY);
    }
…
    private void CallBack()
    {
        // force redraw
        requestRender();
    }
}


class OpenGLRenderer implements GLSurfaceView.Renderer 
{
    …
public void onSurfaceCreated(GL10 gl, EGLConfig config) 
    {
        nativeObj.init(…);
        nativeObj.cachejavaobject(JNIEnv *env, jobject obj); // for caching obj on native side
    }

    public void onSurfaceChanged(GL10 gl, int w, int h) 
    {
    }

    public void onDrawFrame(GL10 gl) 
    {
        nativeObj.draw(…);
    }
}

在本机代码中,我有一个函数texturesLoaded(),当一些纹理完全加载到另一个线程上时我会发出信号,我需要在JAVA端强制刷新nativeLib.draw(...)。我是这样做的:

我在JNI_OnLoad和gJObjectCached上缓存JavaVM,jClass,jMethodID

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved)
{
gJVM = jvm;  // cache the JavaVM pointer

LogNativeToAndroidExt("JNILOAD!!!!!!!!!!");
JNIEnv *env;
int status = gJVM->GetEnv((void **)&env, JNI_VERSION_1_6);
if(status < 0)
{
    LogNativeToAndroidExt("Failed to get JNI environment, assuming native thread");
    status = gJVM->AttachCurrentThread(&env, NULL);
    if(status < 0)
    {
        LogNativeToAndroidExt("callback_handler: failed to attach current thread");
        return JNI_ERR;
    }
}

gJClass = env->FindClass("com/android/newlineactivity/NewLineGLSurfaceView");
if(gJClass == NULL)
{
    LogNativeToAndroidExt("Can't find Java class.");
    return JNI_ERR;
}

gJMethodID = env->GetMethodID(gJClass, "callback", "()V");
if(gJMethodID == NULL)
{
    LogNativeToAndroidExt("Can't find Java method void callback()");
    return JNI_ERR;
}

return JNI_VERSION_1_6;

}

JNIEXPORT void Java_com_android_OpenGL_NativeLib_cachejavaobject(JNIEnv* env, jobject obj)
{
    // cache the java object
    gJObjectCached = obj;
...
}

然后在texturesLoaded()上执行:

void texturesLoaded()
{
    // Cannot share a JNIEnv between threads. You should share the JavaVM, and use JavaVM->GetEnv to discover the thread's JNIEnv
    JNIEnv *env = NULL;
    int status = gJVM->GetEnv((void **)&env, JNI_VERSION_1_6);
    if(status < 0)
    {
        LogNativeToAndroidExt("callback_handler: failed to get JNI environment, assuming native thread");
        status = gJVM->AttachCurrentThread(&env, NULL);
        if(status < 0)
        {
            LogNativeToAndroidExt("callback_handler: failed to attach current thread");
            return;
        }
    }

    LogNativeToAndroidExt("Calling JAVA method from NATIVE C/C++");
    env->CallVoidMethod(gJObjectCached, gJMethodID);
    LogNativeToAndroidExt("DONE!!!");
}

结果......从本机方面我得到了类,我得到了方法,方法被调用,但是当它到达/调用requestRender()里面(或者试图访问GLSurfaceView的任何其他成员方法时它崩溃了!)

我不能尝试使用CallStaticVoidMethod(gJClass,gjMethodID);因为那时我无权访问requestRender();

任何想法或意见,也许我在这里做错了。

由于

1 个答案:

答案 0 :(得分:3)

您需要为您隐藏的类/对象创建全局引用。您正在保存的引用是本地引用,它们不能跨线程共享,并在运行时清理JNI本地引用堆栈时消失。

查看Sun/Oracle documentation for global and local references,查看JNI方法JNIEnv::NewGlobalRefJNIEnv::DeleteGlobalRef

gJClass = env->NewGlobalRef(env->FindClass( ... ));

gJObjectCached = env->NewGlobalRef(obj);

(编辑:原来你不需要方法ID的全局引用。)