Module to expose build config variables set in Gradle to your javascript code in React Native.
iOS version greatly contributed by maddijoyce
npm i react-native-build-configDeclare config variables in Gradle, under android/app/build.gradle:
android {
defaultConfig {
buildConfigField "String", "API_URL", '"https://myapi.com"'
buildConfigField "Boolean", "SHOW_ERRORS", "true"
...
Or declare them in your info.plist file in your ios project.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>API_URL</key>
<string>https://myapi.com</string>
<key>SHOW_ERRORS</key>
<true />
...
Then access those from javascript:
const BuildConfig = require('react-native-build-config')
BuildConfig.API_URL // "https://myapi.com"
BuildConfig.SHOW_ERRORS // trueGradle sets some variables by default:
VERSION_NAMEandVERSION_CODE, both coming from the build settings. Keep in mind the code is a numberAPPLICATION_ID: Your package name, eg:com.ExampleDEBUG: set totruewhen running the app locallyBUILD_TYPEandFLAVOR: more build settings
ProGuard/R8 by default removes BuildConfig class from release Android build.
JavaScript BuildConfig object will be undefined.
To keep it add the following line to proguard-rules.pro file
-keep class **.BuildConfig { *; }
You can replace ** with your.package.name.
Native modules are auto-linked since v0.60. If you have a lower version, you need this:
- Include this module in
android/settings.gradle:
include ':react-native-build-config'
include ':app'
project(':react-native-build-config').projectDir = new File(rootProject.projectDir,
'../node_modules/react-native-build-config/android')
- Add a dependency to your app build in
android/app/build.gradle:
dependencies {
...
compile project(':react-native-build-config')
}
- Change your main activity to add a new package, in
android/app/src/main/.../MainActivity.java:
import com.ismaeld.RNBuildConfig.RNBuildConfigPackage; // add import
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
/* ... */
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new RNBuildConfigPackage(BuildConfig.class), // add the package here
);
}
};