简体   繁体   中英

Load bundle for react native from remote server [Android]

I tried to create a offline bundle in react native for android app.I used the following steps :-

react-native init myProject
react-native run-android

created offline js bundle using the following command:-

reac-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest asset-path

Then build an apk with the following command and installed in a device and everything is fine.

.\gradlew assembleRelease

Now my question is can the app in device fetch the bundle from a specific server for example in this case i like to keep the index.android.bundle in a remote server and each time when user opens the app it should fetch from specific ip. So i need to provide the ip address some where before creating bundle.

After some google i found AppDelegate.m is the file for ios where we can mention the ip address.And for android it is mainactivity where you over ride the getJSBundle method but the information was not clear.Any links or steps to do this is helpful.

I accomplished this using following steps and hope will be helpful.

  1. Create a bundle of the app using the following command.This will create a file with .bundle extension and a res folder with all other resource files (.jpg,.json etc).

react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/

  1. Deploy this bundle along with the resource folder in server in my case i used a IIS Server with simple web config file to download the bundle file.

In Case of Android:-

  1. You need to add helper class that will use the URL to download the bundle and store it in your app.
  2. Override the getJSBundleMethod() from Main Application(can be found in android folder) to use this path instead.

In Case of IOS:-

  1. Its simple just reset the path in AppDelegate.m file.

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