mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: I've implemented a feature that automatically bundles the Metro Bundler's IP address into Android builds. This change aligns the Android development experience with iOS, allowing the app to maintain a connection to the Metro Bundler even when disconnected from USB. Currently, in iOS builds, the IP address of the computer running the Metro Bundler is automatically bundled into the app, ensuring seamless connectivity even when the device is disconnected from USB. In contrast, Android developers must manually input the IP address if the USB connection is lost, which can be tedious and error-prone. More info in discussion thread: https://github.com/react-native-community/discussions-and-proposals/issues/870 I anticipate that a change where making IP the default method of connection will result in a lot of people running into issues where they can't connect to Metro server (for example, if they're on a different network, or they disable wifi). So I also changed the default error message you get in case the app can't connect to the bundler and updated the "Change Bundle Location" dev menu. The previous error message ``` Unable to load script. Make sure you're either running Metro (run 'npx react-native start') or that your bundle 'RNTesterApp.android.bundle' is packaged correctly for release. ``` was changed to: ``` Unable to load script. Make sure you're running Metro (npx react-native start) or that your bundle 'RNTesterApp.android.bundle' is packaged correctly for release. The device must be on the same WiFi as your laptop to connect to Metro. To use USB instead, shake the device to open the dev menu and set the bundler location to 'localhost: 8081' and run: adb reverse tcp:8081 tcp:8081 ```  And the new dev menu UI looks like this:  The two buttons with "10.0.2.2:8081" and "localhost:8081" are suggestions which when tapped fill the input with the text from the button. The first button suggests the IP of the development machine, and the second one is hardcoded to localhost:8081. ## Changelog: [ANDROID] [CHANGED] - Automatically use Metro bundler IP address when installing apps on Android Pull Request resolved: https://github.com/facebook/react-native/pull/49166 Test Plan: I've tested the implementation on a physical device and on emulator and it's working solid. However, I would invite further testing in order to catch possible edge cases. I've recorded common scenarios Scenario 1: Device doesn't have the app installed. We connect the device via USB, install the app and open it. Device is on the same network as the dev machine. Bundler location is by default set to the IP of the dev machine. When starting app, the app is able to connect to the dev machine and download the bundle. Scenario 2: Device doesn't have the app installed. Wi-Fi is turned off on the device but device is connected via USB We install the app and open it. Bundler location is by default set to the IP of the dev machine. When starting app, the app is not able to connect to the dev machine and shows the error message. After opening the dev menu we see that the IP is set to the IP of the dev machine. We click the "localhost" option in the dev menu and click apply After that the app is able to connect to the dev machine and download the bundle (via USB) since the traffic is forwarded using adb reverse. Notes: When we set an IP in the dev menu, the app will persist it. If we connect the device via USB and reinstall the app the persisted data stays the same, so the previously set IP will be used. However, the IP of the dev machine will be displayed as an option in the dev menu. https://github.com/user-attachments/assets/cc2da5d4-de07-4980-a61c-68ca53db74c7 https://github.com/user-attachments/assets/407b8871-8b83-4a6b-a833-f87ddc0afc82 Reviewed By: huntie Differential Revision: D69664231 Pulled By: cortinico fbshipit-source-id: 5a339be50a17a59202416b99e72f4397d8ff4805
@react-native/gradle-plugin
A Gradle Plugin used to support development of React Native applications for Android.
Installation
yarn add @react-native/gradle-plugin
Note: We're using yarn to install deps. Feel free to change commands to use npm 3+ and npx if you like
Testing
To run the tests in this package, run the following commands from the React Native root folder:
yarnto install the dependencies. You just need to run this once./gradlew -p packages/gradle-plugin test.