From c28f35c9fa1bc8d2a0d46978d8eb75d47c9c6213 Mon Sep 17 00:00:00 2001 From: Nate Hunzaker Date: Sat, 23 Feb 2019 15:36:16 -0800 Subject: [PATCH 1/9] Allow debug dev server port override This commit removes the hardcoded instances of port 8081 on Android with a build configuration property. This allows setting of the port React Native Android connects to for the local build server. For this change to work, there must also be an update to the react native CLI to pass along this setting. Related issues: https://github.com/facebook/react-native/issues/9145 --- ReactAndroid/build.gradle | 7 +++++++ ReactAndroid/src/main/java/com/facebook/react/common/BUCK | 1 + .../com/facebook/react/common/DebugServerException.java | 6 ++++-- .../com/facebook/react/common/build/ReactBuildConfig.java | 2 ++ .../react/modules/systeminfo/AndroidInfoHelpers.java | 7 ++++++- .../main/java/com/facebook/react/modules/systeminfo/BUCK | 1 + .../packagerconnection/PackagerConnectionSettings.java | 2 +- 7 files changed, 22 insertions(+), 4 deletions(-) diff --git a/ReactAndroid/build.gradle b/ReactAndroid/build.gradle index 343543143431bc..efe19e4d29007a 100644 --- a/ReactAndroid/build.gradle +++ b/ReactAndroid/build.gradle @@ -203,6 +203,11 @@ def findNdkBuildFullPath() { return null } +def debugHostPort(String defaultValue){ + def value = project.getProperties().get("debugHostPort") + return value != null ? value : defaultValue +} + def getNdkBuildFullPath() { def ndkBuildFullPath = findNdkBuildFullPath() if (ndkBuildFullPath == null) { @@ -289,6 +294,8 @@ android { buildConfigField("boolean", "IS_INTERNAL_BUILD", "false") buildConfigField("int", "EXOPACKAGE_FLAGS", "0") + buildConfigField("int", "DEBUG_SERVER_HOST_PORT", debugHostPort("8081")) + testApplicationId("com.facebook.react.tests.gradle") testInstrumentationRunner("androidx.test.runner.AndroidJUnitRunner") } diff --git a/ReactAndroid/src/main/java/com/facebook/react/common/BUCK b/ReactAndroid/src/main/java/com/facebook/react/common/BUCK index 5c67573a0254d1..cbbb80154edcad 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/common/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/common/BUCK @@ -38,6 +38,7 @@ rn_android_build_config( package = "com.facebook.react", values = [ "boolean IS_INTERNAL_BUILD = true", + "int DEBUG_SERVER_HOST_PORT = 8081", ], visibility = [ "PUBLIC", diff --git a/ReactAndroid/src/main/java/com/facebook/react/common/DebugServerException.java b/ReactAndroid/src/main/java/com/facebook/react/common/DebugServerException.java index bc2423b665b8ce..6dedb5c0cdf8c5 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/common/DebugServerException.java +++ b/ReactAndroid/src/main/java/com/facebook/react/common/DebugServerException.java @@ -18,6 +18,8 @@ import org.json.JSONException; import org.json.JSONObject; +import static com.facebook.react.common.build.ReactBuildConfig.DEBUG_SERVER_HOST_PORT; + /** * Tracks errors connecting to or received from the debug server. * The debug server returns errors as json objects. This exception represents that error. @@ -28,8 +30,8 @@ public class DebugServerException extends RuntimeException { "\u2022 Ensure that the packager server is running\n" + "\u2022 Ensure that your device/emulator is connected to your machine and has USB debugging enabled - run 'adb devices' to see a list of connected devices\n" + "\u2022 Ensure Airplane Mode is disabled\n" + - "\u2022 If you're on a physical device connected to the same machine, run 'adb reverse tcp:8081 tcp:8081' to forward requests from your device\n" + - "\u2022 If your device is on the same Wi-Fi network, set 'Debug server host & port for device' in 'Dev settings' to your machine's IP address and the port of the local dev server - e.g. 10.0.1.1:8081\n\n"; + "\u2022 If you're on a physical device connected to the same machine, run 'adb reverse tcp:" + DEBUG_SERVER_HOST_PORT + " tcp:" + DEBUG_SERVER_HOST_PORT + "' to forward requests from your device\n" + + "\u2022 If your device is on the same Wi-Fi network, set 'Debug server host & port for device' in 'Dev settings' to your machine's IP address and the port of the local dev server - e.g. 10.0.1.1:" + DEBUG_SERVER_HOST_PORT + "\n\n"; public static DebugServerException makeGeneric(String reason, Throwable t) { return makeGeneric(reason, "", t); diff --git a/ReactAndroid/src/main/java/com/facebook/react/common/build/ReactBuildConfig.java b/ReactAndroid/src/main/java/com/facebook/react/common/build/ReactBuildConfig.java index f2ea5a6a4b0378..8aa35b5ae7e8bf 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/common/build/ReactBuildConfig.java +++ b/ReactAndroid/src/main/java/com/facebook/react/common/build/ReactBuildConfig.java @@ -20,4 +20,6 @@ public class ReactBuildConfig { public static final boolean DEBUG = BuildConfig.DEBUG; public static final boolean IS_INTERNAL_BUILD = BuildConfig.IS_INTERNAL_BUILD; public static final int EXOPACKAGE_FLAGS = BuildConfig.EXOPACKAGE_FLAGS; + public static final int DEBUG_SERVER_HOST_PORT = BuildConfig.DEBUG_SERVER_HOST_PORT; + } diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java index c336a7b5d6ada8..376ebfa61fa0ab 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java @@ -14,6 +14,7 @@ import android.os.Build; import com.facebook.common.logging.FLog; +import com.facebook.react.common.build.ReactBuildConfig; public class AndroidInfoHelpers { @@ -23,7 +24,7 @@ public class AndroidInfoHelpers { public static final String METRO_HOST_PROP_NAME = "metro.host"; - private static final int DEBUG_SERVER_HOST_PORT = 8081; + private static final int DEBUG_SERVER_HOST_PORT = ReactBuildConfig.DEBUG_SERVER_HOST_PORT; private static final int INSPECTOR_PROXY_PORT = 8081; private static final String TAG = AndroidInfoHelpers.class.getSimpleName(); @@ -40,6 +41,10 @@ public static String getServerHost() { return getServerIpAddress(DEBUG_SERVER_HOST_PORT); } + public static String getAdbReverseTcpCommand() { + return "adb reverse tcp:" + DEBUG_SERVER_HOST_PORT + " tcp:" + DEBUG_SERVER_HOST_PORT; + } + public static String getInspectorProxyHost() { return getServerIpAddress(INSPECTOR_PROXY_PORT); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/BUCK b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/BUCK index 396d6c574e3046..5def9d8f6096f8 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/BUCK @@ -29,6 +29,7 @@ rn_android_library( "PUBLIC", ], deps = [ + react_native_target("java/com/facebook/react/common:common"), react_native_dep("libraries/fbcore/src/main/java/com/facebook/common/logging:logging"), react_native_dep("third-party/java/infer-annotations:infer-annotations"), react_native_dep("third-party/java/jsr-305:jsr-305"), diff --git a/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java b/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java index 98ac8cff7f552e..e7699a3851f9d8 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java +++ b/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java @@ -44,7 +44,7 @@ public String getDebugServerHost() { if (host.equals(AndroidInfoHelpers.DEVICE_LOCALHOST)) { FLog.w( TAG, - "You seem to be running on device. Run 'adb reverse tcp:8081 tcp:8081' " + + "You seem to be running on device. Run '" + AndroidInfoHelpers.getAdbReverseTcpCommand() + "' " + "to forward the debug server's port to the device."); } From 448907909cf135b3e49808247e3c60fc0067d4c7 Mon Sep 17 00:00:00 2001 From: Nate Hunzaker Date: Mon, 25 Feb 2019 17:21:41 -0800 Subject: [PATCH 2/9] Pass port through resource integer value --- ReactAndroid/build.gradle | 3 ++- .../main/java/com/facebook/react/common/BUCK | 3 +-- .../react/common/DebugServerException.java | 6 ++---- .../react/common/build/ReactBuildConfig.java | 1 - .../react/devsupport/DevServerHelper.java | 12 +++++++++--- .../modules/systeminfo/AndroidInfoHelpers.java | 8 ++++---- .../modules/systeminfo/AndroidInfoModule.java | 17 +++++++++++++---- .../PackagerConnectionSettings.java | 11 +++++++++-- react.gradle | 13 ++++++++++++- 9 files changed, 52 insertions(+), 22 deletions(-) diff --git a/ReactAndroid/build.gradle b/ReactAndroid/build.gradle index efe19e4d29007a..bc7749d077ba03 100644 --- a/ReactAndroid/build.gradle +++ b/ReactAndroid/build.gradle @@ -294,7 +294,8 @@ android { buildConfigField("boolean", "IS_INTERNAL_BUILD", "false") buildConfigField("int", "EXOPACKAGE_FLAGS", "0") - buildConfigField("int", "DEBUG_SERVER_HOST_PORT", debugHostPort("8081")) + + resValue "integer", "REACT_NATIVE_DEV_SERVER_PORT", "8081" testApplicationId("com.facebook.react.tests.gradle") testInstrumentationRunner("androidx.test.runner.AndroidJUnitRunner") diff --git a/ReactAndroid/src/main/java/com/facebook/react/common/BUCK b/ReactAndroid/src/main/java/com/facebook/react/common/BUCK index cbbb80154edcad..66cf8846d4b4a5 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/common/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/common/BUCK @@ -37,8 +37,7 @@ rn_android_build_config( name = "build_config", package = "com.facebook.react", values = [ - "boolean IS_INTERNAL_BUILD = true", - "int DEBUG_SERVER_HOST_PORT = 8081", + "boolean IS_INTERNAL_BUILD = true" ], visibility = [ "PUBLIC", diff --git a/ReactAndroid/src/main/java/com/facebook/react/common/DebugServerException.java b/ReactAndroid/src/main/java/com/facebook/react/common/DebugServerException.java index 6dedb5c0cdf8c5..6bc553490663a9 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/common/DebugServerException.java +++ b/ReactAndroid/src/main/java/com/facebook/react/common/DebugServerException.java @@ -18,8 +18,6 @@ import org.json.JSONException; import org.json.JSONObject; -import static com.facebook.react.common.build.ReactBuildConfig.DEBUG_SERVER_HOST_PORT; - /** * Tracks errors connecting to or received from the debug server. * The debug server returns errors as json objects. This exception represents that error. @@ -30,8 +28,8 @@ public class DebugServerException extends RuntimeException { "\u2022 Ensure that the packager server is running\n" + "\u2022 Ensure that your device/emulator is connected to your machine and has USB debugging enabled - run 'adb devices' to see a list of connected devices\n" + "\u2022 Ensure Airplane Mode is disabled\n" + - "\u2022 If you're on a physical device connected to the same machine, run 'adb reverse tcp:" + DEBUG_SERVER_HOST_PORT + " tcp:" + DEBUG_SERVER_HOST_PORT + "' to forward requests from your device\n" + - "\u2022 If your device is on the same Wi-Fi network, set 'Debug server host & port for device' in 'Dev settings' to your machine's IP address and the port of the local dev server - e.g. 10.0.1.1:" + DEBUG_SERVER_HOST_PORT + "\n\n"; + "\u2022 If you're on a physical device connected to the same machine, run 'adb reverse tcp: tcp:' to forward requests from your device\n" + + "\u2022 If your device is on the same Wi-Fi network, set 'Debug server host & port for device' in 'Dev settings' to your machine's IP address and the port of the local dev server - e.g. 10.0.1.1:\n\n"; public static DebugServerException makeGeneric(String reason, Throwable t) { return makeGeneric(reason, "", t); diff --git a/ReactAndroid/src/main/java/com/facebook/react/common/build/ReactBuildConfig.java b/ReactAndroid/src/main/java/com/facebook/react/common/build/ReactBuildConfig.java index 8aa35b5ae7e8bf..cd189102663bbb 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/common/build/ReactBuildConfig.java +++ b/ReactAndroid/src/main/java/com/facebook/react/common/build/ReactBuildConfig.java @@ -20,6 +20,5 @@ public class ReactBuildConfig { public static final boolean DEBUG = BuildConfig.DEBUG; public static final boolean IS_INTERNAL_BUILD = BuildConfig.IS_INTERNAL_BUILD; public static final int EXOPACKAGE_FLAGS = BuildConfig.EXOPACKAGE_FLAGS; - public static final int DEBUG_SERVER_HOST_PORT = BuildConfig.DEBUG_SERVER_HOST_PORT; } 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 2f70a32292ece7..58e80cd03c2e3c 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java @@ -8,6 +8,7 @@ package com.facebook.react.devsupport; import android.content.Context; +import android.content.res.Resources; import android.os.AsyncTask; import android.os.Handler; import android.os.Looper; @@ -261,7 +262,7 @@ protected Boolean doInBackground(Void... ignore) { public boolean doSync() { try { - String attachToNuclideUrl = getInspectorAttachUrl(title); + String attachToNuclideUrl = getInspectorAttachUrl(context, title); OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(attachToNuclideUrl).build(); client.newCall(request).execute(); @@ -367,11 +368,16 @@ private String getInspectorDeviceUrl() { mPackageName); } - private String getInspectorAttachUrl(String title) { + private String getInspectorAttachUrl(Context context, String title) { + + Resources resources = context.getResources(); + + Integer port = resources.getInteger(R.integer.REACT_NATIVE_DEV_SERVER_PORT); + return String.format( Locale.US, "http://%s/nuclide/attach-debugger-nuclide?title=%s&app=%s&device=%s", - AndroidInfoHelpers.getServerHost(), + AndroidInfoHelpers.getServerHost(port), title, mPackageName, AndroidInfoHelpers.getFriendlyDeviceName()); diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java index 376ebfa61fa0ab..b59d39b0d81dd8 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java @@ -37,12 +37,12 @@ private static boolean isRunningOnStockEmulator() { return Build.FINGERPRINT.contains("generic"); } - public static String getServerHost() { - return getServerIpAddress(DEBUG_SERVER_HOST_PORT); + public static String getServerHost(Integer port) { + return getServerIpAddress(port); } - public static String getAdbReverseTcpCommand() { - return "adb reverse tcp:" + DEBUG_SERVER_HOST_PORT + " tcp:" + DEBUG_SERVER_HOST_PORT; + public static String getAdbReverseTcpCommand(Integer port) { + return "adb reverse tcp:" + port + " tcp:" + port; } public static String getInspectorProxyHost() { diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoModule.java index 391dedccd869c5..6fb3f385d3236d 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoModule.java @@ -9,10 +9,13 @@ import android.annotation.SuppressLint; import android.app.UiModeManager; +import android.content.Context; import android.content.res.Configuration; +import android.content.res.Resources; import android.os.Build; import android.provider.Settings.Secure; +import com.facebook.react.R; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.common.build.ReactBuildConfig; @@ -35,9 +38,7 @@ public class AndroidInfoModule extends ReactContextBaseJavaModule { public static final String NAME = "PlatformConstants"; private static final String IS_TESTING = "IS_TESTING"; - public AndroidInfoModule(ReactApplicationContext reactContext) { - super(reactContext); - } + public AndroidInfoModule(ReactApplicationContext reactContext) { super(reactContext); } /** * See: https://developer.android.com/reference/android/app/UiModeManager.html#getCurrentModeType() @@ -74,7 +75,7 @@ public String getName() { constants.put("Fingerprint", Build.FINGERPRINT); constants.put("Model", Build.MODEL); if (ReactBuildConfig.DEBUG) { - constants.put("ServerHost", AndroidInfoHelpers.getServerHost()); + constants.put("ServerHost", getServerHost()); } constants.put("isTesting", "true".equals(System.getProperty(IS_TESTING)) || isRunningScreenshotTest()); @@ -96,4 +97,12 @@ private Boolean isRunningScreenshotTest() { return false; } } + + private String getServerHost() { + Resources resources = getReactApplicationContext().getApplicationContext().getResources(); + + Integer devServerPort = resources.getInteger(R.integer.REACT_NATIVE_DEV_SERVER_PORT); + + return AndroidInfoHelpers.getServerHost(devServerPort); + } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java b/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java index e7699a3851f9d8..5ff7a94370d779 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java +++ b/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java @@ -11,11 +11,13 @@ import android.content.Context; import android.content.SharedPreferences; +import android.content.res.Resources; import android.preference.PreferenceManager; import android.text.TextUtils; import com.facebook.common.logging.FLog; import com.facebook.infer.annotation.Assertions; +import com.facebook.react.R; import com.facebook.react.modules.systeminfo.AndroidInfoHelpers; public class PackagerConnectionSettings { @@ -24,10 +26,15 @@ public class PackagerConnectionSettings { private final SharedPreferences mPreferences; private final String mPackageName; + private final Integer mServerPort; public PackagerConnectionSettings(Context applicationContext) { mPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext); mPackageName = applicationContext.getPackageName(); + + Resources resources = applicationContext.getResources(); + + mServerPort = resources.getInteger(R.integer.REACT_NATIVE_DEV_SERVER_PORT); } public String getDebugServerHost() { @@ -39,12 +46,12 @@ public String getDebugServerHost() { return Assertions.assertNotNull(hostFromSettings); } - String host = AndroidInfoHelpers.getServerHost(); + String host = AndroidInfoHelpers.getServerHost(mServerPort); if (host.equals(AndroidInfoHelpers.DEVICE_LOCALHOST)) { FLog.w( TAG, - "You seem to be running on device. Run '" + AndroidInfoHelpers.getAdbReverseTcpCommand() + "' " + + "You seem to be running on device. Run '" + AndroidInfoHelpers.getAdbReverseTcpCommand(mServerPort) + "' " + "to forward the debug server's port to the device."); } diff --git a/react.gradle b/react.gradle index 07fd9e3f2541be..9fe8100e3afa3d 100644 --- a/react.gradle +++ b/react.gradle @@ -13,12 +13,23 @@ def entryFile = config.entryFile ?: "index.android.js" def bundleCommand = config.bundleCommand ?: "bundle" def reactRoot = file(config.root ?: "../../") def inputExcludes = config.inputExcludes ?: ["android/**", "ios/**"] -def bundleConfig = config.bundleConfig ? "${reactRoot}/${config.bundleConfig}" : null ; +def bundleConfig = config.bundleConfig ? "${reactRoot}/${config.bundleConfig}" : null; +def debugHostPort(String defaultValue) { + def value = project.getProperties().get("debugHostPort") + return value != null ? value : defaultValue +} + +android { + buildTypes.all { + resValue "integer", "REACT_NATIVE_DEV_SERVER_PORT", debugHostPort("8081") + } +} afterEvaluate { def isAndroidLibrary = plugins.hasPlugin("com.android.library") def variants = isAndroidLibrary ? android.libraryVariants : android.applicationVariants + variants.all { def variant -> // Create variant and target names def targetName = variant.name.capitalize() From b68f88b87134a4aa9f746bb2c2aefc111e3dc1db Mon Sep 17 00:00:00 2001 From: Nate Hunzaker Date: Mon, 25 Feb 2019 21:30:00 -0800 Subject: [PATCH 3/9] Pass dev server port through integer res value - Also adds new buck module for integer resource for testing --- .../react/devsupport/DevServerHelper.java | 7 +------ .../modules/systeminfo/AndroidInfoHelpers.java | 16 ++++++++++++++++ .../com/facebook/react/modules/systeminfo/BUCK | 2 ++ .../PackagerConnectionSettings.java | 17 ++++++----------- ReactAndroid/src/main/res/BUCK | 9 +++++++++ .../src/main/res/systeminfo/values/values.xml | 4 ++++ 6 files changed, 38 insertions(+), 17 deletions(-) create mode 100644 ReactAndroid/src/main/res/systeminfo/values/values.xml 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 58e80cd03c2e3c..deaffe8ed5baec 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java @@ -369,15 +369,10 @@ private String getInspectorDeviceUrl() { } private String getInspectorAttachUrl(Context context, String title) { - - Resources resources = context.getResources(); - - Integer port = resources.getInteger(R.integer.REACT_NATIVE_DEV_SERVER_PORT); - return String.format( Locale.US, "http://%s/nuclide/attach-debugger-nuclide?title=%s&app=%s&device=%s", - AndroidInfoHelpers.getServerHost(port), + AndroidInfoHelpers.getServerHost(context), title, mPackageName, AndroidInfoHelpers.getFriendlyDeviceName()); diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java index b59d39b0d81dd8..665798150a5651 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java @@ -11,9 +11,12 @@ import java.nio.charset.StandardCharsets; import java.util.Locale; +import android.content.Context; +import android.content.res.Resources; import android.os.Build; import com.facebook.common.logging.FLog; +import com.facebook.react.R; import com.facebook.react.common.build.ReactBuildConfig; public class AndroidInfoHelpers { @@ -41,10 +44,18 @@ public static String getServerHost(Integer port) { return getServerIpAddress(port); } + public static String getServerHost(Context context) { + return getServerIpAddress(getDevServerPort(context)); + } + public static String getAdbReverseTcpCommand(Integer port) { return "adb reverse tcp:" + port + " tcp:" + port; } + public static String getAdbReverseTcpCommand(Context context) { + return getAdbReverseTcpCommand(getDevServerPort(context)); + } + public static String getInspectorProxyHost() { return getServerIpAddress(INSPECTOR_PROXY_PORT); } @@ -59,6 +70,11 @@ public static String getFriendlyDeviceName() { } } + private static Integer getDevServerPort(Context context) { + Resources resources = context.getResources(); + return resources.getInteger(R.integer.REACT_NATIVE_DEV_SERVER_PORT); + } + private static String getServerIpAddress(int port) { // Since genymotion runs in vbox it use different hostname to refer to adb host. // We detect whether app runs on genymotion and replace js bundle server hostname accordingly diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/BUCK b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/BUCK index 5def9d8f6096f8..433ecaf80863a3 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/BUCK @@ -14,6 +14,7 @@ rn_android_library( react_native_target("java/com/facebook/react/bridge:bridge"), react_native_target("java/com/facebook/react/common:common"), react_native_target("java/com/facebook/react/module/annotations:annotations"), + react_native_target("res:systeminfo"), ], exported_deps = [ ":systeminfo-moduleless", @@ -33,5 +34,6 @@ rn_android_library( react_native_dep("libraries/fbcore/src/main/java/com/facebook/common/logging:logging"), react_native_dep("third-party/java/infer-annotations:infer-annotations"), react_native_dep("third-party/java/jsr-305:jsr-305"), + react_native_target("res:systeminfo"), ], ) diff --git a/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java b/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java index 5ff7a94370d779..c1e132edf5602a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java +++ b/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java @@ -7,34 +7,29 @@ package com.facebook.react.packagerconnection; -import javax.annotation.Nullable; - import android.content.Context; import android.content.SharedPreferences; -import android.content.res.Resources; import android.preference.PreferenceManager; import android.text.TextUtils; import com.facebook.common.logging.FLog; import com.facebook.infer.annotation.Assertions; -import com.facebook.react.R; import com.facebook.react.modules.systeminfo.AndroidInfoHelpers; +import javax.annotation.Nullable; + public class PackagerConnectionSettings { private static final String TAG = PackagerConnectionSettings.class.getSimpleName(); private static final String PREFS_DEBUG_SERVER_HOST_KEY = "debug_http_host"; private final SharedPreferences mPreferences; private final String mPackageName; - private final Integer mServerPort; + private final Context mAppContext; public PackagerConnectionSettings(Context applicationContext) { mPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext); mPackageName = applicationContext.getPackageName(); - - Resources resources = applicationContext.getResources(); - - mServerPort = resources.getInteger(R.integer.REACT_NATIVE_DEV_SERVER_PORT); + mAppContext = applicationContext; } public String getDebugServerHost() { @@ -46,12 +41,12 @@ public String getDebugServerHost() { return Assertions.assertNotNull(hostFromSettings); } - String host = AndroidInfoHelpers.getServerHost(mServerPort); + String host = AndroidInfoHelpers.getServerHost(mAppContext); if (host.equals(AndroidInfoHelpers.DEVICE_LOCALHOST)) { FLog.w( TAG, - "You seem to be running on device. Run '" + AndroidInfoHelpers.getAdbReverseTcpCommand(mServerPort) + "' " + + "You seem to be running on device. Run '" + AndroidInfoHelpers.getAdbReverseTcpCommand(mAppContext) + "' " + "to forward the debug server's port to the device."); } diff --git a/ReactAndroid/src/main/res/BUCK b/ReactAndroid/src/main/res/BUCK index afb0be006d627c..dfa1853a259397 100644 --- a/ReactAndroid/src/main/res/BUCK +++ b/ReactAndroid/src/main/res/BUCK @@ -36,4 +36,13 @@ rn_android_resource( ], ) +rn_android_resource( + name = "systeminfo", + package = "com.facebook.react", + res = "systeminfo", + visibility = [ + "PUBLIC" + ], +) + # New resource directories must be added to react-native-github/ReactAndroid/build.gradle diff --git a/ReactAndroid/src/main/res/systeminfo/values/values.xml b/ReactAndroid/src/main/res/systeminfo/values/values.xml new file mode 100644 index 00000000000000..141bc2d3ea0402 --- /dev/null +++ b/ReactAndroid/src/main/res/systeminfo/values/values.xml @@ -0,0 +1,4 @@ + + + 8081 + From 2a47e940a416b44604d09ca11d9ccc93b091a849 Mon Sep 17 00:00:00 2001 From: Nate Hunzaker Date: Mon, 25 Feb 2019 21:31:25 -0800 Subject: [PATCH 4/9] Revert trailing comma ommission --- ReactAndroid/src/main/java/com/facebook/react/common/BUCK | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/common/BUCK b/ReactAndroid/src/main/java/com/facebook/react/common/BUCK index 66cf8846d4b4a5..5c67573a0254d1 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/common/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/common/BUCK @@ -37,7 +37,7 @@ rn_android_build_config( name = "build_config", package = "com.facebook.react", values = [ - "boolean IS_INTERNAL_BUILD = true" + "boolean IS_INTERNAL_BUILD = true", ], visibility = [ "PUBLIC", From 1331f8ae701690373c5720f7a420b61a6743a578 Mon Sep 17 00:00:00 2001 From: Nate Hunzaker Date: Mon, 25 Feb 2019 21:34:53 -0800 Subject: [PATCH 5/9] Revert some whitespace and module styles --- .../com/facebook/react/common/build/ReactBuildConfig.java | 1 - .../facebook/react/modules/systeminfo/AndroidInfoHelpers.java | 2 -- .../react/packagerconnection/PackagerConnectionSettings.java | 4 ++-- react.gradle | 3 +-- 4 files changed, 3 insertions(+), 7 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/common/build/ReactBuildConfig.java b/ReactAndroid/src/main/java/com/facebook/react/common/build/ReactBuildConfig.java index cd189102663bbb..f2ea5a6a4b0378 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/common/build/ReactBuildConfig.java +++ b/ReactAndroid/src/main/java/com/facebook/react/common/build/ReactBuildConfig.java @@ -20,5 +20,4 @@ public class ReactBuildConfig { public static final boolean DEBUG = BuildConfig.DEBUG; public static final boolean IS_INTERNAL_BUILD = BuildConfig.IS_INTERNAL_BUILD; public static final int EXOPACKAGE_FLAGS = BuildConfig.EXOPACKAGE_FLAGS; - } diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java index 665798150a5651..fa5ac8503ed7b8 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java @@ -8,7 +8,6 @@ import java.io.BufferedReader; import java.io.InputStreamReader; import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.util.Locale; import android.content.Context; @@ -17,7 +16,6 @@ import com.facebook.common.logging.FLog; import com.facebook.react.R; -import com.facebook.react.common.build.ReactBuildConfig; public class AndroidInfoHelpers { diff --git a/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java b/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java index c1e132edf5602a..432c70d5166765 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java +++ b/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java @@ -12,12 +12,12 @@ import android.preference.PreferenceManager; import android.text.TextUtils; +import javax.annotation.Nullable; + import com.facebook.common.logging.FLog; import com.facebook.infer.annotation.Assertions; import com.facebook.react.modules.systeminfo.AndroidInfoHelpers; -import javax.annotation.Nullable; - public class PackagerConnectionSettings { private static final String TAG = PackagerConnectionSettings.class.getSimpleName(); private static final String PREFS_DEBUG_SERVER_HOST_KEY = "debug_http_host"; diff --git a/react.gradle b/react.gradle index 9fe8100e3afa3d..26fbfe3a78232e 100644 --- a/react.gradle +++ b/react.gradle @@ -13,7 +13,7 @@ def entryFile = config.entryFile ?: "index.android.js" def bundleCommand = config.bundleCommand ?: "bundle" def reactRoot = file(config.root ?: "../../") def inputExcludes = config.inputExcludes ?: ["android/**", "ios/**"] -def bundleConfig = config.bundleConfig ? "${reactRoot}/${config.bundleConfig}" : null; +def bundleConfig = config.bundleConfig ? "${reactRoot}/${config.bundleConfig}" : null ; def debugHostPort(String defaultValue) { def value = project.getProperties().get("debugHostPort") @@ -29,7 +29,6 @@ android { afterEvaluate { def isAndroidLibrary = plugins.hasPlugin("com.android.library") def variants = isAndroidLibrary ? android.libraryVariants : android.applicationVariants - variants.all { def variant -> // Create variant and target names def targetName = variant.name.capitalize() From 5fb254ea1237a0a0ce62209cc15f218b3eea0cce Mon Sep 17 00:00:00 2001 From: Nate Hunzaker Date: Wed, 27 Feb 2019 09:49:54 -0800 Subject: [PATCH 6/9] Downcase port resource. Pass port into DebugServerException This commit downcases REACT_NATIVE_DEV_SERVER_PORT to match other string resources and sets up the debug server to correctly report errors. --- ReactAndroid/build.gradle | 2 +- .../com/facebook/react/bridge/JSBundleLoader.java | 4 ++-- .../react/common/DebugServerException.java | 15 ++++++++++----- .../react/devsupport/BundleDownloader.java | 8 +++++--- .../modules/systeminfo/AndroidInfoHelpers.java | 2 +- .../modules/systeminfo/AndroidInfoModule.java | 2 +- .../src/main/res/systeminfo/values/values.xml | 2 +- react.gradle | 6 +++--- 8 files changed, 24 insertions(+), 17 deletions(-) diff --git a/ReactAndroid/build.gradle b/ReactAndroid/build.gradle index bc7749d077ba03..e3929680cd7e0c 100644 --- a/ReactAndroid/build.gradle +++ b/ReactAndroid/build.gradle @@ -295,7 +295,7 @@ android { buildConfigField("boolean", "IS_INTERNAL_BUILD", "false") buildConfigField("int", "EXOPACKAGE_FLAGS", "0") - resValue "integer", "REACT_NATIVE_DEV_SERVER_PORT", "8081" + resValue "integer", "react_native_dev_server_port", "8081" testApplicationId("com.facebook.react.tests.gradle") testInstrumentationRunner("androidx.test.runner.AndroidJUnitRunner") diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoader.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoader.java index 3aff0da69e3026..9a67632c5c3661 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoader.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoader.java @@ -72,7 +72,7 @@ public String loadScript(JSBundleLoaderDelegate delegate) { delegate.loadScriptFromFile(cachedFileLocation, sourceURL, false); return sourceURL; } catch (Exception e) { - throw DebugServerException.makeGeneric(e.getMessage(), e); + throw DebugServerException.makeGeneric(sourceURL, e.getMessage(), e); } } }; @@ -94,7 +94,7 @@ public String loadScript(JSBundleLoaderDelegate delegate) { delegate.loadScriptFromDeltaBundle(sourceURL, nativeDeltaClient, false); return sourceURL; } catch (Exception e) { - throw DebugServerException.makeGeneric(e.getMessage(), e); + throw DebugServerException.makeGeneric(sourceURL, e.getMessage(), e); } } }; diff --git a/ReactAndroid/src/main/java/com/facebook/react/common/DebugServerException.java b/ReactAndroid/src/main/java/com/facebook/react/common/DebugServerException.java index 6bc553490663a9..6c84a9bbc6f5f0 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/common/DebugServerException.java +++ b/ReactAndroid/src/main/java/com/facebook/react/common/DebugServerException.java @@ -11,6 +11,7 @@ import java.io.IOException; +import android.net.Uri; import android.text.TextUtils; import com.facebook.common.logging.FLog; @@ -31,12 +32,16 @@ public class DebugServerException extends RuntimeException { "\u2022 If you're on a physical device connected to the same machine, run 'adb reverse tcp: tcp:' to forward requests from your device\n" + "\u2022 If your device is on the same Wi-Fi network, set 'Debug server host & port for device' in 'Dev settings' to your machine's IP address and the port of the local dev server - e.g. 10.0.1.1:\n\n"; - public static DebugServerException makeGeneric(String reason, Throwable t) { - return makeGeneric(reason, "", t); + public static DebugServerException makeGeneric(String url, String reason, Throwable t) { + return makeGeneric(url, reason, "", t); } - public static DebugServerException makeGeneric(String reason, String extra, Throwable t) { - return new DebugServerException(reason + GENERIC_ERROR_MESSAGE + extra, t); + public static DebugServerException makeGeneric(String url, String reason, String extra, Throwable t) { + Uri uri = Uri.parse(url); + + String message = GENERIC_ERROR_MESSAGE.replace("", String.valueOf(uri.getPort())); + + return new DebugServerException(reason + message + extra, t); } private DebugServerException(String description, String fileName, int lineNumber, int column) { @@ -56,7 +61,7 @@ public DebugServerException(String detailMessage, Throwable throwable) { * @param str json string returned by the debug server * @return A DebugServerException or null if the string is not of proper form. */ - @Nullable public static DebugServerException parse(String str) { + @Nullable public static DebugServerException parse(String url, String str) { if (TextUtils.isEmpty(str)) { return null; } diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/BundleDownloader.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/BundleDownloader.java index ba798c14165ccc..e7a6fdb0cffd70 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/BundleDownloader.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/BundleDownloader.java @@ -142,10 +142,12 @@ public void onFailure(Call call, IOException e) { } mDownloadBundleFromURLCall = null; + String url = call.request().url().toString(); + callback.onFailure( - DebugServerException.makeGeneric( + DebugServerException.makeGeneric(url, "Could not connect to development server.", - "URL: " + call.request().url().toString(), + "URL: " + url, e)); } @@ -284,7 +286,7 @@ private void processBundleResult( // Check for server errors. If the server error has the expected form, fail with more info. if (statusCode != 200) { String bodyString = body.readUtf8(); - DebugServerException debugServerException = DebugServerException.parse(bodyString); + DebugServerException debugServerException = DebugServerException.parse(url, bodyString); if (debugServerException != null) { callback.onFailure(debugServerException); } else { diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java index fa5ac8503ed7b8..e36e02f4ec04e6 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java @@ -70,7 +70,7 @@ public static String getFriendlyDeviceName() { private static Integer getDevServerPort(Context context) { Resources resources = context.getResources(); - return resources.getInteger(R.integer.REACT_NATIVE_DEV_SERVER_PORT); + return resources.getInteger(R.integer.react_native_dev_server_port); } private static String getServerIpAddress(int port) { diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoModule.java index 6fb3f385d3236d..cf5ca4036c5597 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoModule.java @@ -101,7 +101,7 @@ private Boolean isRunningScreenshotTest() { private String getServerHost() { Resources resources = getReactApplicationContext().getApplicationContext().getResources(); - Integer devServerPort = resources.getInteger(R.integer.REACT_NATIVE_DEV_SERVER_PORT); + Integer devServerPort = resources.getInteger(R.integer.react_native_dev_server_port); return AndroidInfoHelpers.getServerHost(devServerPort); } diff --git a/ReactAndroid/src/main/res/systeminfo/values/values.xml b/ReactAndroid/src/main/res/systeminfo/values/values.xml index 141bc2d3ea0402..25235638f8577d 100644 --- a/ReactAndroid/src/main/res/systeminfo/values/values.xml +++ b/ReactAndroid/src/main/res/systeminfo/values/values.xml @@ -1,4 +1,4 @@ - 8081 + 8081 diff --git a/react.gradle b/react.gradle index 26fbfe3a78232e..32c50b3a04733b 100644 --- a/react.gradle +++ b/react.gradle @@ -15,14 +15,14 @@ def reactRoot = file(config.root ?: "../../") def inputExcludes = config.inputExcludes ?: ["android/**", "ios/**"] def bundleConfig = config.bundleConfig ? "${reactRoot}/${config.bundleConfig}" : null ; -def debugHostPort(String defaultValue) { - def value = project.getProperties().get("debugHostPort") +def reactNativeDevServerPort(String defaultValue) { + def value = project.getProperties().get("reactNativeDevServerPort") return value != null ? value : defaultValue } android { buildTypes.all { - resValue "integer", "REACT_NATIVE_DEV_SERVER_PORT", debugHostPort("8081") + resValue "integer", "react_native_dev_server_port", reactNativeDevServerPort("8081") } } From d3f325d626bb6722c99cb273e6823a9e6a149020 Mon Sep 17 00:00:00 2001 From: Nate Hunzaker Date: Wed, 27 Feb 2019 10:06:56 -0800 Subject: [PATCH 7/9] Update ReactNative build.gradle to use new dev server port property --- ReactAndroid/build.gradle | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ReactAndroid/build.gradle b/ReactAndroid/build.gradle index e3929680cd7e0c..3b03fb3d83d8ed 100644 --- a/ReactAndroid/build.gradle +++ b/ReactAndroid/build.gradle @@ -203,8 +203,8 @@ def findNdkBuildFullPath() { return null } -def debugHostPort(String defaultValue){ - def value = project.getProperties().get("debugHostPort") +def reactNativeDevServerPort(String defaultValue) { + def value = project.getProperties().get("reactNativeDevServerPort") return value != null ? value : defaultValue } @@ -295,7 +295,7 @@ android { buildConfigField("boolean", "IS_INTERNAL_BUILD", "false") buildConfigField("int", "EXOPACKAGE_FLAGS", "0") - resValue "integer", "react_native_dev_server_port", "8081" + resValue "integer", "react_native_dev_server_port", reactNativeDevServerPort("8081") testApplicationId("com.facebook.react.tests.gradle") testInstrumentationRunner("androidx.test.runner.AndroidJUnitRunner") From 657c2b4e49848239d41d388c48f9a9ce6fa32a56 Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 4 Jun 2019 21:14:20 -0700 Subject: [PATCH 8/9] Remove unused build config field --- .../facebook/react/modules/systeminfo/AndroidInfoHelpers.java | 1 - 1 file changed, 1 deletion(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java index e36e02f4ec04e6..f248b39ceb0343 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java @@ -25,7 +25,6 @@ public class AndroidInfoHelpers { public static final String METRO_HOST_PROP_NAME = "metro.host"; - private static final int DEBUG_SERVER_HOST_PORT = ReactBuildConfig.DEBUG_SERVER_HOST_PORT; private static final int INSPECTOR_PROXY_PORT = 8081; private static final String TAG = AndroidInfoHelpers.class.getSimpleName(); From 9aeea1e1b5c0b9a48c694b2e7a3ac7edfd975c2c Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 4 Jun 2019 22:08:13 -0700 Subject: [PATCH 9/9] Allow inspector proxy port to be configured as well, defaulting to dev port --- ReactAndroid/build.gradle | 12 +++++++++--- .../react/modules/systeminfo/AndroidInfoHelpers.java | 11 +++++++---- .../PackagerConnectionSettings.java | 2 +- .../src/main/res/systeminfo/values/values.xml | 1 + react.gradle | 12 +++++++++--- 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/ReactAndroid/build.gradle b/ReactAndroid/build.gradle index 3b03fb3d83d8ed..a1433f88207a58 100644 --- a/ReactAndroid/build.gradle +++ b/ReactAndroid/build.gradle @@ -203,9 +203,14 @@ def findNdkBuildFullPath() { return null } -def reactNativeDevServerPort(String defaultValue) { +def reactNativeDevServerPort() { def value = project.getProperties().get("reactNativeDevServerPort") - return value != null ? value : defaultValue + return value != null ? value : "8081" +} + +def reactNativeInspectorProxyPort() { + def value = project.getProperties().get("reactNativeInspectorProxyPort") + return value != null ? value : reactNativeDevServerPort() } def getNdkBuildFullPath() { @@ -295,7 +300,8 @@ android { buildConfigField("boolean", "IS_INTERNAL_BUILD", "false") buildConfigField("int", "EXOPACKAGE_FLAGS", "0") - resValue "integer", "react_native_dev_server_port", reactNativeDevServerPort("8081") + resValue "integer", "react_native_dev_server_port", reactNativeDevServerPort() + resValue "integer", "react_native_inspector_proxy_port", reactNativeInspectorProxyPort() testApplicationId("com.facebook.react.tests.gradle") testInstrumentationRunner("androidx.test.runner.AndroidJUnitRunner") diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java index f248b39ceb0343..3659abaccc0ec7 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java @@ -25,8 +25,6 @@ public class AndroidInfoHelpers { public static final String METRO_HOST_PROP_NAME = "metro.host"; - private static final int INSPECTOR_PROXY_PORT = 8081; - private static final String TAG = AndroidInfoHelpers.class.getSimpleName(); private static boolean isRunningOnGenymotion() { @@ -53,8 +51,8 @@ public static String getAdbReverseTcpCommand(Context context) { return getAdbReverseTcpCommand(getDevServerPort(context)); } - public static String getInspectorProxyHost() { - return getServerIpAddress(INSPECTOR_PROXY_PORT); + public static String getInspectorProxyHost(Context context) { + return getServerIpAddress(getInspectorProxyPort(context)); } // WARNING(festevezga): This RN helper method has been copied to another FB-only target. Any changes should be applied to both. @@ -72,6 +70,11 @@ private static Integer getDevServerPort(Context context) { return resources.getInteger(R.integer.react_native_dev_server_port); } + private static Integer getInspectorProxyPort(Context context) { + Resources resources = context.getResources(); + return resources.getInteger(R.integer.react_native_dev_server_port); + } + private static String getServerIpAddress(int port) { // Since genymotion runs in vbox it use different hostname to refer to adb host. // We detect whether app runs on genymotion and replace js bundle server hostname accordingly diff --git a/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java b/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java index 432c70d5166765..a154553f0d06b1 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java +++ b/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java @@ -54,7 +54,7 @@ public String getDebugServerHost() { } public String getInspectorServerHost() { - return AndroidInfoHelpers.getInspectorProxyHost(); + return AndroidInfoHelpers.getInspectorProxyHost(mAppContext); } public @Nullable String getPackageName() { diff --git a/ReactAndroid/src/main/res/systeminfo/values/values.xml b/ReactAndroid/src/main/res/systeminfo/values/values.xml index 25235638f8577d..7d52389be83dad 100644 --- a/ReactAndroid/src/main/res/systeminfo/values/values.xml +++ b/ReactAndroid/src/main/res/systeminfo/values/values.xml @@ -1,4 +1,5 @@ 8081 + @integer/react_native_dev_server_port diff --git a/react.gradle b/react.gradle index 32c50b3a04733b..9b45a8899c384e 100644 --- a/react.gradle +++ b/react.gradle @@ -15,14 +15,20 @@ def reactRoot = file(config.root ?: "../../") def inputExcludes = config.inputExcludes ?: ["android/**", "ios/**"] def bundleConfig = config.bundleConfig ? "${reactRoot}/${config.bundleConfig}" : null ; -def reactNativeDevServerPort(String defaultValue) { +def reactNativeDevServerPort() { def value = project.getProperties().get("reactNativeDevServerPort") - return value != null ? value : defaultValue + return value != null ? value : "8081" +} + +def reactNativeInspectorProxyPort() { + def value = project.getProperties().get("reactNativeInspectorProxyPort") + return value != null ? value : reactNativeDevServerPort() } android { buildTypes.all { - resValue "integer", "react_native_dev_server_port", reactNativeDevServerPort("8081") + resValue "integer", "react_native_dev_server_port", reactNativeDevServerPort() + resValue "integer", "react_native_inspector_proxy_port", reactNativeInspectorProxyPort() } }