react-native-config: Can not read .env file on Android.
Hi there. I have a problem with the installation of RNC. It works like a charm on IOS but I can not read the env variables on Android. I checked this repo’s installation guide many times and matched the introduction step by step. I’ve read many issues related to my problem but nothing helped me.
I’ll let you know the implementation, may you can help me.
My RN & RNC version:
"react-native-config": "^1.5.0"
, "react-native": "0.70.6"
.
My gradle file:
project.ext.envConfigFiles = [
debug: ".env",
release: ".env",
]
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
...
def enableProguardInReleaseBuilds = false
...
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 142
versionName "0.1.38"
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
//React-native-config needs this line because we have flavors
resValue "string", "build_config_package", "com.ifsguide.app"
// codepush -> https://github.com/microsoft/react-native-code-push/issues/1961
resValue 'string', "CODE_PUSH_APK_BUILD_TIME", String.format("\"%d\"", System.currentTimeMillis())
if (isNewArchitectureEnabled()) {
// We configure the CMake build only if you decide to opt-in for the New Architecture.
externalNativeBuild {
cmake {
arguments "-DPROJECT_BUILD_DIR=$buildDir",
"-DREACT_ANDROID_DIR=$rootDir/../node_modules/react-native/ReactAndroid",
"-DREACT_ANDROID_BUILD_DIR=$rootDir/../node_modules/react-native/ReactAndroid/build",
"-DNODE_MODULES_DIR=$rootDir/../node_modules",
"-DANDROID_STL=c++_shared"
}
}
if (!enableSeparateBuildPerCPUArchitecture) {
ndk {
abiFilters (*reactNativeArchitectures())
}
}
}
}
...
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
// Caution! In production, you need to generate your own keystore file.
// see https://reactnative.dev/docs/signed-apk-android.
signingConfig signingConfigs.release
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}
flavorDimensions "version"
productFlavors {
production {
dimension "version"
applicationId "com.ifsguide.app"
resValue "string", "app_name", "IFS Guide"
ndk {
abiFilters "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
}
}
staging {
dimension "version"
applicationId "com.ifsguide.app.staging"
resValue "string", "app_name", "IFSG-Staging"
versionName defaultConfig.versionName + "-Staging"
ndk {
abiFilters "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
}
}
}
...
And, also this my BuildConfig.java
file from /build/generated/source/buildConfig/staging/debug/com/ifsguide/BuildConfig.java
.
/**
* Automatically generated file. DO NOT MODIFY
*/
package com.ifsguide;
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.ifsguide.app.staging";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "staging";
public static final int VERSION_CODE = 142;
public static final String VERSION_NAME = "0.1.38-Staging";
// Field from default config.
public static final boolean IS_NEW_ARCHITECTURE_ENABLED = false;
// Field from default config.
public static final String RELEASE_MODE = "staging";
}
My .env
file in the root of the project:
RELEASE_MODE=staging
So, as you can see, we have the env variable in the generated BuildConfig.java file but IDK why I can not access the variable on Android.
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 9
- Comments: 19
Adding the following line to the start of the file /android/app/build.gradle solved my problem:
apply from: project(":react-native-config").projectDir.getPath() + "/dotenv.gradle"
If it is totally necessary I consider the official set up documentation should include it. Thanks!I can confirm same issue happens on Android. iOS works fine.
react-native: 0.71.1
react-native-config: 1.5.0
/build/generated/source/buildConfig/staging/debug/com/ifsguide/BuildConfig.java
is not empty and fields generated correctly. CallingConfig
from JS side returns empty object.The issue that the topic starter has is because the res value of build_config_package in the gradle file
is incorrect.
The package name in
BuildConfig
isThe gradle file should therefore contain
Nevermind. My issue is that I had accidentally dropped a line from the Advanced android setup:
same issue
perfect solution ❤️
-keep class com.xcule.BuildConfig { *; }
Please check and confirm proguard is enabled / not.