First off, nice package.
In your related presentation, you mention defining different API endpoints as resource values in build.gradle, allowing different values to be used based on the flavor being built.
It would be handy to address this use case in a fashion similar to the Dotenv Ruby Gem, wherein environment variables can be overridden using files named after the current environment (e.g. .env.staging and .env.production). In React Native's case, it seems like it would make sense to read config vars in from the appropriate file based on the flavor that Gradle is building.
Currently, however, it looks like readDotEnv is called from outside of the android Gradle config - would it make sense to somehow put this method inside of the android closure so that it can read in different config variables based on a given flavor (or even buildType)?
Hey! Sorry I missed this, wasn't set to watch the project for some weird reason.
I personally use a single .env file per env to keep things simpler and easier to reason (eg: there's just .env.sample, use that to generate any other, etc). I realize this is cumbersome for certain projects/styles though, so down to experiment with alternatives.
So – if I got this right – we'd keep reading from .env in the normal case, but when ENVFILE is set then we'd read from that file and fill in missing values with whatever is in .env?
oh, and interesting take on how we could integrate this better form inside the android Gradle config.
Guess I ended up going this way because react-native run-android doesn't seem to take an environment, although maybe there's something you can pass there to get Gradle to figure out what environment it should build..?
I'm currently using a modified dotenv.gradle in which I use:
def envFile = project.hasProperty('envFile') ? project.get('envFile').toLowerCase() : ".env.debug"
This allows the envFile prop to be set when calling gradle with a different buildType, e.g.:
./gradlew -PenvFile=.env.production assembleReleaseProduction
I've tried to integrate into
android {
buildTypes {
debug {
...
}
releaseProduction {
...
}
}
}
but I couldn't figure out how, so if anyone has a more elegant solution, I'm curious to learn about it :-)
Seconding the proposal to support defining which env is loaded from buildTypes. I'm using Build Buddy to create builds, and it cannot set different environment variables per build type.
Follow up: the patch in https://github.com/luggit/react-native-config/pull/52 also does not work in Buddy Build when building multiple variants. They are running one command like ./gradlew assembleReleaseStaging assembleReleaseProduction to build multiple variants in one pass, but this only executes the dotenv.gradle code once for both builds. It ends up taking the staging settings for both builds.
@pedro
... because react-native run-android doesn't seem to take an environment...
There is a --variant switch for run-android, but react-native-config always uses the .dev file.
I'm still confused but to me it looks like ./gradlew assembleRelease already builds the different flavors using the correct env-files.
I think I understand better:
If I run react-native run-android --variant staging the correct environment file is loaded. But then the command exits with the error Task 'installStaging' is ambiguous in root project 'my_app'.
When I run react-native run-android --variant stagingDebug the command runs successful, but with the default .env file.
So it looks like react-native run --variant needs flavor + buildType which is not understood by react-native-config.
Follow up: the patch in #52 also does not work in Buddy Build when building multiple variants. They are running one command like ./gradlew assembleReleaseStaging assembleReleaseProduction to build multiple variants in one pass, but this only executes the dotenv.gradle code once for both builds. It ends up taking the staging settings for both builds.
@chase-seibert i need this as well. Not sure if or when Buddy Build will implement separate gradle runs for each variant.
Does anyone have a work around for this on Buddy Build?
I created a pull request to address this: https://github.com/luggit/react-native-config/pull/180 @caseyg1204
Most helpful comment
I think I understand better:
If I run
react-native run-android --variant stagingthe correct environment file is loaded. But then the command exits with the errorTask 'installStaging' is ambiguous in root project 'my_app'.When I run
react-native run-android --variant stagingDebugthe command runs successful, but with the default.envfile.So it looks like
react-native run --variantneeds flavor + buildType which is not understood by react-native-config.