androidreact-nativestagingreact-native-config

react native staging in android


I am trying to get flavors or build variants running in android for my react-native project.

Via Schemes I got the same running on iOS, but Android does not want to follow. I guess something I am doing wrong.

Steps that I took so far (all in a fresh project to test):

  1. npm install react-native-config
  2. created 3 files .env, .env.staging and .env.production
  3. Added the following into the android/app/build.gradle:
 project.ext.envConfigFiles = [
  staging: ".env.staging",
  production: ".env.production",
]

apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
  1. Added the flavours into the same file (suffix is commented out to not change the name -> hope that is not the problem):
flavorDimensions "appType"
        productFlavors {
        staging {
            dimension "appType"
            applicationIdSuffix ".staging"
            // resValue "string", "app_name", "Config Demo-Staging"
        }
        production {
            dimension "appType"
            applicationIdSuffix ".production"
            // resValue "string", "app_name", "Config Demo"
        }
        }
  1. Added the following scripts in the package.json:
"scripts": {
    "androidStagingDebug": "react-native run-android --variant=stagingDebug",
    "androidProductionDebug": "react-native run-android --variant=productionDebug",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },

Now I want to start the app from Android Studio and just check via:

import config from 'react-native-config';

and respectively in a later function:

console.log(config)

whether I read the right file when choosing a build variant.

Unfortunately it either does not load any or only debug. Any idea?! Additionally in some variants it seems the app does not connect to metro..


Solution

  • Your build.gradle file seems ok.

    Mine is slightly different though :

    project.ext.envConfigFiles = [
        prod: "./environment/.prod",
        beta: "./environment/.beta",
        alpha: "./environment/.alpha",
    ]
    

    Then :

    flavorDimensions "default"
    
      productFlavors {
        prod {
            applicationIdSuffix ".prod"
        }
        beta {
            applicationIdSuffix ".beta"
        }
        alpha {
            applicationIdSuffix ".alpha"
        }
    }
    

    Like I said in a comment earlier, I don't use any script to launch the app, I just select the build variant in Android Studio.

    Here's the resource I used:

    https://medium.com/swlh/setting-up-multiple-environments-on-react-native-for-ios-and-android-c43f3128754f

    And here's an example project:

    https://github.com/therealemjy/react-native-tuto-multiple-environments/tree/result

    Hope it helps a bit.