12static jboolean
jniInit(JNIEnv *env, jobject thiz)
14 qCDebug(AndroidInitLog) << Q_FUNC_INFO;
16 const jclass context_cls = env->GetObjectClass(thiz);
21 const jmethodID get_app_context_id = env->GetMethodID(context_cls,
"getApplicationContext",
"()Landroid/content/Context;");
22 env->DeleteLocalRef(context_cls);
23 if (QJniEnvironment::checkAndClearExceptions(env)) {
27 const jobject app_context = env->CallObjectMethod(thiz, get_app_context_id);
28 if (QJniEnvironment::checkAndClearExceptions(env) || !app_context) {
32 const jclass app_context_cls = env->GetObjectClass(app_context);
33 if (!app_context_cls) {
34 env->DeleteLocalRef(app_context);
38 const jmethodID get_class_loader_id = env->GetMethodID(app_context_cls,
"getClassLoader",
"()Ljava/lang/ClassLoader;");
39 env->DeleteLocalRef(app_context_cls);
40 if (QJniEnvironment::checkAndClearExceptions(env)) {
41 env->DeleteLocalRef(app_context);
45 const jobject class_loader = env->CallObjectMethod(app_context, get_class_loader_id);
46 if (QJniEnvironment::checkAndClearExceptions(env)) {
47 env->DeleteLocalRef(app_context);
51 JavaVM* javaVm =
nullptr;
52 if (env->GetJavaVM(&javaVm) == JNI_OK) {
56 env->DeleteLocalRef(app_context);
57 env->DeleteLocalRef(class_loader);
59 return QJniEnvironment::checkAndClearExceptions(env) ? JNI_FALSE : JNI_TRUE;
64 qCDebug(AndroidInitLog) << Q_FUNC_INFO;
66 const JNINativeMethod javaMethods[] {
67 {
"nativeInit",
"()Z",
reinterpret_cast<void *
>(
jniInit)},
70 QJniEnvironment jniEnv;
71 (void) jniEnv.checkAndClearExceptions();
76 (void) jniEnv.checkAndClearExceptions();
80 const jint val = jniEnv->RegisterNatives(objectClass, javaMethods, std::size(javaMethods));
81 jniEnv->DeleteLocalRef(objectClass);
83 qCWarning(AndroidInitLog) <<
"Error registering methods:" << val;
84 (void) jniEnv.checkAndClearExceptions();
88 qCDebug(AndroidInitLog) <<
"Main Native Functions Registered";
90 (void) jniEnv.checkAndClearExceptions();