Examples and reference types Android JNI Java constructor

Dog First class constructor add the C ++ constructor to be instantiated directly
Here Insert Picture Description
add two buttons
Here Insert Picture Description
define two native methods in MainActivity testDog and testUnDog, the two click buttons calling these two methods, respectively
Here Insert Picture Description

And global reference constructor instantiated:

Here Insert Picture Description

Use NewGlobalRef dogClass to define a global reference. If you do not set a global reference, implicitly release dogClass, dogClass is not NULL but vacant, so click again "reference test" button will throw an error.

Global references must be manually released last set dogClass = NULL

// TODO 引用类型 + Java构造方法的实例化
jclass dogClass;
extern "C"
JNIEXPORT void JNICALL
Java_com_hongx_jni_MainActivity_testDog(JNIEnv *env, jobject thiz) {
    // 局部引用:如果在函数里面,是在栈区,不用回收,函数结束,会自动回收 ,为了专业性,最好要写回收
    if (dogClass == NULL) { // 第一次满足,  第二次不满足了
        // 局部引用的方式
//        const char * dog_class_str = "com/hongx/jni/Dog";
//        dogClass = env->FindClass(dog_class_str);
        // 解决各个局部引用带来的问题,全局引用(自己来提升)
        const char *dog_class_str = "com/hongx/jni/Dog";
        jclass temp = env->FindClass(dog_class_str);
        dogClass = static_cast<jclass>(env->NewGlobalRef(temp));
        // 手动释放全局引用之后,再次点击,没有进来
        __android_log_print(ANDROID_LOG_DEBUG, "Dog", "dogClass == NULL");
    }
    // Java构造方法的实例化
    const char *sig = "()V";
    const char *method = "<init>"; // Java构造方法的标识
    jmethodID init = env->GetMethodID(dogClass, method, sig);
    env->NewObject(dogClass, init); // 由于dogClass 是悬空的,直接报错
    // 会隐式释放 dogClass  , dogClass不为NULL, 悬空   同时手动全局释放一致
}
extern "C"
JNIEXPORT void JNICALL
Java_com_hongx_jni_MainActivity_testUnDog(JNIEnv *env, jobject thiz) {
    if (dogClass != NULL) {
        __android_log_print(ANDROID_LOG_DEBUG, "Dog", "全局应用被释放了,上面的按钮不能点击了,否则报错");
        env->DeleteGlobalRef(dogClass);
        dogClass = NULL;
    }
    // Studnet * student = new Student; // 堆 必须释放
}
Published 446 original articles · won praise 67 · views 240 000 +

Guess you like

Origin blog.csdn.net/hongxue8888/article/details/105127931