Closed ATypescriptEnjoyer closed 5 years ago
Used this instead: https://github.com/luggit/react-native-config
Used this instead: https://github.com/luggit/react-native-config
Thanks! This library is perfect for that! Before I was using the react-native-dotenv
to import my .env
file inside my js code, but, now I need that my MainApplication.java
code import the .env
file too and I Can't do this with react-native-dotenv
lib.
My solution was to use both libs, react-native-dotenv
to my JS code, and react-native-config
to java code (native).
For any who would find the solution is easy:
react-native-library
:yarn add react-native-config
react-native link react-native-config
PRINCIPAL STEP: Add inside your MainApplication.java
file this code where you need to use the environment var:
// example, here I added the code inside onCreate() method of my MainApplication class
public void onCreate() {
String VAR_WITH_ANY_NAME = BuildConfig.ANY_ENV_VAR_AS_YOU_WANT;
// ... any code ...
}
Now, rebuild your project (I like to do it in different terminals, one for the bundle and another for build)
yarn && yarn start --reset-cache
yarn run android
I recommend to read react-native-config
README.md with most updated doc:
https://github.com/luggit/react-native-config#native-usage
Hi,
Is there any detailed way of using the .env file and building it into either the BuildConfig.java or using the files directly in a different way? Any help is appreciated :)