collection of recipes how react-native-ultimate-config
can be used for
typical tasks
☝️ most of the recipes assume default template of react-native app (single target/scheme on ios and no flavors on android) unless stated otherwise
- Application name
- Bundle identifier
- Deeplink
- Using multiple schemes (ios)
- Using multiple flavors (android)
- Generate fastlane dotenv
- Override native values in js
- Declare env variable
APP_NAME=RNUC Demo
- Initialize config
yarn rnuc .env
- Configure native projects
-
open xcode
-
go to "Info" tab
-
find entry "Bundle Display Name"
-
replace it with
${APP_NAME}
-
checkout app name has changes
-
open
android/app/src/main/AndroidManifest.xml
-
find tag
application
and set attributeandroid:label
to@strings/APP_NAME
or${APP_NAME}
<manifest> ... <application ... android:label="@string/APP_NAME" </application> </manifest>
or
<manifest> ... <application ... android:label="${APP_NAME}" </application> </manifest>
- Declare env variable
BUNDLE_ID=com.awesomern.app
- Initialize config
yarn rnuc .env
- Configure native projects
-
open xcode
-
go to "Build Settings" tab
-
replace it with
${BUNDLE_ID}
- open
android/app/build.gradle
- set bundle id with data from config:
android {
...
defaultConfig {
applicationId project.config.get("BUNDLE_ID")
...
}
}
Suppose you want your app to open links with scheme "awesomeapp://"
- declare env variable
DEEPLINK_SCHEME=awesomeapp
- get familiar with official guide
- open xcode
- go to "Info" tab
- find section "URL Types" and press "+"
- in "scheme" field type
${DEEPLINK_SCHEME}
-
open
android/app/src/main/AndroidManifest.xml
-
add intent filter according to the guide and configure scheme using variable:
<activity> ... <intent-filter> ... <data android:scheme="${DEEPLINK_SCHEME}" /> </intent-filter> </activity>
️❗❗❗This recipe has experimental support and may not cover all edge cases. If your project is using multiple schemes you may still use library via cli without this recipe.
Using multiple schemes it is possible to avoid using cli tool manually when building specific environment. This is possible by defining pre-build script phase in a scheme.
-
open schemes of the project
-
ensure scheme is shared (otherwise it will not be committed)
-
go to scheme details
-
add Script "Pre-action" for "Build" action.
⚠️ make sure to select "Provide build settings from.." -
paste the following code
if [ -d "$HOME/.nvm" ]; then export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm fi RN_ROOT_DIR=$(dirname "$PROJECT_DIR") cd "$RN_ROOT_DIR" yarn run rnuc ".env.yaml" #or #npm run rnuc ".env.yaml"
-
you can now duplicate scheme per every environment you use and change name of the file that is used for
rnuc
command.
️❗❗❗This recipe has experimental support and may not cover all edge cases. If your project is using multiple flavors you may still use library via cli without this recipe. ️❗Typescript typings are not available for this setup at the moment.
Assuming you want to support multiple flavors of the app: "dev" and "staging".
-
Define flavor => env mapping in
android/app/build.gradle
project.ext.flavorEnvMapping = [ dev: "../.env.yaml", staging: "../.env.staging.yaml" ]
️️
⚠️ ️ only yaml files are supported here -
Define some flavors (or you may already have them defined)
flavorDimensions "default" productFlavors { dev{ } staging{ } }
-
Done. If you run
(cd android; ./gradlew/assembleDebug)
it will properly pick up all configs per flavor names. Whenever gradle is configuring tasks it will read env data from files and populate resources, build config and manifest placeholders from them.
-
Create rc file
touch .rnucrc.js
-
Add hook code:
const fs = require("fs"); module.exports = { on_env: async function (env) { if (fs.existsSync("./ios/fastlane")) { const writer = fs.createWriteStream("./ios/fastlane/.env"); for (const key in env) { writer.write(`${key}=${env[key]}\n`); } writer.close(); } // repeat for android }, };
Sometimes you may need to make config values generated in javascript as opposed to consuming them from native. For example if you want to benefit from fast code reload (without recompilation) with metro or to use over-the-air deploys with services like codepush.
This can be achieved with rc config: js_override
:
// rnuc.rc
module.exports = {
js_override: true,
};
In this case react-native-ultimate-config
will embed all config values
into javascript code overriding values from native.
NOTE: This feature does not apply to web projects which do not use native values
either way. See the quickstart guide for help configuring
react-native-ultimate-config
for use in a web project.