diff --git a/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.cpp b/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.cpp index cb04af1ec..bb0ed35b0 100644 --- a/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.cpp +++ b/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.cpp @@ -101,26 +101,24 @@ CatalystInstanceImpl::CatalystInstanceImpl() void CatalystInstanceImpl::registerNatives() { registerHybrid({ - makeNativeMethod("initHybrid", CatalystInstanceImpl::initHybrid), - makeNativeMethod("initializeBridge", CatalystInstanceImpl::initializeBridge), - makeNativeMethod("jniSetSourceURL", CatalystInstanceImpl::jniSetSourceURL), - makeNativeMethod("jniLoadScriptFromAssets", - "(Landroid/content/res/AssetManager;Ljava/lang/String;)V", - CatalystInstanceImpl::jniLoadScriptFromAssets), - makeNativeMethod("jniLoadScriptFromFile", CatalystInstanceImpl::jniLoadScriptFromFile), - makeNativeMethod("jniLoadScriptFromOptimizedBundle", - CatalystInstanceImpl::jniLoadScriptFromOptimizedBundle), - makeNativeMethod("callJSFunction", CatalystInstanceImpl::callJSFunction), - makeNativeMethod("callJSCallback", CatalystInstanceImpl::callJSCallback), - makeNativeMethod("getMainExecutorToken", CatalystInstanceImpl::getMainExecutorToken), - makeNativeMethod("setGlobalVariable", CatalystInstanceImpl::setGlobalVariable), - makeNativeMethod("getJavaScriptContext", CatalystInstanceImpl::getJavaScriptContext), - makeNativeMethod("handleMemoryPressureUiHidden", CatalystInstanceImpl::handleMemoryPressureUiHidden), - makeNativeMethod("handleMemoryPressureModerate", CatalystInstanceImpl::handleMemoryPressureModerate), - makeNativeMethod("handleMemoryPressureCritical", CatalystInstanceImpl::handleMemoryPressureCritical), - makeNativeMethod("supportsProfiling", CatalystInstanceImpl::supportsProfiling), - makeNativeMethod("startProfiler", CatalystInstanceImpl::startProfiler), - makeNativeMethod("stopProfiler", CatalystInstanceImpl::stopProfiler), + makeNativeMethod("initHybrid", CatalystInstanceImpl::initHybrid), + makeNativeMethod("initializeBridge", CatalystInstanceImpl::initializeBridge), + makeNativeMethod("jniSetSourceURL", CatalystInstanceImpl::jniSetSourceURL), + makeNativeMethod("jniLoadScriptFromAssets", CatalystInstanceImpl::jniLoadScriptFromAssets), + makeNativeMethod("jniLoadScriptFromFile", CatalystInstanceImpl::jniLoadScriptFromFile), + makeNativeMethod("jniLoadScriptFromOptimizedBundle", + CatalystInstanceImpl::jniLoadScriptFromOptimizedBundle), + makeNativeMethod("callJSFunction", CatalystInstanceImpl::callJSFunction), + makeNativeMethod("callJSCallback", CatalystInstanceImpl::callJSCallback), + makeNativeMethod("getMainExecutorToken", CatalystInstanceImpl::getMainExecutorToken), + makeNativeMethod("setGlobalVariable", CatalystInstanceImpl::setGlobalVariable), + makeNativeMethod("getJavaScriptContext", CatalystInstanceImpl::getJavaScriptContext), + makeNativeMethod("handleMemoryPressureUiHidden", CatalystInstanceImpl::handleMemoryPressureUiHidden), + makeNativeMethod("handleMemoryPressureModerate", CatalystInstanceImpl::handleMemoryPressureModerate), + makeNativeMethod("handleMemoryPressureCritical", CatalystInstanceImpl::handleMemoryPressureCritical), + makeNativeMethod("supportsProfiling", CatalystInstanceImpl::supportsProfiling), + makeNativeMethod("startProfiler", CatalystInstanceImpl::startProfiler), + makeNativeMethod("stopProfiler", CatalystInstanceImpl::stopProfiler), }); JNativeRunnable::registerNatives(); @@ -163,8 +161,9 @@ void CatalystInstanceImpl::jniSetSourceURL(const std::string& sourceURL) { instance_->setSourceURL(sourceURL); } -void CatalystInstanceImpl::jniLoadScriptFromAssets(jobject assetManager, - const std::string& assetURL) { +void CatalystInstanceImpl::jniLoadScriptFromAssets( + jni::alias_ref assetManager, + const std::string& assetURL) { const int kAssetsLength = 9; // strlen("assets://"); auto sourceURL = assetURL.substr(kAssetsLength); diff --git a/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.h b/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.h index a04a17081..78adfc656 100644 --- a/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.h +++ b/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.h @@ -2,12 +2,12 @@ #include +#include #include -#include - -#include "JMessageQueueThread.h" #include "JExecutorToken.h" +#include "JMessageQueueThread.h" +#include "JSLoader.h" namespace facebook { namespace react { @@ -18,14 +18,12 @@ class ModuleRegistryHolder; class NativeArray; struct ReactCallback : public jni::JavaClass { - static constexpr auto kJavaDescriptor = - "Lcom/facebook/react/cxxbridge/ReactCallback;"; + static constexpr auto kJavaDescriptor = "Lcom/facebook/react/cxxbridge/ReactCallback;"; }; class CatalystInstanceImpl : public jni::HybridClass { public: - static constexpr auto kJavaDescriptor = - "Lcom/facebook/react/cxxbridge/CatalystInstanceImpl;"; + static constexpr auto kJavaDescriptor = "Lcom/facebook/react/cxxbridge/CatalystInstanceImpl;"; static jni::local_ref initHybrid(jni::alias_ref); @@ -55,7 +53,7 @@ class CatalystInstanceImpl : public jni::HybridClass { */ void jniSetSourceURL(const std::string& sourceURL); - void jniLoadScriptFromAssets(jobject assetManager, const std::string& assetURL); + void jniLoadScriptFromAssets(jni::alias_ref assetManager, const std::string& assetURL); void jniLoadScriptFromFile(const std::string& fileName, const std::string& sourceURL); void jniLoadScriptFromOptimizedBundle(const std::string& bundlePath, const std::string& sourceURL, jint flags); void callJSFunction(JExecutorToken* token, std::string module, std::string method, NativeArray* arguments); diff --git a/ReactAndroid/src/main/jni/xreact/jni/JSLoader.cpp b/ReactAndroid/src/main/jni/xreact/jni/JSLoader.cpp index d21cb6f94..717e78b27 100644 --- a/ReactAndroid/src/main/jni/xreact/jni/JSLoader.cpp +++ b/ReactAndroid/src/main/jni/xreact/jni/JSLoader.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include #include #include #include @@ -15,26 +15,40 @@ using fbsystrace::FbSystraceSection; #endif +using namespace facebook::jni; + namespace facebook { namespace react { -static jclass gApplicationHolderClass; -static jmethodID gGetApplicationMethod; -static jmethodID gGetAssetManagerMethod; +struct JApplication : JavaClass { + static constexpr auto kJavaDescriptor = "Landroid/app/Application;"; + + local_ref getAssets() { + static auto method = javaClassStatic()->getMethod("getAssets"); + return method(self()); + } +}; + +struct JApplicationHolder : JavaClass { + static constexpr auto kJavaDescriptor = "Lcom/facebook/react/common/ApplicationHolder;"; + + static local_ref getApplication() { + static auto method = javaClassStatic() + ->getStaticMethod("getApplication"); + return method(javaClassStatic()); + } +}; std::unique_ptr loadScriptFromAssets(const std::string& assetName) { - JNIEnv *env = jni::Environment::current(); - jobject application = env->CallStaticObjectMethod( - gApplicationHolderClass, - gGetApplicationMethod); - jobject assetManager = env->CallObjectMethod(application, gGetAssetManagerMethod); - return loadScriptFromAssets(AAssetManager_fromJava(env, assetManager), assetName); + auto env = Environment::current(); + auto assetManager = JApplicationHolder::getApplication()->getAssets(); + return loadScriptFromAssets(AAssetManager_fromJava(env, assetManager.get()), assetName); } __attribute__((visibility("default"))) -AAssetManager *extractAssetManager(jobject jassetManager) { - auto env = jni::Environment::current(); - return AAssetManager_fromJava(env, jassetManager); +AAssetManager *extractAssetManager(alias_ref assetManager) { + auto env = Environment::current(); + return AAssetManager_fromJava(env, assetManager.get()); } __attribute__((visibility("default"))) @@ -88,14 +102,4 @@ std::string loadScriptFromFile(const std::string& fileName) { return ""; } -void registerJSLoaderNatives() { - JNIEnv *env = jni::Environment::current(); - jclass applicationHolderClass = env->FindClass("com/facebook/react/common/ApplicationHolder"); - gApplicationHolderClass = (jclass)env->NewGlobalRef(applicationHolderClass); - gGetApplicationMethod = env->GetStaticMethodID(applicationHolderClass, "getApplication", "()Landroid/app/Application;"); - - jclass appClass = env->FindClass("android/app/Application"); - gGetAssetManagerMethod = env->GetMethodID(appClass, "getAssets", "()Landroid/content/res/AssetManager;"); -} - } } diff --git a/ReactAndroid/src/main/jni/xreact/jni/JSLoader.h b/ReactAndroid/src/main/jni/xreact/jni/JSLoader.h index 6dc1d90bd..576a7023e 100644 --- a/ReactAndroid/src/main/jni/xreact/jni/JSLoader.h +++ b/ReactAndroid/src/main/jni/xreact/jni/JSLoader.h @@ -2,14 +2,19 @@ #pragma once -#include -#include #include -#include + +#include +#include +#include namespace facebook { namespace react { +struct JAssetManager : jni::JavaClass { + static constexpr auto kJavaDescriptor = "Landroid/content/res/AssetManager;"; +}; + /** * Helper method for loading a JS script from Android assets without * a reference to an AssetManager. @@ -19,7 +24,7 @@ std::unique_ptr loadScriptFromAssets(const std::string& asset /** * Helper method for loading JS script from android asset */ -AAssetManager *extractAssetManager(jobject jassetManager); +AAssetManager *extractAssetManager(jni::alias_ref assetManager); std::unique_ptr loadScriptFromAssets(AAssetManager *assetManager, const std::string& assetName); @@ -28,6 +33,4 @@ std::unique_ptr loadScriptFromAssets(AAssetManager *assetMana */ std::string loadScriptFromFile(const std::string& fileName); -void registerJSLoaderNatives(); - } } diff --git a/ReactAndroid/src/main/jni/xreact/jni/OnLoad.cpp b/ReactAndroid/src/main/jni/xreact/jni/OnLoad.cpp index 5abf1c94a..a062f1ccf 100644 --- a/ReactAndroid/src/main/jni/xreact/jni/OnLoad.cpp +++ b/ReactAndroid/src/main/jni/xreact/jni/OnLoad.cpp @@ -166,7 +166,6 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { #ifdef WITH_INSPECTOR JInspector::registerNatives(); #endif - registerJSLoaderNatives(); NativeArray::registerNatives(); ReadableNativeArray::registerNatives();