This document covers basic setup assuming default react-native's template with single scheme/target on ios and default flavor on android.
For advanced setup please refer to cookbook
-
Add dependency
npm yarn npm install react-native-ultimate-config
yarn add react-native-ultimate-config
-
Create example file
echo "MY_CONFIG=hello" > .env
-
Update .gitignore
Add this to
.gitignore
:# react-native-ultimate-config rnuc.xcconfig
-
ONLY FOR NPM. SKIP IF USING YARN
create script in "package.json"
"rnuc": "$(npm bin)/rnuc"
-
Generate files
generate env files for native projects
npm yarn npm run rnuc .env
yarn rnuc .env
-
Configure native projects (one-off setup)
-
ios
-
-
apply plugin in gradle Add this right after applying react's plugin:
apply from: "../../node_modules/react-native-ultimate-config/android/rnuc.gradle"
Final code:
apply from: "../../node_modules/react-native/react.gradle" apply from: "../../node_modules/react-native-ultimate-config/android/rnuc.gradle"
-
expose
BuildConfig
to the libraryin
MainApplication.java
add// import module import com.reactnativeultimateconfig.UltimateConfigModule; ... @Override public void onCreate() { super.onCreate(); ... UltimateConfigModule.setBuildConfig(BuildConfig.class); // expose }
-
If you are using ProGuard: in
proguard-rules.pro
add the following snippet replacingMY_PACKAGE
with package identifier of yours, e.g.com.mypackage.BuildConfig
.❗keep
<fields>
as is. it SHALL NOT be replaced with actual field names-keepclassmembers class MY_PACKAGE.BuildConfig { public static <fields>; }
-
-
-
save changes made to native projects
.xcodeproj
file andbuild.gradle
. DO NOT COMMMITrnuc.*
files. -
Configure web projects (optional, one-off setup)
You can import your config into a web project as well (e.g. a project using React Native for Web).
react-native-ultimate-config
vends a web-friendly CommonJS module using the package.json browser field. Most web bundlers support this with minimal effort:- Webpack - No configuration needed. If you choose to set target it must include
"web"
. - Rollup - Install @rollup/plugin-node-resolve and set
browser
totrue
. - Parcel - No configuration needed.
- Browserify - No configuration needed.
- Webpack - No configuration needed. If you choose to set target it must include
-
from now on every time you need to switch environment just run
npm run rnuc <dotenv file>
oryarn rnuc <dotenv file>
and rerun your native project (withreact-native run-{ios,android}
) or web project (with your web bundler of choice)