How can I connect to metro bundler after adding flavors?

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.

New contributor

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

  1. Bundle Mismatch for the Flavor
    The index.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 your gradle.properties or build commands to ensure debuggableVariants 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"
              }
          }
      }
      
  2. 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
    
  3. React Native Flavors Configuration
    Double-check your settings.gradle and build.gradle files. Ensure that your localDebug flavor is correctly mapped and resolves to the right build variant. Sometimes, mismatched flavor names can cause issues.

  4. 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 or ifconfig on your system and confirm that Metro is serving on the expected IP address (default is localhost:8081).

  5. Enable Cleartext Traffic for Debug Flavors
    Add this to your AndroidManifest.xml for the localDebug flavor:

    <application android:usesCleartextTraffic="true" />
    
  6. Check App Installation Path
    Sometimes flavors generate different application IDs that do not match what the Metro bundler expects. Verify that your com.noteninja.debug is correctly being resolved.

  7. Run Metro with Specific Configurations
    Sometimes, Metro bundler needs explicit instructions. Run Metro like this:

    npx react-native start --reset-cache --port 8081
    
  8. 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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật