因8081端口被占用,如何构建指向8087端口Metro Bundler的Android Debug版本
Got it, let's fix that port conflict issue from your antivirus blocking 8081. Here's a step-by-step guide to get your Detox Android Debug build connected to Metro running on 8087:
1. Set Metro to Use Port 8087
First, update your Metro config to listen on the new port. Open or create metro.config.js in your project root and add the server port setting:
module.exports = { server: { port: 8087, }, // Keep the rest of your existing Metro config here };
2. Tell Detox About the New Metro Port
Next, update your Detox configuration so it knows to connect to Metro on 8087. Open detox.config.js (or your package.json if Detox config lives there) and adjust the Android configuration:
module.exports = { configurations: { android: { // Your existing Android config (binaryPath, testBinaryPath, etc.) metroPort: 8087, // Add this line to specify the port }, }, };
3. Configure Android App to Connect to 8087
Your Android app needs to know to reach Metro on the new port during debug. The easiest way is to update the string resource:
Navigate to
android/app/src/main/res/values/strings.xmlAdd or modify the
debug_http_hostline:<string name="debug_http_host" translatable="false">localhost:8087</string>If you prefer modifying the Java code instead, open
android/app/src/main/java/com/[your-app-package]/MainApplication.javaand check thegetUseDeveloperSupportmethod. Ensure theReactInstanceManageris initialized with the correct port (though the string resource method is simpler and recommended).
4. Start Metro on Port 8087
Before running your Detox build, launch Metro with the specified port:
npx react-native start --port 8087
5. Build and Run Detox Tests
Now you can build your Android Debug build and run tests—Detox will connect to Metro on 8087 instead of 8081:
detox build -c android.debug detox test -c android.debug
That should resolve the port conflict and get your Detox build working smoothly.
内容的提问来源于stack exchange,提问作者Timothy Doolan

