How to load react native JS bundle from network in Android?

前端 未结 2 1916
醉酒成梦
醉酒成梦 2021-01-13 03:27

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 informa

相关标签:
2条回答
  • 2021-01-13 04:10

    Yes, it's possible. you can give Code Push a try(but their android version not stable yet), or find out how to do that in their code.

    0 讨论(0)
  • 2021-01-13 04:23

    1.download bundle file(s) and manage the path(s).

    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.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(..);
        }
        ...
    }
    

    source code:

    com.facebook.react.ReactNativeHost#createReactInstanceManager
    com.facebook.react.ReactInstanceManager.Builder#setJSBundleFile
    com.facebook.react.cxxbridge.JSBundleLoader#createFileLoader
    

    ...

    may it help u : )

    0 讨论(0)
提交回复
热议问题