androidreact-nativeandroid-productflavorsmetro-bundler

React Native - Can't connect Metro Bundler to Android AVD when using variants


I created some variants for my RN app in Android. In app/build.gradle:

android {
    ...

    namespace "com.myapp"

    flavorDimensions += "default"
    productFlavors {
        create("production") {
            dimension "default"
            resValue "string", "build_config_package", "com.myapp"
            ...
        }
        create("staging") {
            dimension "default" 
            applicationIdSuffix ".staging"
            resValue "string", "build_config_package", "com.myapp"
            ...
        }
        create("development") {
            dimension "default" 
            applicationIdSuffix ".development" 
            resValue "string", "build_config_package", "com.myapp"
            ...
        }
    }

    defaultConfig {
        applicationId "com.myapp"
        ...
    }

    buildTypes {
        debug {
            ...
        }
        release {
            ...
        }
    }
}

In package.json:

"scripts": {
    "android:staging": "react-native run-android --mode=stagingDebug --appIdSuffix \"staging\"",
    "android:staging-release": "react-native run-android --mode=stagingRelease --appIdSuffix \"staging\"",
    "android:dev": "react-native run-android --mode=developmentDebug --appIdSuffix \"development\"",
    "android:dev-release": "react-native run-android --mode=developmentRelease --appIdSuffix \"development\"",
    "android:prod": "react-native run-android --mode=productionDebug",
    "android:prod-release": "react-native run-android --mode=productionRelease",
    "android": "react-native run-android",
   ...
  },

When i run, for example, yarn android:dev, Metro Bundler and AVD will start, Metro will build, install and launch the app in emulator, but then it will close and disconnect from the app (i can't debug/fast refresh) without a message:

BUILD SUCCESSFUL in 16s
491 actionable tasks: 19 executed, 472 up-to-date
info Connecting to the development server...
info Starting the app on "emulator-5554"...
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.myapp.development/com.myapp.MainActivity }
✨  Done in 26.22s.

(I think this package name com.myapp.development/com.myapp.MainActivity is looking weird, could it be related to the problem somehow?)

If i refresh the app from Bundler, it will say:

info Reloading app...
warn No apps connected. Sending "reload" to all React Native apps failed. Make sure your app is running in the simulator or on a phone connected via USB.

I've already tried adb reverse 8081:8081, clear gradle cache and android:usesCleartextTraffic="true". Nothing works for Android. On iOS emulator Metro Bundler stays connected.

I'm running react-native version: 0.72.3.

Do you guys know what else can it be?


Solution

  • Welp, after a struggle of try/errors, i found out that the problem was the AVD SDK version.

    I was using one with SDK 35, but seems that Metro Bundler of React Native 0.72.x connects only in AVD with SDK <= 33.