yarn add react-native-config
react-native link react-native-config
- Create
.env
files for each configuration. Ex:.env.dev
,.env.prod
, etc Ex:
API_ENDPOINT=https://api.myresource.com/dev
ENV=dev
- Add
resValue "string", "build_config_package", "PACKAGE_NAME_FROM_ANDROIDMANIFEST.XML"
todefaultConfig
inandroid/app/build.gradle
Note: The targets will have suffixes corresponding to the target. Ex: prod: com.company.app
, dev: com.company.app.development
- Add
productFlavors
for different desired targets
android {
defaultConfig { //... }
productFlavors {
dev {
// Pre-compile run ENVFILE=.env.dev
applicationIdSuffix ".development"
}
prod {
// Pre-compile run ENVFILE=.env.prod
}
}
}
- Add target dependent keys in
src/<flavor>/res/values/strings.xml
(main
is the default config. Use this for prod configuration). Ex:src/dev/res/values/strings.xml
,src/main/res/values/strings.xml
- Since
react-native-config
requiresENVFILE
to be defined in the environment during compile time, make sure to export the variable before assembing. I usually add these scripts to mypackage.json
"scripts": {
// ...
"install:android": "cd android/ && export ENVFILE=.env.dev && ./gradlew installDevDebug && cd .. && yarn start:android",
"start:android": "adb shell am start -n com.bundle.id/.MainActivity",
"assemble:android": "cd android/ && export ENVFILE=.env.prod && ./gradlew assembleProdRelease && cd ..",
"assemble:android:dev": "cd android/ && export ENVFILE=.env.dev && ./gradlew assembleDevRelease && cd ..",
}
- Run
yarn assemble:android
to build your prod app andyarn assemble:android:dev
for your dev app.
- Instead of multiple schemes for the same target, we are going to add a target for each pipeline. (This will allow for multiple bundle ids, and installing the different target apps to iOS at the same time.)
- Right click your primary target > Duplicate > Rename to
<TargetName>Dev
- Move the newly created
<TargetName>Dev-Info.plist
into the project directory ><TargetName>Dev
Build Settings > Search "plist" and fix references to plist - General > Set appropriate display name > Set bundle id (same as primary target, append
.<pipeline>
ex:.development
) - Schemes > Manage Schemes > Delete new scheme that was created for target > Duplicate primary scheme > Rename
<TargetName>Dev
- Edit new scheme > Profile > Make sure Executable is set to the new target
.app
executable - Make sure to follow the Pre-Actions setup and extra instructions for iOS
- Select appropriate scheme before building or running on device
- To keep versions and build in sync across all pipelines, I usually add these scripts into my
package.json
"scripts": {
// ...
"bump:build:ios": "cd ios/ && agvtool next-version -all && cd ..",
"set:build:ios": "cd ios/ && agvtool new-version -all",
"set:version:ios": "cd ios/ && agvtool new-marketing-version"
}
To run: yarn bump:build:ios
, yarn set:build:ios 23
, yarn set:version:ios 1.0.1
- Makes sure to have the correct bundle ids in Firebase to test push notifications in dev before production.
- When linking libraries for iOS, the
react-native link
command will be applied to the primary target, and NOT your pipeline targets. These will need to be manually linked.
This is great, thanks a lot!