From 7cb10a822245ef3a5f933112bed98bda1d7b9e4f Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Tue, 4 Nov 2025 07:07:16 -0800 Subject: [PATCH] Use AssetManagerString on Android (#54384) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/54384 Delegate to the existing AssetManager loading logic in RN Android in the platform-specific ResourceLoader bits. Changelog: [Internal] Reviewed By: christophpurrer Differential Revision: D86099684 fbshipit-source-id: 43d33aa9b4f353e817966d76b8272950d954feab --- .../src/main/jni/react/jni/JSLoader.h | 3 ++- .../react/io/ResourceLoader.cpp | 20 +++++++------------ .../react/io/ResourceLoader.h | 7 +++++-- .../io/platform/android/ResourceLoader.cpp | 17 ++++++---------- .../react/io/platform/cxx/ResourceLoader.cpp | 16 +++++---------- .../ReactCxxPlatform/react/jni/JniHelper.cpp | 2 ++ .../react/runtime/ReactHost.cpp | 4 +--- 7 files changed, 28 insertions(+), 41 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/jni/JSLoader.h b/packages/react-native/ReactAndroid/src/main/jni/react/jni/JSLoader.h index 4c0135bfe20..e41806bf68c 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/jni/JSLoader.h +++ b/packages/react-native/ReactAndroid/src/main/jni/react/jni/JSLoader.h @@ -10,11 +10,12 @@ #include #include -#include #include namespace facebook::react { +class JSBigString; + struct JAssetManager : jni::JavaClass { static constexpr auto kJavaDescriptor = "Landroid/content/res/AssetManager;"; }; diff --git a/packages/react-native/ReactCxxPlatform/react/io/ResourceLoader.cpp b/packages/react-native/ReactCxxPlatform/react/io/ResourceLoader.cpp index 5e800fba629..dd7e6817195 100644 --- a/packages/react-native/ReactCxxPlatform/react/io/ResourceLoader.cpp +++ b/packages/react-native/ReactCxxPlatform/react/io/ResourceLoader.cpp @@ -6,9 +6,9 @@ */ #include "ResourceLoader.h" + +#include #include -#include -#include namespace facebook::react { /* static */ bool ResourceLoader::isAbsolutePath(const std::string& path) { @@ -32,19 +32,13 @@ namespace facebook::react { return isResourceFile(path); } -/* static */ std::string ResourceLoader::getFileContents( +/* static */ std::unique_ptr ResourceLoader::getFileContents( const std::string& path) { - if (isAbsolutePath(path)) { - std::ifstream file(path, std::ios::binary); - if (!file.good()) { - return getResourceFileContents(path); - } - std::stringstream buffer; - buffer << file.rdbuf(); - return buffer.str(); + if (isResourceFile(path)) { + return getResourceFileContents(path); + } else { + return JSBigFileString::fromPath(path); } - - return getResourceFileContents(path); } /* static */ std::filesystem::path ResourceLoader::getCacheDirectory( diff --git a/packages/react-native/ReactCxxPlatform/react/io/ResourceLoader.h b/packages/react-native/ReactCxxPlatform/react/io/ResourceLoader.h index 870213964c9..8b4588a1e87 100644 --- a/packages/react-native/ReactCxxPlatform/react/io/ResourceLoader.h +++ b/packages/react-native/ReactCxxPlatform/react/io/ResourceLoader.h @@ -11,18 +11,21 @@ #include namespace facebook::react { + +class JSBigString; + class ResourceLoader { public: static bool isDirectory(const std::string &path); static bool isFile(const std::string &path); static bool isAbsolutePath(const std::string &path); - static std::string getFileContents(const std::string &path); + static std::unique_ptr getFileContents(const std::string &path); static std::filesystem::path getCacheDirectory(const std::string &path = std::string()); protected: static bool isResourceDirectory(const std::string &path); static bool isResourceFile(const std::string &path); - static std::string getResourceFileContents(const std::string &path); + static std::unique_ptr getResourceFileContents(const std::string &path); private: static constexpr const auto CACHE_DIR = ".react-native-cxx-cache"; diff --git a/packages/react-native/ReactCxxPlatform/react/io/platform/android/ResourceLoader.cpp b/packages/react-native/ReactCxxPlatform/react/io/platform/android/ResourceLoader.cpp index c6dedb6b468..616a23a281a 100644 --- a/packages/react-native/ReactCxxPlatform/react/io/platform/android/ResourceLoader.cpp +++ b/packages/react-native/ReactCxxPlatform/react/io/platform/android/ResourceLoader.cpp @@ -10,12 +10,15 @@ #include #include +#include #include +#include #include namespace facebook::react { namespace { + AAssetManager* assetManager_ = nullptr; AAssetManager* getAssetManager() { @@ -52,17 +55,9 @@ bool ResourceLoader::isResourceFile(const std::string& path) { return true; } -std::string ResourceLoader::getResourceFileContents(const std::string& path) { - auto asset = AAssetManager_open( - getAssetManager(), path.c_str(), AASSET_MODE_STREAMING); - if (asset == nullptr) { - throw std::runtime_error("File not found " + path); - } - - std::string result( - (const char*)AAsset_getBuffer(asset), (size_t)AAsset_getLength(asset)); - AAsset_close(asset); - return result; +std::unique_ptr ResourceLoader::getResourceFileContents( + const std::string& path) { + return loadScriptFromAssets(getAssetManager(), path); } std::filesystem::path ResourceLoader::getCacheRootPath() { diff --git a/packages/react-native/ReactCxxPlatform/react/io/platform/cxx/ResourceLoader.cpp b/packages/react-native/ReactCxxPlatform/react/io/platform/cxx/ResourceLoader.cpp index 8cd2d27c228..2ff6f354555 100644 --- a/packages/react-native/ReactCxxPlatform/react/io/platform/cxx/ResourceLoader.cpp +++ b/packages/react-native/ReactCxxPlatform/react/io/platform/cxx/ResourceLoader.cpp @@ -5,11 +5,10 @@ * LICENSE file in the root directory of this source tree. */ +#include #include #include -#include -#include namespace facebook::react { @@ -21,17 +20,12 @@ bool ResourceLoader::isResourceFile(const std::string& path) { return std::filesystem::exists(path) && !std::filesystem::is_directory(path); } -std::string ResourceLoader::getResourceFileContents(const std::string& path) { - std::ifstream file(path, std::ios::binary); - if (!file.good()) { - throw std::runtime_error("File not found " + path); - } - std::stringstream buffer; - buffer << file.rdbuf(); - return buffer.str(); +/* static */ std::unique_ptr +ResourceLoader::getResourceFileContents(const std::string& path) { + return JSBigFileString::fromPath(path); } -std::filesystem::path ResourceLoader::getCacheRootPath() { +/* static */ std::filesystem::path ResourceLoader::getCacheRootPath() { return std::filesystem::temp_directory_path(); } } // namespace facebook::react diff --git a/packages/react-native/ReactCxxPlatform/react/jni/JniHelper.cpp b/packages/react-native/ReactCxxPlatform/react/jni/JniHelper.cpp index a3689d5d094..3799ed893ad 100644 --- a/packages/react-native/ReactCxxPlatform/react/jni/JniHelper.cpp +++ b/packages/react-native/ReactCxxPlatform/react/jni/JniHelper.cpp @@ -5,6 +5,8 @@ * LICENSE file in the root directory of this source tree. */ +#include "JniHelper.h" + #include #include diff --git a/packages/react-native/ReactCxxPlatform/react/runtime/ReactHost.cpp b/packages/react-native/ReactCxxPlatform/react/runtime/ReactHost.cpp index a5f14842c82..d58d0a18692 100644 --- a/packages/react-native/ReactCxxPlatform/react/runtime/ReactHost.cpp +++ b/packages/react-native/ReactCxxPlatform/react/runtime/ReactHost.cpp @@ -484,9 +484,7 @@ bool ReactHost::loadScriptFromDevServer() { bool ReactHost::loadScriptFromBundlePath(const std::string& bundlePath) { try { LOG(INFO) << "Loading JS bundle from bundle path: " << bundlePath; - // TODO: use platform-native asset loading strategy - auto script = std::make_unique( - ResourceLoader::getFileContents(bundlePath)); + auto script = ResourceLoader::getFileContents(bundlePath); reactInstance_->loadScript(std::move(script), bundlePath); LOG(INFO) << "Loaded JS bundle from bundle path: " << bundlePath; return true;