Skip to content

Commit

Permalink
Allow debug dev server port override
Browse files Browse the repository at this point in the history
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:
facebook#9145
  • Loading branch information
nhunzaker committed Feb 24, 2019
1 parent 0b4f741 commit 30b207c
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 4 deletions.
7 changes: 7 additions & 0 deletions ReactAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -286,6 +291,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("android.support.test.runner.AndroidJUnitRunner")
}
Expand Down
1 change: 1 addition & 0 deletions ReactAndroid/src/main/java/com/facebook/react/common/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ rn_android_build_config(
package = "com.facebook.react",
values = [
"boolean IS_INTERNAL_BUILD = true",
"int DEBUG_SERVER_HOST_PORT = 8081",
],
visibility = [
"PUBLIC",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.os.Build;

import com.facebook.common.logging.FLog;
import com.facebook.react.common.build.ReactBuildConfig;

public class AndroidInfoHelpers {

Expand All @@ -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 = 8082;

private static final String TAG = AndroidInfoHelpers.class.getSimpleName();
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}

Expand Down

0 comments on commit 30b207c

Please sign in to comment.