Working toward making a vulkan + OpenXR app on Oculus Quest: How can I get a pointer to the JVM?

140 views
Skip to first unread message

Phil Dougherty

unread,
Aug 11, 2020, 10:37:41 PM8/11/20
to android-ndk
Slowly figuring out how to get all of these things correctly hooked together-

on the oculus website ( https://developer.oculus.com/downloads/package/oculus-openxr-mobile-sdk/) it has the following snippet:

loaderInitializeInfoAndroid.applicationVM = data->applicationVM; 

This is part of a process to hook up the openXR loader. However, this documentation shows nothing of how to get such a pointer. Googling "NDK get JVM" isn't coming up with any straightforward documentation either...

So, how can I get access to the JVM from within C++ using the NDK?

What do I need to link against? What do I need to #include? What initialization do I need to do?

I'm using android_native_app_glue.h, which gets me access to the ANativeActivity (via struct android_app), which I also needed in the snippet from the oculus website. But nothing is there regarding the JVM.

mic _

unread,
Aug 12, 2020, 3:12:42 AM8/12/20
to andro...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/e85f2b88-218f-4606-a297-be6b9b42f8a3n%40googlegroups.com.

Florian Limberger

unread,
Aug 12, 2020, 9:22:54 AM8/12/20
to android-ndk
I don't know how the Oculus SDK integrates with Android, but typically, an Android app has a Java entrypoint to the native code in form of a native method, like this:

public class MyActivity extends Activity {
    static {
        System.loadLibrary("mylib");
    }

    public void onCreate() {
        super.onCreate();
        if (!myInit()) {
            // handle the error
        }
    }

    private static native boolean myInit();
}

This method provided by a shared library (in this example mylib.so) like described here: https://developer.android.com/training/articles/perf-jni#native-libraries
All the native functions take a pointer to a JNIEnv as first parameter, which can then used to retrieve a pointer to the JVM:

#include <jni.h>

// Do the JNI_OnLoad() dance here, described in the link above

jboolean myInit(JNIEnv *env, jclass clazz) {
    (void)clazz;
    JavaVM *jvm = NULL;
    jint result = GetJavaVM(env, &jvm);
    if (result < 0) {
        return JNI_FALSE;
    }
    return JNI_TRUE;
}


Disclaimer: I wrote all code above from top of my head and tested nothing of this. I hope this helps anyway.

Phil Dougherty

unread,
Aug 12, 2020, 11:58:54 AM8/12/20
to android-ndk
ah awesome. that's... really strange they would have me separately pass in the ANativeActivity AND the JavaVM when one is accessible from the other...

Florian Limberger

unread,
Aug 13, 2020, 5:07:31 AM8/13/20
to android-ndk
As far as I understand it, ANativeActivity is for convenience and not necessary.
At work we use native code from a Service with the approach I described above.
If you use ANativeActivity, you do not need to retrieve the pointer to the JVM manually.
Reply all
Reply to author
Forward
0 new messages