I’m experiencing an issue that I can’t get my React Native app to connect to the metro bundler after I added flavors to the app. I searched a lot for possible solutions but couldn’t make it work. This issue happens when I’m using a physical device connected through USB. USB debugging is set on and everything worked fine before I added flavors. I need to use a physical device to test some bluetooth features.
This is what I added to my gradle file:
debuggableVariants = ["localDebug"]
flavorDimensions "default"
productFlavors {
local {
resValue "string", "app_name", "Note Ninja (Debug)"
applicationIdSuffix ".debug"
versionNameSuffix "-debug"
buildConfigField "String", "FLAVOR_NAME", ""local""
}
internal {
resValue "string", "app_name", "Note Ninja (Internal Testing)"
versionNameSuffix "-internal"
buildConfigField "String", "FLAVOR_NAME", ""internal""
}
prod {
resValue "string", "app_name", "Note Ninja"
buildConfigField "String", "FLAVOR_NAME", ""prod""
}
}
Now I adjusted my npm script to the following:
"android:local": "react-native run-android --mode=localDebug --appId com.noteninja.debug",
When I run this script, metro is starting and the app is installed on the phone but I get the error message “Unable to load script. Make sure you’re either running Metro or that bundle ‘index.android.bundle’ is packaged correctly for release. When I click reload I get the error “Could not connect to development server”.
I tried to open localhost:8081 in the phone’s browser and I can access metro so it does not seem like a firewall issue. I tried adb reverse tcp:8081 tcp:8081 but that also did not help (the device is connected as it’s listed when I use adb devices). I uninstalled the app, I cleared gradle and metro cache but nothing seems to help.
Krzysztof Chabowski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
This issue is a common headache when working with React Native flavors and connecting a physical device to the Metro bundler. Let me share a possible solution that can help you resolve it. Since you have already tried adb reverse
and clearing the caches, let’s focus on a couple of other steps that might have been overlooked.
Possible Causes and Fixes
-
Bundle Mismatch for the Flavor
Theindex.android.bundle
might not be correctly associated with your flavor. Ensure you have not accidentally created a production-style build for your custom flavors. Check yourgradle.properties
or build commands to ensuredebuggableVariants
includes the correct variant (localDebug
).-
Add this in your
build.gradle
:debuggable true applicationVariants.all { variant -> if (variant.buildType.name == "debug" && variant.flavorName == "local") { variant.outputs.all { outputFileName = "index.android.bundle" } } }
-
-
Ensure Flavors Are Passed Correctly to Metro
Metro bundler might not know which flavor is being used. Try explicitly specifying the flavor and mode when starting Metro.Modify your
package.json
to add:"start:local": "react-native start --reset-cache"
Then, run Metro before installing the app:
npm run start:local
-
React Native Flavors Configuration
Double-check yoursettings.gradle
andbuild.gradle
files. Ensure that yourlocalDebug
flavor is correctly mapped and resolves to the right build variant. Sometimes, mismatched flavor names can cause issues. -
Device Connectivity
Run the following commands again to ensure proper connection:adb reverse tcp:8081 tcp:8081 adb shell input keyevent 82 # Ensures the device stays awake
Also, check the
ipconfig
orifconfig
on your system and confirm that Metro is serving on the expected IP address (default islocalhost:8081
). -
Enable Cleartext Traffic for Debug Flavors
Add this to yourAndroidManifest.xml
for thelocalDebug
flavor:<application android:usesCleartextTraffic="true" />
-
Check App Installation Path
Sometimes flavors generate different application IDs that do not match what the Metro bundler expects. Verify that yourcom.noteninja.debug
is correctly being resolved. -
Run Metro with Specific Configurations
Sometimes, Metro bundler needs explicit instructions. Run Metro like this:npx react-native start --reset-cache --port 8081
-
Rebuild Gradle
If none of the above works, try a full rebuild:cd android ./gradlew clean cd .. npm run android:local
Additional Debugging
- Ensure the device is connected properly by checking
adb devices
. - Use
adb logcat
to check logs for more specific errors. - Open
localhost:8081/debugger-ui
on your browser and ensure Metro is running.
By following these steps, you should be able to resolve the issue. If the problem persists, try sharing the full logs for deeper analysis.
1