android jni在getObjectClass

时间:2019-03-11 07:16:43

标签: android c++ java-native-interface

崩溃在某些android设备上很少发生。

代码在java类中调用一个返回字符串值的方法 该值将添加到现有字符串中并返回到调用方。

我想念什么?

这是追溯轨迹:

  #00  pc 000000000004a3d8  /system/lib/libc.so (tgkill+12)
  #01  pc 0000000000047b53  /system/lib/libc.so (pthread_kill+34)
  #02  pc 000000000001d6a9  /system/lib/libc.so (raise+10)
  #03  pc 00000000000191f5  /system/lib/libc.so (__libc_android_abort+34)
  #04  pc 0000000000017264  /system/lib/libc.so (abort+4)
  #05  pc 000000000031b859  /system/lib/libart.so (_ZN3art7Runtime5AbortEPKc+328)
  #06  pc 00000000000b5305  /system/lib/libart.so (_ZN3art10LogMessageD2Ev+1132)
  #07  pc 0000000000239d35  /system/lib/libart.so (_ZN3art9JavaVMExt8JniAbortEPKcS2_+1664)
  #08  pc 0000000000239f9b  /system/lib/libart.so (_ZN3art9JavaVMExt9JniAbortFEPKcS2_z+66)
  #09  pc 0000000000264fb7  /system/lib/libart.so (_ZN3art3JNI14GetObjectClassEP7_JNIEnvP8_jobject+478)
  #10  pc 00000000000852cd  /data/app/xxx.xxx.xxx-1/lib/arm/libmain.so (_ZN8JNIcalls3GetENSt6__ndk112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE+52)
  #11  pc 00000000000854d5  /data/app/xxx.xxx.xxx-1/lib/arm/libmain.so (_ZN8JNIcalls6UpdateEd+124)

这是jni代码:

std::string JNIcalls::Get(std::string key)
{
std::string value = "";
#ifdef __ANDROID__
// retrieve the JNI environment.
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();

// retrieve the Java instance of the SDLActivity
jobject activity = (jobject)SDL_AndroidGetActivity();

// find the Java class of the activity. It should be SDLActivity or a subclass of it.
jclass clazz(env->GetObjectClass(activity));

// find the identifier of the method to call
jmethodID method_id = env->GetStaticMethodID(clazz, "ImpJNIcallsGet", "(Ljava/lang/String;)Ljava/lang/String;");

jstring jkey = env->NewStringUTF(key.c_str());

// effectively call the Java method
jstring rv = (jstring)env->CallStaticObjectMethod(clazz, method_id, jkey);
if(rv != NULL) {
    const char *strReturn = env->GetStringUTFChars(rv, 0);

    int strlen_size = strlen(strReturn);
    for(int i = 0; i < strlen_size; i++)
    {
        value.append(1,strReturn[i]);
    }

    env->DeleteLocalRef(rv);
}
// clean up the local references.
env->DeleteLocalRef(activity);
env->DeleteLocalRef(clazz);
env->DeleteLocalRef(jkey);

#endif
return value;
}

void JNIcalls::Update()
{
    std::string result = JNIcalls::Get("reward_video_watched");
    //more code
}

0 个答案:

没有答案