Skip to content

Commit 0ea16fd

Browse files
kmagierafacebook-github-bot
authored andcommitted
Unify codepath for retrieveing metro location on Android (#42617)
Summary: With the current ways metro location is determined, when we want to use a different metro port this requires app to be rebuild as the port and location are stored in resource file that gets compiled to R.class. The only way to avoid app rebuild due to a port change is to use shared preferences that can be accessed from dev menu, where metro URL can be specified. However, due to a separate code-paths for retrieving bundle location and for `/inspector/device` calls, the setting only applies to the former. As a consequence, you can change metro URL in the shared preferences, but debugging would only work if you use the default port or you rebuild the app with the correct port number. This PR removes the separate code-path for retrieving inspector URL including all the dependencies scattered across different files including the gradle plugin. We then replace calls to `PackagerConnectionSettings.getInspectorServerHost` with `PackagerConnectionSettings.getDebugServerHost` which respects the shared preferences and other possible ways of configuring the port. I decided to remove the separate inspector URL code path, as the resource value for inspector port added in #23616 was never functioning properly due to a bug. In the said PR introduced a bug in [AndroidInfoHelpers.java](https://github.com/facebook/react-native/blob/a13d51ff1c38ea85e59f4215563c0dd05452f670/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java#L77) where `react_native_dev_server_port` was used instead `react_native_inspector_proxy_port`. As a result the added resource value was never read. This can be potentially a breaking change as I'm removing some public methods. However I think it is unlikely anyone relied on said methods. As a part of this PR I'm also changing occurences of removed methods from ReactAndroid.api – I don't know how to test those changes since I don't understand how this file is used as it doesn't have any references in public code. ## Changelog: [ANDROID] [FIXED] - Make Android respect metro location from shared preferences for the debugger workflow Pull Request resolved: #42617 Test Plan: 1. Run android app on emulator using default port 2. Check the debugger works when using "Open Debugger" option from dev menu 3. Restart metro with custom port (`--port 9090`) while keeping the app running 4. Open dev menu, click "Settings" then "Debug server host & port", put "10.0.2.2:9090" there 5. Reload the app 6. Before this change things like hot reload would continue to work while "Open Debugger" option would do nothing 7. After this change both reloading and debugging will work Important: I haven't tested changes made to ReactAndroid.api as I don't know what this files is used for with no references in the codebase. Reviewed By: cortinico Differential Revision: D53010023 Pulled By: huntie fbshipit-source-id: cc8b9c5c7e834ec9ea02b1ed5acf94f04f7b7116
1 parent 631b6a1 commit 0ea16fd

File tree

6 files changed

+2
-27
lines changed

6 files changed

+2
-27
lines changed

packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/AgpConfiguratorUtils.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,11 @@ internal object AgpConfiguratorUtils {
5353
fun configureDevPorts(project: Project) {
5454
val devServerPort =
5555
project.properties["reactNativeDevServerPort"]?.toString() ?: DEFAULT_DEV_SERVER_PORT
56-
val inspectorProxyPort =
57-
project.properties["reactNativeInspectorProxyPort"]?.toString() ?: devServerPort
5856

5957
val action =
6058
Action<AppliedPlugin> {
6159
project.extensions.getByType(AndroidComponentsExtension::class.java).finalizeDsl { ext ->
6260
ext.defaultConfig.resValue("integer", "react_native_dev_server_port", devServerPort)
63-
ext.defaultConfig.resValue(
64-
"integer", "react_native_inspector_proxy_port", inspectorProxyPort)
6561
}
6662
}
6763

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3452,7 +3452,6 @@ public class com/facebook/react/modules/systeminfo/AndroidInfoHelpers {
34523452
public static fun getAdbReverseTcpCommand (Landroid/content/Context;)Ljava/lang/String;
34533453
public static fun getAdbReverseTcpCommand (Ljava/lang/Integer;)Ljava/lang/String;
34543454
public static fun getFriendlyDeviceName ()Ljava/lang/String;
3455-
public static fun getInspectorProxyHost (Landroid/content/Context;)Ljava/lang/String;
34563455
public static fun getServerHost (Landroid/content/Context;)Ljava/lang/String;
34573456
public static fun getServerHost (Ljava/lang/Integer;)Ljava/lang/String;
34583457
}
@@ -3528,7 +3527,6 @@ public abstract class com/facebook/react/packagerconnection/NotificationOnlyHand
35283527
public class com/facebook/react/packagerconnection/PackagerConnectionSettings {
35293528
public fun <init> (Landroid/content/Context;)V
35303529
public fun getDebugServerHost ()Ljava/lang/String;
3531-
public fun getInspectorServerHost ()Ljava/lang/String;
35323530
public fun getPackageName ()Ljava/lang/String;
35333531
public fun setDebugServerHost (Ljava/lang/String;)V
35343532
}

packages/react-native/ReactAndroid/build.gradle.kts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -429,11 +429,6 @@ fun reactNativeDevServerPort(): String {
429429
return value?.toString() ?: "8081"
430430
}
431431

432-
fun reactNativeInspectorProxyPort(): String {
433-
val value = project.properties["reactNativeInspectorProxyPort"]
434-
return value?.toString() ?: reactNativeDevServerPort()
435-
}
436-
437432
fun reactNativeArchitectures(): List<String> {
438433
val value = project.properties["reactNativeArchitectures"]
439434
return value?.toString()?.split(",") ?: listOf("armeabi-v7a", "x86", "x86_64", "arm64-v8a")
@@ -498,7 +493,6 @@ android {
498493
buildConfigField("int", "EXOPACKAGE_FLAGS", "0")
499494

500495
resValue("integer", "react_native_dev_server_port", reactNativeDevServerPort())
501-
resValue("integer", "react_native_inspector_proxy_port", reactNativeInspectorProxyPort())
502496

503497
testApplicationId = "com.facebook.react.tests.gradle"
504498
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ private String getInspectorDeviceUrl() {
312312
return String.format(
313313
Locale.US,
314314
"http://%s/inspector/device?name=%s&app=%s&device=%s",
315-
mPackagerConnectionSettings.getInspectorServerHost(),
315+
mPackagerConnectionSettings.getDebugServerHost(),
316316
Uri.encode(AndroidInfoHelpers.getFriendlyDeviceName()),
317317
Uri.encode(mPackageName),
318318
Uri.encode(getInspectorDeviceId()));
@@ -491,7 +491,7 @@ public void openDebugger(final ReactContext context, final String errorMessage)
491491
String.format(
492492
Locale.US,
493493
"http://%s/open-debugger?appId=%s&device=%s",
494-
mPackagerConnectionSettings.getInspectorServerHost(),
494+
mPackagerConnectionSettings.getDebugServerHost(),
495495
Uri.encode(mPackageName),
496496
Uri.encode(getInspectorDeviceId()));
497497
Request request =

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ public static String getAdbReverseTcpCommand(Context context) {
5252
return getAdbReverseTcpCommand(getDevServerPort(context));
5353
}
5454

55-
public static String getInspectorProxyHost(Context context) {
56-
return getServerIpAddress(getInspectorProxyPort(context));
57-
}
58-
5955
// WARNING(festevezga): This RN helper method has been copied to another FB-only target. Any
6056
// changes should be applied to both.
6157
public static String getFriendlyDeviceName() {
@@ -72,11 +68,6 @@ private static Integer getDevServerPort(Context context) {
7268
return resources.getInteger(R.integer.react_native_dev_server_port);
7369
}
7470

75-
private static Integer getInspectorProxyPort(Context context) {
76-
Resources resources = context.getResources();
77-
return resources.getInteger(R.integer.react_native_dev_server_port);
78-
}
79-
8071
private static String getServerIpAddress(int port) {
8172
// Since genymotion runs in vbox it use different hostname to refer to adb host.
8273
// We detect whether app runs on genymotion and replace js bundle server hostname accordingly

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ public void setDebugServerHost(String host) {
5757
mPreferences.edit().putString(PREFS_DEBUG_SERVER_HOST_KEY, host).apply();
5858
}
5959

60-
public String getInspectorServerHost() {
61-
return AndroidInfoHelpers.getInspectorProxyHost(mAppContext);
62-
}
63-
6460
public @Nullable String getPackageName() {
6561
return mPackageName;
6662
}

0 commit comments

Comments
 (0)