Search code examples
candroid-studiojava-native-interface

Is it possible to create a NewGlobalRef from a Class in last Android versions?


I have import and tested this project:

https://github.com/android/ndk-samples/tree/master/hello-jni

in Android Studio and worked well.

But when I copy the code for another new project I have the follow error:

“Incompatible point types jclass and `jobject”

In this line:

    g_ctx.mainActivityClz = (*env).NewGlobalRef(clz);

enter image description here

Is it not possible to use “NewGlobalRef” to create a new reference for a class in more recent versions?


Solution

  • NewGlobalRef always returns a jobject even though you're giving it a jclass (which is a subclass of jobject).

    You can solve this warning by explicitly downcasting to jclass, either as

    (jclass)env->NewGlobalRef(...)
    

    or

    static_cast<jclass>(env->NewGlobalRef(...)