Android NativeActivity: AttachCurrentThread JNIInvokeInterface is not a structure or union

1.5k views Asked by At

I am using the NativeActivity from C. My goal is to get a reference to a JNIEnv so I can make further Android calls.

Going by the examples I have gathered on the Android documentation and Stackoverflow, I keep seeing this

state->activity->vm->AttachCurrentThread(&env, NULL);```

But I keep getting this error...

error: member reference base type 'JavaVM' (aka 'const struct JNIInvokeInterface *') is not a structure or union
1

There are 1 answers

1
bailey.cosier On BEST ANSWER

You need to dereference your "pointer to a JavaVM pointer".

This will work:

(*state->activity->vm)->AttachCurrentThread(...);

Since you are using C, the function signature will also be different since you need to also pass in a pointer to the JavaVM.

Try this:

JavaVM *vm = state->activity->vm;

(*vm)->AttachCurrentThread(vm, &env, NULL);

It looks like you are calling from C, and not C++. In that case there is a typedef for JavaVM to the JNIInvokeInterface pointer.

Here's the header definition for that type (jni.h)

struct _JavaVM;
typedef const struct JNINativeInterface* C_JNIEnv;

#if defined(__cplusplus)
typedef _JNIEnv JNIEnv;
typedef _JavaVM JavaVM;
#else
typedef const struct JNINativeInterface* JNIEnv;
typedef const struct JNIInvokeInterface* JavaVM;
#endif

Using a C compiler, you are calling functions on a pointer to a pointer, which is causing your described error: "not a structure or a union".

But the examples your using are for a C++ compiler, which as you can see in the header definition, is typedef'ed to a struct instead.

C usage is a bit different from C++