Module to expose build config variables set in Gradle to your javascript code in React Native.
Inspired from deprecated luggit/react-native-build-config.
npm i ismaeldcom/react-native-build-config
react-native link react-native-build-config
You need rnpm
(npm install -g rnpm
)
rnpm link react-native-device-info
- 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
);
}
};
Declare config variables in Gradle, under android/app/build.gradle
:
android {
defaultConfig {
buildConfigField "String", "API_URL", '"https://myapi.com"'
buildConfigField "Boolean", "SHOW_ERRORS", "true"
...
Then access those from javascript:
const BuildConfig = require('react-native-build-config');
BuildConfig.API_URL // "https://myapi.com"
BuildConfig.SHOW_ERRORS // true
Gradle sets some variables by default:
VERSION_NAME
andVERSION_CODE
, both coming from the build settings. Keep in mind the code is a numberAPPLICATION_ID
: Your package name, eg:com.Example
DEBUG
: set totrue
when running the app locallyBUILD_TYPE
andFLAVOR
: more build settings