Android ICS 4.0 NDK NewStringUTF is crashing down the App

前端 未结 10 1985
长情又很酷
长情又很酷 2020-12-02 15:01

I have a method in JNI C/C++ which takes jstring and returns back jstring some thing like as below,

  NATIVE_CALL(jstring, method)(JNIEnv * env, jobject obj         


        
10条回答
  •  误落风尘
    2020-12-02 15:45

    I also struggled with the same problem from the last day. Finally figured out a solution after a day .. I hope this reply may save someone's day..

    The problem was I was calling another function within the native function, used the returned string directly and which caused crash in android older versions

    So firstly I saved the string returned from another function to a variable then used it, and the problem gone :D

    The below example may clear your concept

    //older code with error
    //here key_ is the string from java code
    
    const char *key = env->GetStringUTFChars(key_, 0);
    const char *keyx = getkey(key).c_str();
    return env->NewStringUTF(keyx);
    

    And here is how I solved this error

    //newer code which is working
    //here key_ is the string from java code
    
    const char *key = env->GetStringUTFChars(key_, 0);
    string k = getkey(key);
    const char *keyx = k.c_str();
    return env->NewStringUTF(keyx);
    

    Happy coding :D

提交回复
热议问题