There are lot of examples that Android C/C++ native calls Java APIs. However, all of these examples I have read are the Android Java APIs call native first and then the native calls other Java APIs by using the passed JNI-ENV.
Without the passed JNI-ENV, how can the C/C++ get it? Is it possible that C/C++ native calls Java APIs in Android by creating JavaVM ?If so please share the links. I have gone through the one project CoCos2dx which iam unable to find clear details. github.com/nokia-developer/cocos2d-x-qt/tree/master/
Thanks in advance!
Here's how I do it. I found out how by referring to The Java Native Interface by Sheng Liang; see page 96. I have a class called ThreadEnv that owns a JNIEnv pointer for the current thread and creates it when it's first needed:
class ThreadEnv
{
public:
JNIEnv* GetEnv()
{
if (m_env == nullptr)
#if defined(ANDROID) || defined(__ANDROID__)
TheJvm->AttachCurrentThread(&m_env,nullptr);
#else
TheJvm->AttachCurrentThread((void**)&m_env,nullptr);
#endif
return m_env;
}
~ThreadEnv()
{
if (m_env)
TheJvm->DetachCurrentThread();
}
private:
JNIEnv* m_env = nullptr;
};
I then use it by making a ThreadEnv object a member of any C++ class that needs it in my JNI code, and calling GetEnv to get the JNIEnv pointer. Here's an example of how I use it in one of my classes: take a look at the OnChange member function.
class MyFrameworkObserver: public MFrameworkObserver, public MUserData
{
public:
MyFrameworkObserver(jobject aFrameworkObject): m_framework_object(aFrameworkObject) { }
~MyFrameworkObserver()
{
JNIEnv* env = m_thread_env.GetEnv();
if (env)
env->DeleteGlobalRef(m_framework_object);
}
private:
void OnViewChange() override { OnChange(TheFrameworkOnViewChangeMethodId); }
void OnMainDataChange() override { OnChange(TheFrameworkOnMainDataChangeMethodId); }
void OnDynamicDataChange() override { OnChange(TheFrameworkOnDynamicDataChangeMethodId); }
void OnChange(jmethodID aMethodID)
{
JNIEnv* env = m_thread_env.GetEnv();
if (env)
env->CallVoidMethod(m_framework_object,aMethodID);
}
jobject m_framework_object;
ThreadEnv m_thread_env;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With