quetzalfir quetzalfir - 2 months ago 7
Android Question

android JNI report can't find method

I'm using the following code for wrapping all the native functions

package com.user.game;

import android.content.res.AssetManager;

public class MyGLRenderer {

static {
System.loadLibrary("Engine");
}

public static native void onSurfaceCreated();
public static native void passAssetManager(AssetManager assetManager);
public static native void getAppDirectory(String dir);
public static native void setExternalDirectory(String dir);
public static native void pointerLeave(int id, float x, float y);

}


and I use the following from C++

jclass javaClassRef;
JavaVM* javaVM = NULL;

jint JNI_OnLoad(JavaVM *vm, void *reserved) {
javaVM = vm;
return JNI_VERSION_1_2;
}

JNIEXPORT void JNICALL Java_com_user_game_MyGLRenderer_onSurfaceCreated(JNIEnv *env, jobject obj) {
Main_Manager::on_created();

static bool once = true;
if (once) {
jclass dataClass = env->FindClass("com/user/game/Game_Activity");
if (env->ExceptionCheck()) {
SFLOGI("JNI: error findclass");
/* code to handle exception */
env->ThrowNew(dataClass, "error message");
env->ExceptionClear();

return;
}

javaClassRef = (jclass) env->NewGlobalRef(dataClass);
if (env->ExceptionCheck()) {
SFLOGI("JNI: error get classRef");
jthrowable flag = env->ExceptionOccurred();
env->ExceptionClear();
env->Throw(flag);

return;
}

once = false;
}
}

bool call_Java_checkStoragePermission() {
JNIEnv *env;
javaVM->AttachCurrentThread(&env, NULL);

jmethodID javaMethodRef = env->GetMethodID(javaClassRef, "checkStoragePermission", "()Z");
if (env->ExceptionCheck()) {
SFLOGI("JNI: error get methodID");
jthrowable flag = env->ExceptionOccurred();
env->ExceptionClear();
env->Throw(flag);

return false;
}

//jobject javaObjectRef = env->NewObject(javaClassRef, javaMethodRef);
jboolean ans = env->CallStaticBooleanMethod(javaClassRef, javaMethodRef);
//jboolean ans = env->CallBooleanMethod(javaObjectRef, javaMethodRef);

javaVM->DetachCurrentThread();

return ans;
}


and I call the wrapper class from my main activity which is

package com.user.game;

public class Game_Activity extends Activity {
private static String TAG = "Debugging";

@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);

MyGLRenderer.getAppDirectory(getExternalFilesDir(null).getAbsolutePath());
MyGLRenderer.setExternalDirectory(Environment.getExternalStorageDirectory().getPath());

MyGLRenderer.passAssetManager(getAssets());

MyGLRenderer.onSurfaceCreated();
}

public static boolean checkStoragePermission() { //before edit Boolean

Log.w(TAG, "calling...........");

return true;
}

}


but when I call
call_Java_checkStoragePermission()
I get the following error

10-09 16:34:48.502 18590-18590/com.user.game D/dalvikvm: GetMethodID: not returning static method Lcom/user/game/Game_Activity;.checkStoragePermission ()Z
10-09 01:15:17.081 14171-14171/com.user.game I/Debugging: JNI: error get methodID
10-09 01:15:17.081 14171-14171/com.user.game I/Debugging: bool = 0
10-09 01:15:17.081 14171-14171/com.user.game E/InputEventReceiver: Exception dispatching input event.
10-09 01:15:17.081 14171-14171/com.user.game E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
10-09 01:15:17.091 14171-14171/com.user.game E/MessageQueue-JNI: java.lang.NoSuchMethodError: no method with name='checkStoragePermission' signature='()Z' in class Lcom/user/game/Game_Activity;
at com.user.game.MyGLRenderer.pointerLeave(Native Method)


Can someone help me?

Answer

Use GetStaticMethodId for a static function:

jmethodID javaMethodRef = env->GetStaticMethodID(javaClassRef, "checkStoragePermission", "()Z");