Giter VIP home page Giter VIP logo

react-native-build-config's Introduction

React Native - Build config access from JS (android only for now)

Module to expose build config variables set in Gradle to your javascript code in React Native.

Inspired from deprecated luggit/react-native-build-config.

Install

npm i ismaeldcom/react-native-build-config

RN > 0.47 use 0.11 or higher

Automatically link

With React Native 0.27+

react-native link react-native-build-config

With older versions of React Native

You need rnpm (npm install -g rnpm)

rnpm link react-native-device-info

Manually link

  1. 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')
  1. Add a dependency to your app build in android/app/build.gradle:
dependencies {
    ...
    compile project(':react-native-build-config')
}
  1. 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
          );
      }
  };

Usage

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 and VERSION_CODE, both coming from the build settings. Keep in mind the code is a number
  • APPLICATION_ID: Your package name, eg: com.Example
  • DEBUG: set to true when running the app locally
  • BUILD_TYPE and FLAVOR: more build settings

react-native-build-config's People

Contributors

askesis avatar ismaeldcom avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.