mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
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
This commit is contained in:
1 parent
5f69ff704a
commit
7cb10a8222
7 files changed
+28
-41
No files matched your search
@@ -10,11 +10,12 @@
|
||||
#include <string>
|
||||
|
||||
#include <android/asset_manager.h>
|
||||
#include <cxxreact/JSExecutor.h>
|
||||
#include <fbjni/fbjni.h>
|
||||
|
||||
namespace facebook::react {
|
||||
|
||||
class JSBigString;
|
||||
|
||||
struct JAssetManager : jni::JavaClass<JAssetManager> {
|
||||
static constexpr auto kJavaDescriptor = "Landroid/content/res/AssetManager;";
|
||||
};
|
||||
|
||||
@@ -6,9 +6,9 @@
|
||||
*/
|
||||
|
||||
#include "ResourceLoader.h"
|
||||
|
||||
#include <cxxreact/JSBigString.h>
|
||||
#include <glog/logging.h>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
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<const JSBigString> 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(
|
||||
|
||||
@@ -11,18 +11,21 @@
|
||||
#include <string>
|
||||
|
||||
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<const JSBigString> 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<const JSBigString> getResourceFileContents(const std::string &path);
|
||||
|
||||
private:
|
||||
static constexpr const auto CACHE_DIR = ".react-native-cxx-cache";
|
||||
|
||||
+6
-11
@@ -10,12 +10,15 @@
|
||||
|
||||
#include <android/asset_manager.h>
|
||||
#include <android/asset_manager_jni.h>
|
||||
#include <cxxreact/JSBigString.h>
|
||||
#include <fbjni/fbjni.h>
|
||||
#include <react/jni/JSLoader.h>
|
||||
#include <react/jni/JniHelper.h>
|
||||
|
||||
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<const JSBigString> ResourceLoader::getResourceFileContents(
|
||||
const std::string& path) {
|
||||
return loadScriptFromAssets(getAssetManager(), path);
|
||||
}
|
||||
|
||||
std::filesystem::path ResourceLoader::getCacheRootPath() {
|
||||
|
||||
@@ -5,11 +5,10 @@
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include <cxxreact/JSBigString.h>
|
||||
#include <react/io/ResourceLoader.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
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<const JSBigString>
|
||||
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
|
||||
@@ -5,6 +5,8 @@
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include "JniHelper.h"
|
||||
|
||||
#include <fbjni/Context.h>
|
||||
#include <fbjni/fbjni.h>
|
||||
|
||||
|
||||
@@ -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<JSBigStdString>(
|
||||
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;
|
||||
|
||||
Reference in new issue
Block a user