diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevInternalSettings.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevInternalSettings.java index 291f63adc..825bc1c59 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevInternalSettings.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevInternalSettings.java @@ -34,6 +34,7 @@ public class DevInternalSettings implements private static final String PREFS_ANIMATIONS_DEBUG_KEY = "animations_debug"; private static final String PREFS_RELOAD_ON_JS_CHANGE_KEY = "reload_on_js_change"; private static final String PREFS_INSPECTOR_DEBUG_KEY = "inspector_debug"; + private static final String PREFS_HOT_MODULE_REPLACEMENT_KEY = "hot_module_replacement"; private final SharedPreferences mPreferences; private final DevSupportManager mDebugManager; @@ -77,6 +78,14 @@ public class DevInternalSettings implements } } + public boolean isHotModuleReplacementEnabled() { + return mPreferences.getBoolean(PREFS_HOT_MODULE_REPLACEMENT_KEY, false); + } + + public void setHotModuleReplacementEnabled(boolean enabled) { + mPreferences.edit().putBoolean(PREFS_HOT_MODULE_REPLACEMENT_KEY, enabled).apply(); + } + public boolean isReloadOnJSChangeEnabled() { return mPreferences.getBoolean(PREFS_RELOAD_ON_JS_CHANGE_KEY, false); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java index 1ba9b96e3..3439eaf2d 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java @@ -25,7 +25,6 @@ import com.facebook.common.logging.FLog; import com.facebook.infer.annotation.Assertions; import com.facebook.react.bridge.UiThreadUtil; import com.facebook.react.common.ReactConstants; - import com.squareup.okhttp.Call; import com.squareup.okhttp.Callback; import com.squareup.okhttp.ConnectionPool; @@ -33,6 +32,7 @@ import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.Response; import com.squareup.okhttp.ResponseBody; + import okio.Okio; import okio.Sink; @@ -55,7 +55,7 @@ public class DevServerHelper { private static final String DEVICE_LOCALHOST = "localhost:8081"; private static final String BUNDLE_URL_FORMAT = - "http://%s/%s.bundle?platform=android&dev=%s"; + "http://%s/%s.bundle?platform=android&dev=%s&hot=%s"; private static final String SOURCE_MAP_URL_FORMAT = BUNDLE_URL_FORMAT.replaceFirst("\\.bundle", ".map"); private static final String LAUNCH_CHROME_DEVTOOLS_COMMAND_URL_FORMAT = @@ -120,12 +120,19 @@ public class DevServerHelper { } /** - * @return whether we should enabled dev mode or not when requesting JS bundles. + * @return whether we should enable dev mode when requesting JS bundles. */ private boolean getDevMode() { return mSettings.isJSDevModeEnabled(); } + /** + * @return whether we should enabled HMR when requesting JS bundles. + */ + private boolean getHMR() { + return mSettings.isHotModuleReplacementEnabled(); + } + /** * @return the host to use when connecting to the bundle server. */ @@ -160,15 +167,15 @@ public class DevServerHelper { return Build.FINGERPRINT.contains("generic"); } - private static String createBundleURL(String host, String jsModulePath, boolean devMode) { - return String.format(Locale.US, BUNDLE_URL_FORMAT, host, jsModulePath, devMode); + private static String createBundleURL(String host, String jsModulePath, boolean devMode, boolean hmr) { + return String.format(Locale.US, BUNDLE_URL_FORMAT, host, jsModulePath, devMode, hmr); } public void downloadBundleFromURL( final BundleDownloadCallback callback, final String jsModulePath, final File outputFile) { - final String bundleURL = createBundleURL(getDebugServerHost(), jsModulePath, getDevMode()); + final String bundleURL = createBundleURL(getDebugServerHost(), jsModulePath, getDevMode(), getHMR()); Request request = new Request.Builder() .url(bundleURL) .build(); @@ -354,17 +361,17 @@ public class DevServerHelper { } public String getSourceMapUrl(String mainModuleName) { - return String.format(Locale.US, SOURCE_MAP_URL_FORMAT, getDebugServerHost(), mainModuleName, getDevMode()); + return String.format(Locale.US, SOURCE_MAP_URL_FORMAT, getDebugServerHost(), mainModuleName, getDevMode(), getHMR()); } public String getSourceUrl(String mainModuleName) { - return String.format(Locale.US, BUNDLE_URL_FORMAT, getDebugServerHost(), mainModuleName, getDevMode()); + return String.format(Locale.US, BUNDLE_URL_FORMAT, getDebugServerHost(), mainModuleName, getDevMode(), getHMR()); } public String getJSBundleURLForRemoteDebugging(String mainModuleName) { // The host IP we use when connecting to the JS bundle server from the emulator is not the // same as the one needed to connect to the same server from the Chrome proxy running on the // host itself. - return createBundleURL(getHostForJSProxy(), mainModuleName, getDevMode()); + return createBundleURL(getHostForJSProxy(), mainModuleName, getDevMode(), getHMR()); } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManager.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManager.java index ce60e861c..2625bec62 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManager.java @@ -262,6 +262,17 @@ public class DevSupportManager implements NativeModuleCallExceptionHandler { handleReloadJS(); } }); + options.put( + mDevSettings.isHotModuleReplacementEnabled() + ? mApplicationContext.getString(R.string.catalyst_hot_module_replacement_off) + : mApplicationContext.getString(R.string.catalyst_hot_module_replacement), + new DevOptionHandler() { + @Override + public void onOptionSelected() { + mDevSettings.setHotModuleReplacementEnabled(!mDevSettings.isHotModuleReplacementEnabled()); + handleReloadJS(); + } + }); options.put( mDevSettings.isReloadOnJSChangeEnabled() ? mApplicationContext.getString(R.string.catalyst_live_reload_off) diff --git a/ReactAndroid/src/main/res/devsupport/values/strings.xml b/ReactAndroid/src/main/res/devsupport/values/strings.xml index 1e60598d3..d56ca71d1 100644 --- a/ReactAndroid/src/main/res/devsupport/values/strings.xml +++ b/ReactAndroid/src/main/res/devsupport/values/strings.xml @@ -3,6 +3,8 @@ Reload JS Debug in Chrome Stop Chrome Debugging + Enable Hot Module Replacement + Disable Hot Module Replacement Enable Live Reload Disable Live Reload Enable Perf Monitor