-
Notifications
You must be signed in to change notification settings - Fork 660
Description
Environment
- React Native: 0.81.4
- React: 19.1.0
- react-native-config: 1.5.9
- Android compileSdk/targetSdk: 36
- Kotlin: 2.1.20
- Using React Native’s new Gradle Plugin (com.facebook.react)
- Auto-linking for react-native-config is disabled — using manual apply from dotenv.gradle
Issue Description
I’ve set up three Android product flavors — development, staging, and production — each mapped to its own .env file (.env.development, .env.staging, .env.production). There is no root .env file.
✅ What works:
- App builds and installs successfully for each flavor.
- applicationId and app_name are correctly set per flavor (e.g., “Laundry(Staging)” appears in launcher).
- Build variant is correctly selected via CLI:
npx react-native run-android --mode=stagingDebug --appIdSuffix=staging
❌ What fails:
- All Config values are undefined at runtime.
console.log(Config.API_URL)
→undefined
console.log(JSON.stringify(Config, null, 2))
→{}
(empty object)- Happens consistently across all flavors and build types.
Relevant Code Snippets
1. android/app/build.gradle
(partial, key sections only)
apply plugin: "com.android.application"
apply plugin: "org.jetbrains.kotlin.android"
apply plugin: "com.facebook.react"
project.ext {
envConfigFiles = [
developmentDebug: ".env.development",
developmentRelease: ".env.development",
stagingDebug: ".env.staging",
stagingRelease: ".env.staging",
productionDebug: ".env.production",
productionRelease: ".env.production"
]
}
// Manual linking for react-native-config
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
android {
namespace "com.laundry"
compileSdk rootProject.ext.compileSdkVersion
flavorDimensions "environment"
productFlavors {
production {
dimension 'environment'
applicationId "com.laundry"
resValue "string", "app_name", "Laundry"
}
staging {
dimension 'environment'
applicationId "com.laundry.staging"
resValue "string", "app_name", "Laundry(Staging)"
}
development {
dimension 'environment'
applicationId "com.laundry.dev"
resValue "string", "app_name", "Laundry(Dev)"
}
}
defaultConfig {
applicationId "com.laundry"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
}
buildTypes {
debug {
signingConfig signingConfigs.debug
matchingFallbacks = ['debug', 'release']
}
release {
signingConfig signingConfigs.debug
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}
}
2. Example .env.staging
file (root of project)
API_URL=https://api.staging.laundry.app
APP_ENV=staging
SENTRY_DSN=https://example@sentry.io/12345
(Identical structure for .env.development and .env.production, with different values.)
3. React Native Component (App.tsx
)
import React from 'react';
import { StatusBar, Text, useColorScheme } from 'react-native';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import Config from 'react-native-config';
function App() {
const isDarkMode = useColorScheme() === 'dark';
// Debug logs
console.log('*** Full react-native-config Object ***');
console.log(JSON.stringify(Config, null, 2));
console.log('**************************************');
console.log('Config.API_URL (specifically):', Config.API_URL);
console.log('Config.APP_ENV:', Config.APP_ENV);
return (
<SafeAreaProvider>
<StatusBar barStyle={isDarkMode ? 'light-content' : 'dark-content'} />
<Text>API: {Config.API_URL || 'NOT FOUND'}</Text>
<Text>Env: {Config.APP_ENV || 'UNKNOWN'}</Text>
</SafeAreaProvider>
);
}
export default App;
4. package.json
script
"scripts": {
"android:staging": "npx react-native run-android --mode=stagingDebug --appIdSuffix=staging",
"android:dev": "npx react-native run-android --mode=developmentDebug --appIdSuffix=dev"
}
5. android/settings.gradle
(react-native-config inclusion)
include ':react-native-config'
project(':react-native-config').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-config/android')
(Auto-linking is disabled for this package — confirmed via react-native.config.js or absence thereof.)
What I’ve Tried
- Verified .env files are in project root and contain no syntax errors or BOM.
- Ran
./gradlew clean
inandroid/
and deletedandroid/app/build
before rebuilding. - Confirmed flavor selection works — app_name and package name change, so Gradle is building the correct variant.
- Checked
BuildConfig.java
inandroid/app/build/generated/.../stagingDebug/.../BuildConfig.java
— no env vars appear there. - Checked
res/values/generated.xml
— no string resources from .env files are generated. - Tried downgrading react-native-config to 1.4.11 — same behavior.
- Searched generated code — no trace of injected variables.
Expected Behavior
When running yarn android:staging
, the app should read from .env.staging
and Config.API_URL
should return https://api.staging.laundry.app
.
Request for Help
Is react-native-config v1.5.9 compatible with React Native 0.81.4’s new architecture and Gradle plugin? If not, is there a known workaround or fork that supports it?
If it should work — what’s the best way to debug whether dotenv.gradle
is executing and generating resources correctly? Are there specific tasks I should inspect or logs I can enable?
Any guidance on aligning flavor+buildType combinations with the env file mapping under the new plugin would be extremely helpful.