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 ?

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.