如何在Android中从网络加载本机JS捆绑包? [英] How to load react native JS bundle from network in Android?
问题描述
对于我的Android应用,我需要能够在运行时动态更新Bundle,并使用Assets中预先保存的bundle作为后备.我未能在官方文档.
For my Android app, I need to be able to dynamically update the Bundle at runtime and use the pre-saved bundle in my Assets as a fall back. I have failed to find any information on this in the Official Documentation.
在iOS版本的react native中,有一种方法可以让您指定从中加载JS捆绑包的URL,但是我还没有看到适用于Android的等效选项.对于Android,这怎么可能?
In the iOS version of react native, there is a method that lets you specify a URL from which to load the JS bundle, but I have yet to see an equivalent option for Android. How is this possible for Android?
更新看起来RN团队一直在研究此功能,并将成为下一个版本的一部分: https://github.com/facebook/react-native/commit/3a743ef228a14e07c77c5488b080413643ec9c4b
Update It looks like the RN team has been working on this very feature and will be part of the next release: https://github.com/facebook/react-native/commit/3a743ef228a14e07c77c5488b080413643ec9c4b
推荐答案
1.下载捆绑文件并管理路径.
1.download bundle file(s) and manage the path(s).
2.在您的应用程序中新建一个ReactNativeHost:
2.new a ReactNativeHost in your Application:
public String mBundleFilePath = "";
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
protected boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
return Arrays.asList(new MainReactPackage(), new MyReactPackage());
}
@Override
protected String getJSBundleFile() {
return mBundleFilePath;
}
};
3.在启动ReactActivity之前,将ur捆绑文件的路径分配给变量:mBundleFilePath.
3.before starting a ReactActivity, assign the path of ur bundle file to variable: mBundleFilePath.
public class ReactNativeActivity extends ReactActivity {
public static void launchSelf(...) {
MyApplication.getInstance().mBundleFilePath = path;
COMPONENT_NAME = ...;
Intent intent = new Intent(activity,ReactNativeActivity.class);
activity.startActivity(..);
}
...
}
源代码:
com.facebook.react.ReactNativeHost#createReactInstanceManager
com.facebook.react.ReactInstanceManager.Builder#setJSBundleFile
com.facebook.react.cxxbridge.JSBundleLoader#createFileLoader
...
对您有帮助吗:)
这篇关于如何在Android中从网络加载本机JS捆绑包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!