From 690bf12c4cc52e7afe8e38afe5e3f43dd0d2d1c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ignacio=20Rold=C3=A1n=20Etcheverry?= Date: Mon, 8 Nov 2021 02:35:06 +0100 Subject: [PATCH] C#: Don't use ActivityThread to determine Android nativeLibraryDir --- .../mono/mono_gd/support/android_support.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/modules/mono/mono_gd/support/android_support.cpp b/modules/mono/mono_gd/support/android_support.cpp index 07b7f0ff841..677e24f6189 100644 --- a/modules/mono/mono_gd/support/android_support.cpp +++ b/modules/mono/mono_gd/support/android_support.cpp @@ -109,16 +109,18 @@ bool jni_exception_check(JNIEnv *p_env) { String app_native_lib_dir_cache; String determine_app_native_lib_dir() { + // The JNI code is the equivalent of: + // + // return godotActivity.getApplicationInfo().nativeLibraryDir; + JNIEnv *env = get_jni_env(); - ScopedLocalRef activityThreadClass(env, env->FindClass("android/app/ActivityThread")); - jmethodID currentActivityThread = env->GetStaticMethodID(activityThreadClass, "currentActivityThread", "()Landroid/app/ActivityThread;"); - ScopedLocalRef activityThread(env, env->CallStaticObjectMethod(activityThreadClass, currentActivityThread)); - jmethodID getApplication = env->GetMethodID(activityThreadClass, "getApplication", "()Landroid/app/Application;"); - ScopedLocalRef ctx(env, env->CallObjectMethod(activityThread, getApplication)); + GodotJavaWrapper *godot_java = static_cast(OS::get_singleton())->get_godot_java(); + jobject activity = godot_java->get_activity(); - jmethodID getApplicationInfo = env->GetMethodID(env->GetObjectClass(ctx), "getApplicationInfo", "()Landroid/content/pm/ApplicationInfo;"); - ScopedLocalRef applicationInfo(env, env->CallObjectMethod(ctx, getApplicationInfo)); + ScopedLocalRef contextClass(env, env->FindClass("android/content/Context")); + jmethodID getApplicationInfo = env->GetMethodID(contextClass, "getApplicationInfo", "()Landroid/content/pm/ApplicationInfo;"); + ScopedLocalRef applicationInfo(env, env->CallObjectMethod(activity, getApplicationInfo)); jfieldID nativeLibraryDirField = env->GetFieldID(env->GetObjectClass(applicationInfo), "nativeLibraryDir", "Ljava/lang/String;"); ScopedLocalRef nativeLibraryDir(env, (jstring)env->GetObjectField(applicationInfo, nativeLibraryDirField)); @@ -369,6 +371,7 @@ void initialize() { String so_path = path::join(app_native_lib_dir, godot_so_name); godot_dl_handle = try_dlopen(so_path, gd_mono_convert_dl_flags(MONO_DL_LAZY)); + ERR_FAIL_COND(!godot_dl_handle); } void cleanup() {