When migrating from react-native-config to react-native-keys, you must transition from .env files to JSON-based key files that distinguish between secure and public keys. You also need to update your Android Gradle configuration, iOS Pre-actions, and both Native and JavaScript access patterns.
1. Data Format Migration
Instead of a flat .env file, use a JSON structure. Keys intended for sensitive use should be placed in the secure object, while non-sensitive keys go in the public object.
Example Transformation:
Old (.env.development):
BASE_URL=https://www.example.com
APP_NAME=Example
GOOGLE_API=ABCDEF
BRANCH_API=ABCDEF
PACKAGE_ID=com.example.rnkeys.dev
New (keys.development.json):
{
"secure": {
"GOOGLE_API": "ABCD",
"BRANCH_API": "ABCDEF"
},
"public": {
"APP_NAME": "dev RNKEYS",
"PACKAGE_ID": "com.example.rnkeys.dev"
}
}
2. Android Configuration
Update build.gradle plugin application:
In android/app/build.gradle, replace the react-native-config plugin with the react-native-keys plugin:
// Remove this:
// apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
// Add this:
apply from: project(':react-native-keys').projectDir.getPath() + "/RNKeys.gradle"
Update key file mapping:
In android/app/build.gradle, change project.ext.envConfigFiles to project.ext.keyFiles and point to your JSON files:
project.ext.keyFiles = [
debug: "keys.development.json",
release: "keys.staging.json",
release: "keys.json",
]
Update Native Java Access:
For secure keys, use the KeysModule.getSecureFor static method instead of accessing BuildConfig directly.
// Old way:
// BuildConfig.GOOGLE_MAPS_API_KEY
// New way:
import static com.reactnativekeysjsi.KeysModule.getSecureFor;
getSecureFor("GOOGLE_MAPS_API_KEY")
BuildConfig.PACKAGE_ID
Update Gradle Configuration Access:
If reading keys within your Gradle files (e.g., defaultConfig), use project.keys.get():
// Old way:
// applicationId project.env.get("APP_ID")
// New way:
defaultConfig {
applicationId project.keys.get("APP_ID")
}
3. iOS Configuration
Update Pre-actions Script:
In Xcode, go to Edit scheme... -> Build -> Pre-actions, click +, and select New Run Script Action. Update the script to export the KEYSFILE and run the new keysIOS.js script:
export KEYSFILE=keys.development.json
"${SRCROOT}/../node_modules/react-native-keys/keysIOS.js"
Update Native Objective-C Access:
Import Keys.h and use secureFor: for sensitive keys and publicFor: for non-sensitive keys.
// Old way:
// #import "RNCConfig.h"
// NSString *googleMapApiKey = [RNCConfig envFor:@
// Example of the new JSON structure
{
"secure": {
"GOOGLE_API": "ABCD",
"BRANCH_API": "ABCDEF"
},
"public": {
"APP_NAME": "dev RNKEYS",
"PACKAGE_ID": "com.example.rnkeys.dev"
}
}