简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM