From aa71dbf1161d538f0208cf2d56ac3fa06dcd858d Mon Sep 17 00:00:00 2001 From: Kudo Chien Date: Fri, 16 Sep 2022 16:29:46 +0800 Subject: [PATCH] [dev-launcher] Fix port -1 error when no port in bundle url (#19136) # Why when load js bundle from a URL without an explicit port, e.g. http://www.example.com/ , the app will crash. the root cause is that `android.net.Uri.getPort()` will return -1 when there's no port. related https://github.com/facebook/react-native/pull/34705 and ENG-5932 # How use `okhttp3.HttpUrl.defaultPort(scheme)` as fallback when port is -1. since the parameter in `injectReactInterceptor` is already android.net.Uri, i don't want to use HttpUrl to parse it again. only change the default port would be lower risk. # Test Plan test on bare-expo 1. [enable dev-launcher](https://github.com/expo/expo/blob/7331e13192c5b798ec67ec180949e1ef4e4da228/apps/bare-expo/android/app/src/main/java/dev/expo/payments/MainApplication.java#L25) 2. `EXPO_NO_DEFAULT_PORT=1 npx expo start --tunnel` 3. in dev-launcher, load the bundle url, e.g. `exp://nquwmbi.kudochien.19000.exp.direct` --- packages/expo-dev-launcher/CHANGELOG.md | 1 + .../expo/modules/devlauncher/helpers/DevLauncherReactUtils.kt | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/expo-dev-launcher/CHANGELOG.md b/packages/expo-dev-launcher/CHANGELOG.md index 2969634c14c19..bdb3fa99bac17 100644 --- a/packages/expo-dev-launcher/CHANGELOG.md +++ b/packages/expo-dev-launcher/CHANGELOG.md @@ -15,6 +15,7 @@ - Remove the deprecated `Linking.removeEventListener` in expo-dev-launcher bundle. ([#18939](https://github.com/expo/expo/pull/18939) by [@kudo](https://github.com/kudo)) - Fixed the incompatibility with react-native-v8 on Android. ([#19117](https://github.com/expo/expo/pull/19117) by [@kudo](https://github.com/kudo)) +- Fixed crash when loading bundle without explicit port on Android. ([#19136](https://github.com/expo/expo/pull/19136) by [@kudo](https://github.com/kudo)) ### 💡 Others diff --git a/packages/expo-dev-launcher/android/src/main/java/expo/modules/devlauncher/helpers/DevLauncherReactUtils.kt b/packages/expo-dev-launcher/android/src/main/java/expo/modules/devlauncher/helpers/DevLauncherReactUtils.kt index c4b61415833cc..2a36d889fb32f 100644 --- a/packages/expo-dev-launcher/android/src/main/java/expo/modules/devlauncher/helpers/DevLauncherReactUtils.kt +++ b/packages/expo-dev-launcher/android/src/main/java/expo/modules/devlauncher/helpers/DevLauncherReactUtils.kt @@ -9,13 +9,15 @@ import com.facebook.react.bridge.JSBundleLoader import expo.interfaces.devmenu.annotations.ContainsDevMenuExtension import expo.modules.devlauncher.react.DevLauncherDevSupportManagerSwapper import expo.modules.devlauncher.react.DevLauncherInternalSettings +import okhttp3.HttpUrl fun injectReactInterceptor( context: Context, reactNativeHost: ReactNativeHost, url: Uri ): Boolean { - val debugServerHost = url.host + ":" + url.port + val port = if (url.port != -1) url.port else HttpUrl.defaultPort(url.scheme) + val debugServerHost = url.host + ":" + port // We need to remove "/" which is added to begin of the path by the Uri // and the bundle type val appBundleName = if (url.path.isNullOrEmpty()) {