# react-native-build-config

> Expose native build config to JS

Latest version **0.3.2** (published 2020-02-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-native-build-config
pnpm add react-native-build-config
yarn add react-native-build-config
bun add react-native-build-config
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.2 |
| Published | 2020-02-18 |
| First published | 2017-10-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 394.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Ismael Martín Alabarce |
| Maintainers | ismaeld |
| Keywords | react-native, android, ios, config, env, config-var, plist |

## Links

- npm: https://www.npmjs.com/package/react-native-build-config
- Homepage: https://github.com/ismaeldcom/react-native-build-config
- npm.io page: https://npm.io/package/react-native-build-config

## Alternatives

- [replicas-cli](https://npm.io/package/replicas-cli.md) — 3.0K weekly downloads
- [env-contract](https://npm.io/package/env-contract.md) — 133 weekly downloads
- [@openveo/api](https://npm.io/package/@openveo/api.md) — 61 weekly downloads
- [@ryniaubenpm2/cumque-error-reiciendis](https://npm.io/package/@ryniaubenpm2/cumque-error-reiciendis.md) — 54 weekly downloads
- [ts-global-type-extra](https://npm.io/package/ts-global-type-extra.md) — 11 weekly downloads

## Recent versions

- 0.3.2 (latest) — 2020-02-18
- 0.3.1 — 2019-07-18
- 0.3.0 — 2019-07-12
- 0.0.1 — 2017-10-25

## README

# React Native - Build config access from JS

Module to expose build config variables set in Gradle to your javascript code in React Native.

iOS version greatly contributed by [maddijoyce](https://github.com/maddijoyce)

## Install

```shell
npm i react-native-build-config
```

:warning: For React Native < 0.60, use version 0.1.0 and use manual linking (see below)		

## 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"
        ...
```

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:

```js
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

## Linking on React Native < 0.60

Native modules are auto-linked since v0.60. If you have a lower version, you need this:

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')
```

2.  Add a dependency to your app build in `android/app/build.gradle`:

```
dependencies {
    ...
    compile project(':react-native-build-config')
}
```

3.  Change your main activity to add a new package, in `android/app/src/main/.../MainActivity.java`:

```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
          );
      }
  };
```

---
_Source: https://npm.io/package/react-native-build-config · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
