Skip to content

Latest commit

 

History

History
23 lines (22 loc) · 719 Bytes

2.9.md

File metadata and controls

23 lines (22 loc) · 719 Bytes

<init>

/**
 * 全局引用
 * 可以全局缓存
 */
extern "C"
JNIEXPORT jstring JNICALL
Java_com_jdqm_ndktutorials_jni_JNIReference_cacheWithGlobalReference(JNIEnv *env, jobject thiz) {
    static jclass stringClass = nullptr;
    if (stringClass == nullptr) {
        jclass cls = env->FindClass("java/lang/String");
        stringClass = static_cast<jclass>(env->NewGlobalRef(cls));
        env->DeleteLocalRef(cls);
    } else {
        LOGD("USER CACHED");
    }
    jmethodID mid = env->GetMethodID(stringClass, "<init>", "(Ljava/lang/String;)V");
    jstring str = env->NewStringUTF("string");
    return static_cast<jstring>(env->NewObject(stringClass, mid, str));
}