N Kaushik

How to read version code, version and product flavor in React native

September 20, 2021

How to read version code, version and product flavor in React native JavaScript code:

Product flavor helps to create different environments in Android. If you want to build an apk in React native for a specific flavor, you can do that by using a third-party library called react-native-config.

react-native-config:

react-native-config is an opensource library to create configur ation files in react native.

This is the link of its Github page.

You can create a .env file and read its properties in JavaScript, Android and iOS files.

Suppose, you have created a .env file at the root of your project with the below code:

KEY = 'dummykey'

You can then access this value in your JavaScript/TypeScript files as:

import Config from "react-native-config";

Config.KEY;

or in your Android Java files:

BuildConfig.KEY

or in your build.gradle file:

defaultConfig {
    apiKey project.env.get("KEY")
}

or in your iOS Objective-C files:

#import "ReactNativeConfig.h"

NSString *apiKey = [ReactNativeConfig envFor:@"KEY"];

react-native-config to build flavors:

You can also use it to read the current flavor. For example, if I print it, it will print the below content:

{
  VERSION_NAME: '1.0',
  APPLICATION_ID: 'com.myapp.id',
  BUILD_TYPE: 'debug',
  VERSION_CODE: 2,
  DEBUG: true,
  FLAVOR: 'qa',
  getConstants: [Function] }

So, you can simply read the FLAVOR property to get the current flavor.


Subscribe to my Newsletter

© 2021, nkaushik