Bring some 12 factor love to your mobile apps!
Go to file
Pedro Belo 57ea6c3a03 take custom env for ios in a file :{ /tmp/envfile
not great but dunno how to do it otherwise yet
2016-03-03 16:34:45 -08:00
Example readme on example app 2016-03-03 13:35:15 -08:00
android always use relative paths for ENVFILE 2016-03-03 13:32:32 -08:00
ios take custom env for ios in a file :{ /tmp/envfile 2016-03-03 16:34:45 -08:00
.gitignore gitignore 2016-02-22 17:21:49 -08:00
LICENSE mit license 2016-02-24 14:18:16 -08:00
README.md Merge pull request #4 from danielweinmann/fix_readme_android 2016-03-01 23:02:42 -08:00
index.android.js bring android implementation from react-native-android-config 2016-02-22 13:24:36 -08:00
index.ios.js setup example app to consume ios stub 2016-02-22 17:37:50 -08:00
package.json v0.0.3 2016-03-01 23:11:50 -08:00

README.md

Config variables for React Native apps

Module to expose config variables to your javascript code in React Native, supporting both iOS and Android.

Bring some 12 factor love to your mobile apps!

Usage

Declare config variables in .env:

API_URL=https://myapi.com
GOOGLE_MAPS_API_KEY=abcdefgh

Then access from your app:

import Config from 'react-native-config'

Config.API_URL  // 'https://myapi.com'
Config.GOOGLE_MAPS_API_KEY  // 'abcdefgh'

Android

Config variables set in .env are available to your Java classes via BuildConfig:

public HttpURLConnection getApiClient() {
    URL url = new URL(BuildConfig.API_URL);
    // ...
}

You can also read them from your Gradle configuration:

signingConfigs {
    release {
        storeFile file(project.env.get("RELEASE_STORE_FILE"))
        storePassword project.env.get("RELEASE_STORE_PASSWORD")
        keyAlias project.env.get("RELEASE_KEY_ALIAS")
        keyPassword project.env.get("RELEASE_KEY_PASSWORD")
    }
}

And use them to configure libraries in AndroidManifest.xml and others:

<meta-data
  android:name="com.google.android.geo.API_KEY"
  android:value="@string/GOOGLE_MAPS_API_KEY" />

iOS

Xcode support is missing; variables declared in .env. can be consumed from React Native apps in iOS via Config, but not from plist files.

Different environments

Save config for different environments in different files: .env.staging, .env.production, etc.

By default react-native-config will read from .env, but you can change it setting ENVFILE before running it, like:

$ ENVFILE=.env.staging react-native run-android

This is only support in Android at the moment  The iOS equivalent should be coming soon.

Setup

Install the package:

$ npm install react-native-config --save

Then follow the platform-specific instructions below:

iOS

Link the library with rnpm:

$ rnpm link react-native-config

Android

Include this module in android/settings.gradle:

include ':react-native-config'
include ':app'

project(':react-native-config').projectDir = new File(rootProject.projectDir,
  '../node_modules/react-native-config/android')

Apply a plugin and add dependency to your app build, in android/app/build.gradle:

// 2nd line, add a new apply:
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"

// down below, add new compile:
dependencies {
    ...
    compile project(':react-native-config')
}

Change your main activity to add a new package, in android/app/src/main/.../MainActivity.java:

import com.lugg.ReactNativeConfig.ReactNativeConfigPackage; // add import

public class MainActivity extends ReactActivity {
    // ...

    @Override
    protected List<ReactPackage> getPackages() {
        return Arrays.<ReactPackage>asList(
            new MainReactPackage(),
            new ReactNativeConfigPackage() // add package
        );
    }