Skip to content

๐Ÿ›  Easily access any of the build configs defined in build.gradle or info.plist from your JS code.

License

Notifications You must be signed in to change notification settings

sishuguojixuefu/react-native-config-reader

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

30 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ›  react-native-config-reader npm npm

A native library to access all the native code's build configurations from JS.

Installation

  1. $ npm install react-native-config-reader --save or $ yarn add react-native-config-reader

  2. $ react-native link react-native-config-reader

  3. Go to android/app/src/main/packageName/MainApplication.java and find line

    new RNConfigReaderPackage()

    and replace it with

    new RNConfigReaderPackage(BuildConfig.class)

See manual installation below if you have issues with react-native link.

Usage

import RNConfigReader from 'react-native-config-reader';

// access any of the defined config variables in andoird build gradle or ios info.plist
const configValue = RNConfigReader.ANY_DEFINED_CONFIG_FIELD;

More examples

Create new build config field inside android build.gradle file (android/app/build.gradle)

android {

    defaultConfig {
        applicationId "com.react-native.react-native-config-reader"
        versionCode 1
        versionName "1.0"
        
        buildConfigField "String", "TEST_CONFIG_FIELD", "Hello I'm your test config value"
    }
}

Create new field inside ios info.plist file

<?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>CFBundleDisplayName</key>
	<string>com.react-native.react-native-config-reader</string>
  
	<key>TEST_CONFIG_FIELD</key>
	<string>"Hello I'm your test config value"</string>
</dict>
</plist>

Now you can acess them inside the JS code

import { Platform } from 'react-native';
import RNConfigReader from 'react-native-config-reader';

if(Platform.OS === 'ios') {
  const iosBundleDisplayName = RNConfigReader.CFBundleDisplayName;
  const testConfigValue = RNConfigReader.TEST_CONFIG_FIELD;
}

if(Platform.OS === 'android') {
  const androidApplicationID = RNConfigReader.applicationId;
  const testConfigValue = RNConfigReader.TEST_CONFIG_FIELD;
}

Manual installation

iOS

  1. In XCode, in the project navigator, right click Libraries โžœ Add Files to [your project's name]
  2. Go to node_modules โžœ react-native-config-reader and add RNConfigReader.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libRNConfigReader.a to your project's Build Phases โžœ Link Binary With Libraries
  4. Run your project (Cmd+R)<

Android

  1. Open up android/app/src/main/java/[...]/MainActivity.java
  • Add import com.reactlibrary.RNConfigReaderPackage; to the imports at the top of the file
  • Add new RNConfigReaderPackage() to the list returned by the getPackages() method
  1. Append the following lines to android/settings.gradle:
    include ':react-native-config-reader'
    project(':react-native-config-reader').projectDir = new File(rootProject.projectDir, 	'../node_modules/react-native-config-reader/android')
    
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:
      compile project(':react-native-config-reader')
    

Windows (Beta)

Read it!

  1. In Visual Studio add the RNConfigReader.sln in node_modules/react-native-config-reader/windows/RNConfigReader.sln folder to their solution, reference from their app.
  2. Open up your MainPage.cs app
  • Add using Config.Reader.RNConfigReader; to the usings at the top of the file
  • Add new RNConfigReaderPackage() to the List<IReactPackage> returned by the Packages method

License

MIT License

Copyright (c) 2019 Chanaka Athurugiriya

About

๐Ÿ›  Easily access any of the build configs defined in build.gradle or info.plist from your JS code.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 26.8%
  • Java 23.3%
  • C# 23.2%
  • Python 12.0%
  • JavaScript 11.0%
  • Ruby 3.7%