Wednesday, October 6, 2010

JNI - where's the env->CallStringMethod?

It makes me cry. JNI is a necessary evil, but still evil.

Why isn't there some nice way to call a CallStringMethod? Especially when working with
Android's bundles, which have getString. It would make life a million times easier.


Anyway here's a workaround:

jstring my_java_string = (jstring)env->CallObjectMethod(bundle, myBundle.getString, env->NewStringUTF("test"));
if(
my_java_string == NULL){
// Error
return;
}
char*
my_c_string = strdup(env->GetStringUTFChars(my_java_string, 0));
env->ReleaseStringUTFChars(
my_java_string, my_c_string);

2 comments:

  1. It worked, thank you!

    ReplyDelete
  2. I think you're passing the wrong parameter to ReleaseStringUTFChars. You shouldn't be passing my_c_string, as that was returned from strdup. Instead you should pass the return value from the GetStringUTFChars call.

    ReplyDelete