【问题标题】:Getting an instance of JNIEnv in an Android Native Executable在 Android Native Executable 中获取 JNIEnv 的实例
【发布时间】:2017-02-26 20:43:29
【问题描述】:

我有一个 NDK 共享库,我想针对它做一些测试。我目前使用 googletest 创建链接到库的本机可执行文件,遵循 README.NDK 中的说明。虚拟可执行文件可以在 Android 模拟器上运行。很好。

现在,棘手的是共享库调用了很多 JNIEnv 实例的函数,例如:

  • env->NewStringUTF()
  • ...

问题是,Android 原生可执行文件如何获取 JNIEnv 的实例?

一种方法是使用下面描述的调用 API。 http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html

但是,我查看了 NDK 的 jni.h,似乎不允许使用此 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

非常感谢任何建议!

(我是 Android 新手,如有错误请指正。谢谢!)

【问题讨论】:

    标签: android android-ndk java-native-interface googletest


    【解决方案1】:

    这是通过AttachCurrentThread()完成的。

    如果线程已经附加,调用它是良性的。

    【讨论】:

    • 谢谢,但是 AttachCurrentThread 需要 JavaVM。但是,JNI_CreateJavaVM 是不允许的,因为上面的代码 sn-p 显示...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2011-08-24
    • 2016-06-16
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多