Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load ReactNativeHost for Android with a bundle from a remote host

I've pre-compiled my react-native bundle and uploaded it to my server (fake url):

http://my-bundle-server.com/index.android.bundle.js

For iOS I can just create a view using that location and it works just fine:

 jsCodeLocation = "http://my-bundle-server.com/index.android.bundle.js";
 RCTRootView *rootView = [[RCTRootView alloc]
     initWithBundleURL:jsCodeLocation                                                      
     moduleName:@"MyCoolComponent"
     initialProperties:nil                                                  
     launchOptions:launchOptions];

With Android it's a different story, I couldn't find a way I can define that path for a remote bundle. I found the docs for the ReactNativeHost and tried to set the getJSBundleFile but it gives me `Could not load the file 'http://....js':

https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/ReactNativeHost.java

Returns a custom path of the bundle file. This is used in cases the bundle should be loaded from a custom path. By default, it is loaded from Android assets, from a path specified by {@link getBundleAssetName}. e.g. "file://sdcard/myapp_cache/index.android.bundle"

It seems that it won't work with the remote locations. It worth mentioning that I'm creating my host in Application so I can reuse it in different fragments of my app.

How can I define pre-compiled bundle location to load for an Android app?

like image 300
Alexey Strakh Avatar asked Oct 18 '25 16:10

Alexey Strakh


1 Answers

The default implementation seems to use filesystem implementation:

https://github.com/facebook/react-native/blob/4466b6fa7ce5325cac3233a23b10ffcb4ff038a7/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java#L93

But it seems there is a possibility of exposing a bundler that could load a remote url here (otherwise how would dev server url load bundle from localhost- rite?):

https://github.com/facebook/react-native/blob/4466b6fa7ce5325cac3233a23b10ffcb4ff038a7/ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoader.java#L65

along with external facing api to set a custom bundleloader here:

https://github.com/facebook/react-native/blob/4466b6fa7ce5325cac3233a23b10ffcb4ff038a7/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java#L112

P.S. I haven't tried any of above, but it seems workable.

like image 176
jay shah Avatar answered Oct 21 '25 06:10

jay shah