1

I have an NDK shared library and I want to do some tests against it. I currently used googletest to create an native executable that links to the library, followed instructions in the README.NDK. A dummy executable can run on a Android emulator. Good.

Now, the tricky thing is that the shared library calls a lot of functions of an JNIEnv instance, for example:

  • env->NewStringUTF()
  • ...

The question is, how can an Android native executable get an instance of JNIEnv?

One method is to use the invocation API described below. http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html

However, I looked at the jni.h of NDK, and it seems disallow the usages of this API:

/*
 * VM initialization functions.
 *
 * Note these are the only symbols exported for JNI by the VM.
 */
#if 0  /* In practice, these are not exported by the NDK so don't declare them */
jint JNI_GetDefaultJavaVMInitArgs(void*);
jint JNI_CreateJavaVM(JavaVM**, JNIEnv**, void*);
jint JNI_GetCreatedJavaVMs(JavaVM**, jsize, jsize*);
#endif

Any suggestions are highly appreciated!

(I am new to Android so please correct me if I am wrong. Thanks!)

ZillGate
  • 1,173
  • 12
  • 22

1 Answers1

0

This is done via AttachCurrentThread().

It is benign to call this if the thread is already attached.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thank but AttachCurrentThread requires a JavaVM. However, JNI_CreateJavaVM is disallowed as the above code snippet shows... – ZillGate Feb 27 '17 at 02:51