mirror of
https://github.com/status-im/react-native.git
synced 2025-02-26 08:05:34 +00:00
Added Android support for loading multiple RAM bundles
Differential Revision: D5901574 fbshipit-source-id: 395bae41e58505918d7ad20ac432eba3361361ea
This commit is contained in:
parent
e691699820
commit
4162d73ec0
@ -14,6 +14,7 @@ LOCAL_SRC_FILES := \
|
||||
JSLoader.cpp \
|
||||
JSLogging.cpp \
|
||||
JniJSModulesUnbundle.cpp \
|
||||
JniRAMBundleRegistry.cpp \
|
||||
MethodInvoker.cpp \
|
||||
ModuleRegistryBuilder.cpp \
|
||||
NativeArray.cpp \
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include "CxxModuleWrapper.h"
|
||||
#include "JavaScriptExecutorHolder.h"
|
||||
#include "JniJSModulesUnbundle.h"
|
||||
#include "JniRAMBundleRegistry.h"
|
||||
#include "JNativeRunnable.h"
|
||||
#include "NativeArray.h"
|
||||
|
||||
@ -186,8 +187,8 @@ void CatalystInstanceImpl::jniLoadScriptFromAssets(
|
||||
auto manager = extractAssetManager(assetManager);
|
||||
auto script = loadScriptFromAssets(manager, sourceURL);
|
||||
if (JniJSModulesUnbundle::isUnbundle(manager, sourceURL)) {
|
||||
auto bundle = folly::make_unique<JniJSModulesUnbundle>(manager, sourceURL);
|
||||
auto registry = folly::make_unique<RAMBundleRegistry>(std::move(bundle));
|
||||
auto bundle = JniJSModulesUnbundle::fromEntryFile(manager, sourceURL);
|
||||
auto registry = folly::make_unique<JniRAMBundleRegistry>(std::move(bundle), manager, sourceURL);
|
||||
instance_->loadRAMBundle(
|
||||
std::move(registry),
|
||||
std::move(script),
|
||||
|
@ -10,6 +10,8 @@
|
||||
#include <sys/endian.h>
|
||||
#include <utility>
|
||||
|
||||
#include <folly/Memory.h>
|
||||
|
||||
using magic_number_t = uint32_t;
|
||||
const magic_number_t MAGIC_FILE_HEADER = 0xFB0BD1E5;
|
||||
const std::string MAGIC_FILE_NAME = "UNBUNDLE";
|
||||
@ -36,9 +38,15 @@ static asset_ptr openAsset(
|
||||
AAsset_close);
|
||||
}
|
||||
|
||||
JniJSModulesUnbundle::JniJSModulesUnbundle(AAssetManager *assetManager, const std::string& entryFile) :
|
||||
std::unique_ptr<JniJSModulesUnbundle> JniJSModulesUnbundle::fromEntryFile(
|
||||
AAssetManager *assetManager,
|
||||
const std::string& entryFile) {
|
||||
return folly::make_unique<JniJSModulesUnbundle>(assetManager, jsModulesDir(entryFile));
|
||||
}
|
||||
|
||||
JniJSModulesUnbundle::JniJSModulesUnbundle(AAssetManager *assetManager, const std::string& moduleDirectory) :
|
||||
m_assetManager(assetManager),
|
||||
m_moduleDirectory(jsModulesDir(entryFile)) {}
|
||||
m_moduleDirectory(moduleDirectory) {}
|
||||
|
||||
bool JniJSModulesUnbundle::isUnbundle(
|
||||
AAssetManager *assetManager,
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <android/asset_manager.h>
|
||||
#include <cxxreact/JSModulesUnbundle.h>
|
||||
|
||||
@ -14,10 +16,12 @@ class JniJSModulesUnbundle : public JSModulesUnbundle {
|
||||
*/
|
||||
public:
|
||||
JniJSModulesUnbundle() = default;
|
||||
JniJSModulesUnbundle(AAssetManager *assetManager, const std::string& entryFile);
|
||||
JniJSModulesUnbundle(AAssetManager *assetManager, const std::string& moduleDirectory);
|
||||
JniJSModulesUnbundle(JniJSModulesUnbundle&& other) = delete;
|
||||
JniJSModulesUnbundle& operator= (JSModulesUnbundle&& other) = delete;
|
||||
|
||||
static std::unique_ptr<JniJSModulesUnbundle> fromEntryFile(AAssetManager *assetManager, const std::string& entryFile);
|
||||
|
||||
static bool isUnbundle(
|
||||
AAssetManager *assetManager,
|
||||
const std::string& assetName);
|
||||
|
36
ReactAndroid/src/main/jni/react/jni/JniRAMBundleRegistry.cpp
Normal file
36
ReactAndroid/src/main/jni/react/jni/JniRAMBundleRegistry.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
#include "JniRAMBundleRegistry.h"
|
||||
|
||||
#include <libgen.h>
|
||||
|
||||
#include <folly/Conv.h>
|
||||
#include <folly/Memory.h>
|
||||
|
||||
#include "JniJSModulesUnbundle.h"
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
static std::string jsBundlesDir(const std::string& entryFile) {
|
||||
std::string dir = dirname(entryFile.c_str());
|
||||
std::string entryName = basename(entryFile.c_str());
|
||||
entryName.erase(entryName.find("."), std::string::npos);
|
||||
|
||||
std::string path = "js-bundles/" + entryName + "/";
|
||||
// android's asset manager does not work with paths that start with a dot
|
||||
return dir == "." ? path : dir + "/" + path;
|
||||
}
|
||||
|
||||
JniRAMBundleRegistry::JniRAMBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle, AAssetManager *assetManager, const std::string& entryFile) :
|
||||
RAMBundleRegistry(std::move(mainBundle)),
|
||||
m_assetManager(assetManager),
|
||||
m_baseDirectoryPath(jsBundlesDir(entryFile)) {}
|
||||
|
||||
std::unique_ptr<JSModulesUnbundle> JniRAMBundleRegistry::bundleById(uint32_t index) const {
|
||||
std::string bundlePathById = m_baseDirectoryPath + folly::to<std::string>(index) + "/js-modules/";
|
||||
return folly::make_unique<JniJSModulesUnbundle>(m_assetManager, bundlePathById);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
21
ReactAndroid/src/main/jni/react/jni/JniRAMBundleRegistry.h
Normal file
21
ReactAndroid/src/main/jni/react/jni/JniRAMBundleRegistry.h
Normal file
@ -0,0 +1,21 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
#include <android/asset_manager.h>
|
||||
#include <cxxreact/RAMBundleRegistry.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
class JniRAMBundleRegistry : public RAMBundleRegistry {
|
||||
public:
|
||||
JniRAMBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle, AAssetManager *assetManager, const std::string& entryFile);
|
||||
|
||||
protected:
|
||||
virtual std::unique_ptr<JSModulesUnbundle> bundleById(uint32_t index) const override;
|
||||
private:
|
||||
AAssetManager *m_assetManager = nullptr;
|
||||
std::string m_baseDirectoryPath;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user