0

I have configured ENVs using react-native-config package. I have 3 different ENVs which I have defined in app/build.gradle

This is defined as below -

project.ext.envConfigFiles = [
    debug: ".env.dev",
    stagingRelease: ".env.staging",
    prodRelease: ".env.prod",
]

Now when I run npx react-native run-android --variant=prodRelease. It is breaking with error - Task 'installProdRelease' not found in project ':app'.

How can I make it working so that I can run different environments in app ?

Anuj Raghuvanshi
  • 664
  • 1
  • 7
  • 24

1 Answers1

0

The value that is passed to --variant option is basically a combination of product flavor and build type. By default there are 2 build types defined - debug and release. To add flavors like development, staging, production, define them below buildTypes in android/app/build.gradle -

    buildTypes {
        .....
    }

    flavorDimensions "default"
    productFlavors {
        production {}
        staging {}
        development {}
    }

Now with a configuration like this -

project.ext.envConfigFiles = [
    developmentdebug: ".env.development",
    developmentrelease: ".env.development",
    stagingdebug: ".env.staging",
    stagingrelease: ".env.staging",
    productiondebug: ".env.production",
    productionrelease: ".env.production",
]

apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"

you can now build like you wanted -

npx react-native run-android --variant=stagingdebug
npx react-native run-android --variant=productionrelease

You can read more about Android build types and flavors here.
https://developer.android.com/studio/build/build-variants

For a similar configuration in iOS, follow this excellent tutorial.
https://www.bigbinary.com/books/learn-react-native/handling-environment-specific-configurations-in-react-native

vinayr
  • 11,026
  • 3
  • 46
  • 42