Skip to content
Salatiel Queiroz
DibbbleLinkedIn

How to read environment variables inside AppDelegate or AndroidManifest using React Native Config

React Native, Android, iOS1 min read

To read environment variables inside the AppDelegate file in iOS or the AndroidManifest file in Android using React Native Config you need to already have react-native-config installed and properly setup.

I'm using the following .env file as an example for this tutorial

1MY_API_KEY=123456789

Android

Add a meta-data tag inside application tag in AndroidManifest.xml:

1<meta-data
2 android:name="com.example.API_URL"
3 android:value="@string/MY_API_KEY"
4/>

iOS

  1. First, open the AppDelegate.m(m) file and import the React Native Config module before the @implementation AppDelegate declaration
  2. Inside didFinishLaunchingWithOptions method in AppDelegate.m create a NSString with the name of your choice and set your environment key name inside the quotes
1// 1. import module
2#import "RNCConfig.h"
3
4@implementation AppDelegate
5
6- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
7
8// 2. declare the variable
9NSString *myApiKey = [RNCConfig envFor:@"MY_API_KEY"];
10
11// example of use:
12[GMSServices provideAPIKey: myApiKey];

Remember to build your app after implementing the changes above.

That's it! Now you can read environment variables inside the AppDelegate or AndroidManifest using React Native Config.

© 2024 by Salatiel Queiroz. All rights reserved.
Theme by LekoArts