Android Fix for 9145: No longer hard code build port (#23616)

Summary:
### Problem

According to https://github.com/facebook/react-native/issues/9145, the `--port` setting is not respected when executing `react-native run-android`. The templates that report things like what port the dev server runs on are hard coded as well.

### Solution

This commit replaces 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:

https://github.com/react-native-community/react-native-cli/compare/master...nhunzaker:9145-android-no-port-hardcode-cli

To avoid some noise on their end, I figured I wouldn't submit a PR until it's this approach is deemed workable.

## Changelog

[Android][fixed] - `react-native run-android --port <x>` correctly connects to dev server and related error messages display the correct port
Pull Request resolved: https://github.com/facebook/react-native/pull/23616

Differential Revision: D15645200

Pulled By: cpojer

fbshipit-source-id: 3bdfd458b8ac3ec78290736c9ed0db2e5776ed46
This commit is contained in:
Nate
2019-06-05 06:15:06 -07:00
committed by Facebook Github Bot
parent 68bca1fbd0
commit 995b4d3049
12 changed files with 119 additions and 32 deletions
@@ -7,13 +7,13 @@
package com.facebook.react.packagerconnection;
import javax.annotation.Nullable;
import android.content.Context;
import android.content.SharedPreferences;
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;
@@ -24,10 +24,12 @@ public class PackagerConnectionSettings {
private final SharedPreferences mPreferences;
private final String mPackageName;
private final Context mAppContext;
public PackagerConnectionSettings(Context applicationContext) {
mPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext);
mPackageName = applicationContext.getPackageName();
mAppContext = applicationContext;
}
public String getDebugServerHost() {
@@ -39,12 +41,12 @@ public class PackagerConnectionSettings {
return Assertions.assertNotNull(hostFromSettings);
}
String host = AndroidInfoHelpers.getServerHost();
String host = AndroidInfoHelpers.getServerHost(mAppContext);
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(mAppContext) + "' " +
"to forward the debug server's port to the device.");
}
@@ -52,7 +54,7 @@ public class PackagerConnectionSettings {
}
public String getInspectorServerHost() {
return AndroidInfoHelpers.getInspectorProxyHost();
return AndroidInfoHelpers.getInspectorProxyHost(mAppContext);
}
public @Nullable String getPackageName() {