Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment Variables keeps reading from .env #652

Open
my-name-is-nheo opened this issue Mar 30, 2022 · 19 comments
Open

Environment Variables keeps reading from .env #652

my-name-is-nheo opened this issue Mar 30, 2022 · 19 comments

Comments

@my-name-is-nheo
Copy link

my-name-is-nheo commented Mar 30, 2022

running ./gradlew bundleRelease to generate app bundles to release to google play store.

Debug mode reads correct environment variants with respective .env files. However, all variant's app bundles are being generated with only the .env file.

Root
image

app/build.gradle
image

image

  1. tried adding proguard override -keep class com.dottid.BuildConfig { *; }
  2. tried ENVFILE=.env.prod ./gradlew bundleRelease, but getting a bunch of gradle errors, ... without declaring an explicit or implicit dependency
  3. tried changing name of .env to .env.developer but then getting a missing env error when i run command ./gradlew bundleRelease

Thank you for your time

@jjdp
Copy link

jjdp commented Apr 14, 2022

are you using monterey?

@justin-tay
Copy link

The specific issue from the topic starter is likely because he is building using ./gradlew bundleRelease to build all the release variants at one go instead of specifically specifying the full variant eg. ./gradlew bundleProductionRelease.

You can look at https://github.com/luggit/react-native-config/blob/master/android/dotenv.gradle in this repository to see the logic for how the env file is determined. From the logic I don't think it is possible for it to determine the correct env file to use if you are building more than one build variant at a time. In which case it will fallback on .env.

As a sanity check you can after building see android/app/build/generated/source/buildConfig/{productFlavor}/{buildType}/{package}/BuildConfig.java to check if the values are as expected.

@Mr0cket
Copy link

Mr0cket commented May 16, 2022

I just updated Android studio & command line tools. Since then, I'm not receiving the the ENVFILE variable (with System.env['ENVFILE']) when dotenv.gradle is executed (Checked by printout output of System.env in dotenv.gradle).

Could there be a change in the new version of either android studio or command line tools which affects the ability to read this variable?

@justin-tay
Copy link

I just updated Android studio & command line tools. Since then, I'm not receiving the the ENVFILE variable (with System.env['ENVFILE']) when dotenv.gradle is executed (Checked by printout output of System.env in dotenv.gradle).

Could there be a change in the new version of either android studio or command line tools which affects the ability to read this variable?

Just tried. Using Android Studio Chipmunk on Windows and tested with SET ENVFILE=.env.staging && react-native run-android in package.json and it works. That said I made sure that I didn't have any pre-existing gradle daemons running. There might be potential issues with a pre-existing gradle daemon and reading the current environment variables. I actually don't use the ENVFILE environment variable and just configure variants for Android and schemes for iOS.

@Mr0cket
Copy link

Mr0cket commented May 18, 2022

That said I made sure that I didn't have any pre-existing gradle daemons running. There might be potential issues with a pre-existing gradle daemon and reading the current environment variables.

Good point. After retrying without pre-existing daemon running, the ENVFILE variable was read correctly.

This may seem pretty common sense in retrospect, but might still be worth adding a small note to the README for anyone who encounters the same issue.

@santitopo
Copy link

Similar thing to what @Mr0cket reports happening here. The ENVFILE=.env.something way of setting the env file seems to have stopped working as expected.

Up till now I have had different npm scripts, each doing ENVFILE=.env.something react-native run-android, and the envfile would be correctly changed if I ran two different builds in a row.

Now the first run will pick the correct env file, and the ones following will use the same ENVFILE variable, unless the gradle daemon is stopped with ./gradlew --stop before the build.

Not sure if it can have something to do but I've recently updated my JDK version from JDK 8 to 11 (Zulu OpenJDK as specified in the latest RN docs) and also the Xcode Command Line Tools to 13.2.0.

I'm running in Mac M1
react-native: 0.67.2
react-native-config: 1.4.6
macOS: BigSur (11.6.2)

@vinhtnk
Copy link

vinhtnk commented Jun 16, 2022

my solution is cp .env.prod .env before run a script, and .env was ignored from git

@Mr0cket
Copy link

Mr0cket commented Jun 16, 2022

my solution is cp .env.prod .env before run a script, and .env was ignored from git

Not a bad approach. I decided to go with the approach of specifying project.ext.envConfigFiles in app/build.gradle. This is interpreted based on the gradle command (e.g: bundleProductionRelease matches production enum defined in envConfigFiles)

@Stas-Buzunko
Copy link

Stas-Buzunko commented Jul 30, 2022

my solution is cp .env.prod .env before run a script, and .env was ignored from git

only this worked.
project.ext.envConfigFiles and ENVFILE=.env.something are ignored

"react-native-config": "^1.4.6"

"./gradlew --stop" - this helped to reset to .env from .env.beta (no idea how it .env.beta was selected before 🤨)

@chamaldesilva
Copy link

./gradlew --stop

This one worked for me finally!

@BoilingOil
Copy link

right above project.ext.envConfigFiles, set this:

project.ext.defaultEnvFile ="../path/to/.env.file"

@ToshKoevoets
Copy link

Yes super annoying, seems to be an issue with m1 or montery keeping gradle deamon in memory. For fastlane added the -- stop command in the build script

gradle(
  task: "--stop"
)

@AdamGerthel
Copy link

I just stumbled on this as well... the strange this is that it's only affecting one of my projects, not both. ./gradlew --stop worked for me so I've added it to my fastlane as per #issuecomment-1230403293

@CaioSantos
Copy link

right above project.ext.envConfigFiles, set this:

project.ext.defaultEnvFile ="../path/to/.env.file"

This one line worked like a charm in my case

@ToshKoevoets
Copy link

When running it locally the java process also stays running forever on the m1 new mac books, taking quite some memory, so works better to stop the process.

gradle(
task: "--stop"
)

@batmanashvili
Copy link

from your root folder try this

cd android && ./gradlew clean && ENVFILE=../.env.production ./gradlew bundleRelease && cd ..

@personalnadir
Copy link

personalnadir commented Jan 16, 2023

my solution is cp .env.prod .env before run a script, and .env was ignored from git

I have a few apps using the same codebase and was attempting to keep the .env files in separate folders. Using the ENVFILE=app1/.env.development option would not work for me on device. Only the above has worked correctly.

I was wondering if it might be do with the folder containing the per app .env files not being available once the app gets built? It was working in the simulator, but only broke once Xcode built and copied to a physical device

@mahardikaindra
Copy link

my solution is cp .env.prod .env before run a script, and .env was ignored from git

only this worked. project.ext.envConfigFiles and ENVFILE=.env.something are ignored

"react-native-config": "^1.4.6"

"./gradlew --stop" - this helped to reset to .env from .env.beta (no idea how it .env.beta was selected before 🤨)

Yes, worked for me

@Abhijeetk28
Copy link

Abhijeetk28 commented May 8, 2024

Hi , I am still facing this issue !
The release builds always picks up .env config instead of .env.prod or .env.staging.
The only solution which helped was copying .env.staging to .env before creating any other flavour release builds!

Can someone please look into this?
I am using -> "react-native-config": "1.5.1"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests