How to download the response native JS package from the network in Android?

For my Android application, I need to be able to dynamically update the Bundle at run time and use the previously saved package in my assets as a rollback. I did not find any information about this in the Official Documentation.

In the iOS version for native response, there is a method that allows you to specify the URL from which the JS package is downloaded, but I still do not see the equivalent feature for Android. How is this possible for Android?

Update The RN team seems to be working on this very feature and will be part of the next release:   https://github.com/facebook/react-native/commit/3a743ef228a14e07c77c5488b080413643ec9c4b

+4
source share
2 answers

Yes it is possible. you can give Code Push a try (but their version for Android is not stable yet) or find out how to do it in your code.

+2
source

1. Download the package file and manage the path (s).

2.New 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 ReactActivity, assign the path of the ur bundle file to the 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:

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

...

can help u :)

+1
source

Source: https://habr.com/ru/post/1614372/


All Articles