Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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?

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

like image 606
Michael T Avatar asked Nov 04 '15 00:11

Michael T


2 Answers

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 : )

like image 82
Raphael.Liu Avatar answered Nov 10 '22 18:11

Raphael.Liu


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.

like image 3
Fomahaut Avatar answered Nov 10 '22 19:11

Fomahaut