Introduction: -
The environment-related builds can be used to use different base URLs for the network requests (test data in the Staging environment app build, production data in the Prod environment app build). As an example, In the first step, we will create an app called envSetup with Staging and Prod environment-based variants, and set a unique package name/bundle id, app name, and app icon for them, which will enable the installation of different app versions on the same device (each app's package name or bundle id must be unique in Android or iOS).We'll add React Native Config as a dependency in the following step. This library's main goal is to make it easier for us to store environment/flavor-related constants in straightforward.env files and read their values in our Javascript coding. Injection of the particular.env files into the apps is also possible during CI-to-build processes.In the end, we’ll have the following 4 app variants for both platforms (Android/IOS) with different package names/bundle id, app names, and app icon- envSetup-Staging, envSetup-Prod as envSetup in Ios- Staging, Prod in AndroidHow to setup env in react native app:-- let’s initialize the React Native app using the React Native CLI:npx react-native init envSetupnpm i react-native-configcd ios && pod install.env
.env.prod
.env.staging

In Android:-
After the project has opened, we should look for the applicationId key in the android/app/build.gradle file under the defaultConfig block to check our initial package name on Android:android {
defaultConfig {
applicationId "com.envSetup"
..
}
}android {
buildTypes {
debug {
...
}
release {
...
}
}
flavorDimensions "default"
productFlavors {
prod {
minSdkVersion rootProject.ext.minSdkVersion
applicationId 'com.envsetup'
targetSdkVersion rootProject.ext.targetSdkVersion
resValue "string", "build_config_package", "com.envsetup"
}
staging {
minSdkVersion rootProject.ext.minSdkVersion
applicationId 'com.envsetup.staging'
targetSdkVersion rootProject.ext.targetSdkVersion
resValue "string", "build_config_package", "com.envsetup"
}
}
}apply plugin: "com.android.application"
import com.android.build.OutputFile
import org.apache.tools.ant.taskdefs.condition.Os
project.ext.envConfigFiles = [
prodDebug: ".env.prod",
prodRelease: ".env.prod",
stagingRelease: ".env.staging",
stagingDebug: ".env.staging"
]
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle""scripts": {
"android:prod": "react-native run-android --variant=prodDebug --appId
com.envsetup",
"android:prod-release": "react-native run-android --variant=prodRelease --appId
com.envsetup",
"android:staging-release": "react-native run-android --variant=stagingRelease
--appId com.envsetup.staging",
"android:staging": "react-native run-android --variant=stagingDebug --appId
com.envsetup.staging",
}In Ios: -
Open the project(envSetup.xcworkspace) in Xcode, In the Project sidebar (on the left), select the envSetup project.ios/envSetup.xcworkspace 








echo ".env.prod" > /tmp/envfile




echo ".env.staging" > /tmp/envfile


cd ios & pod install"scripts": {
"ios:prod": "react-native run-ios --scheme 'envSetup'",
"ios:staging": "react-native run-ios --scheme 'envSetup-staging'"
}npm run ios:prodnpm run ios:stagingDebug
npm run android:prodnpm run android:prod-releaseDebug
npm run android:stagingnpm run android:staging-release

