react-native-config example does not read variables from .env file or .env.prod

Created on 11 Oct 2017  路  37Comments  路  Source: luggit/react-native-config

Windows 10
Mobile Device Nexus 6P
react-native-cli: 2.0.1
react-native: 0.38.1
SET ENVFILE='.env.prod' && react-native run-android

when i run above line on example application its not reading anything from .env file.

Most helpful comment

I am unable to solve this problem. with the provided solutions

All 37 comments

Did you do the Android setup?

https://github.com/luggit/react-native-config#extra-step-for-android

Same problem with ENVFILE=.env.prod cd android && ./gradlew assembleRelease , even with

apply plugin: "com.android.application"
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"

Working fine with react-native run-android debug mode.

Can you paste the output of react-native run-android?

When it's setup properly you can see it's reading (or failing to read) the env like:

$ react-native run-android
Starting JS server...
Building and installing the app on the device (cd android && ./gradlew installDebug...
Reading env from: .env
...

It's fine withreact-native run-android

Building and installing the app on the device (cd android && ./gradlew installDebug)...
Reading env from: .env
...

But when I want to build an apk for release, config.ROOT_URL is undefined, there is my .env.prod

ENV=prod
ROOT_URL=https://playgrounds-app.com

No fail spotted 馃

$ ENVFILE=.env.prod cd android && ./gradlew assembleRelease
Reading env from: .env

Oh! How are you setting it to use .env.prod? What are you calling to build the apk?

ah hmm. Does it work if you export ENVFILE=.env.prod instead of setting it this way?

I also have a .env, so Reading env from: .env should work

ENV=prod
ROOT_URL=https://playgrounds-app.com

How I use it in my app :

import Config from 'react-native-config';

const axiosConfig = {
  baseURL: `${Config.API_URL}`,
  responseType: 'json',
  headers: { 'Content-Type': 'application/json' },
};

Logs when I launch my app (signed) : java.lang.IllegalArgumentException: unexpected url: undefined/playgrounds/around_me?latitude=48.85661166666667&longitude=2.3522216666666664&distance=5

I also see this :react-native-config:compileReleaseJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.). but I'm not sure it's relevant.

Ah weird, so you see "Reading env from .env" when you call gradlew assembleRelease too?

I never had this issue myself, but wondering if it's because of a custom applicationId/applicationIdSuffix, or Proguard.

Have you checked the advanced setup and troubleshooting sections here?

Maybe clean the build too聽to be sure: cd android/ && ./gradlew clean

I change my applicationId and it works! Don't know exactly why.
I used com.playgrounds.app, it works with com.playgrounds, which was the default id.
Thanks for your time 馃檹

Ah, phew!

Surprised this didn't cause a compilation failure in 1st place :| hopefully we can fix this at some point.

But thank you! Closing this for now.

I am unable to solve this problem. with the provided solutions

It seems the issue for me I needed to add

new ReactNativeConfigPackage(), in the MainApplication.java

Have the same problem. I can get environment variable when just run react-native run-android, but if I try build sign apk a just can't get only developer values. ((

For use this constants you mast exported it. And you can't use other assemble type, only assembleRelease. If you create other build type in build.gradle, simple copy release, this don't work.

"build-staging:android": "export ENVFILE=.env.staging && cd android && ./gradlew assembleRelease",

Please add in documentation section "How install manual" - because I spend many time for run lib, because react-native link doesn't work on me.

It seems the issue for me I needed to add

new ReactNativeConfigPackage(), in the MainApplication.java

I was days trying to figure out why my react Config variable was always empty. That solved it. This NEEDS to be added to the README!

Also the import is:
import com.lugg.ReactNativeConfig.ReactNativeConfigPackage;

for me I was using build variants and adding BuildConfig to progurad file solved it.

I already had new ReactNativeConfigPackage() inside MainApplication.java but I had to put it after all the CodePush related stuff. Hope this helps someone!

for react native 0.60.0, I have fixed it adding new ReactNativeConfigPackage() manually like so:
```
@Override
protected List getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable")
List packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
packages.add(new ReactNativeConfigPackage());

  return packages;
}

```
it seems that react native autolinking feature not implemented in this package yet

When we select APK from app bundle in the deploy option, Gradle runs a task named extractApksForFlavour which breaks this regex: https://github.com/luggit/react-native-config/blob/master/android/dotenv.gradle#L10. So make sure that you choose Default APK option when running on device. Screenshot 2019-07-15 at 5 08 21 PM

for me I was using build variants and adding BuildConfig to progurad file solved it.

@pradhul Can you explain how you did that?

For [email protected] released yesterday it seems like it's no longer required to manually add the package and in fact when you do you get an error. Autolinking appears to work out of the box now for this package on react-native@^0.60.5.

I installed running the following:

$ yarn add react-native-config
$ cd ios && pod install && cd ../

And then I made the manual updates that the README specifies for Android and iOS:

  1. Adding apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle" to android/app/build.gradle
  2. Adding -keep class com.mypackage.BuildConfig { *; } to android/app/proguard-rules.pro
  3. Adding pre-build scripts in XCode to be able to use variables in Info.plist and project build settings.

And everything just... worked. In both iOS and Android.

So I think the only thing that's needed is notes in the README:

If your react-native version is < 0.60.0, follow the linking steps below.
If your react-native version is >= 0.60.0 and < 0.60.5, you still need to follow the linking steps below but you need to additionally manually register the package by editing MainApplication.java to add packages.add(new ReactNativeConfigPackage()); in the getPackages method.
If your react-native version is > 0.60.0 this package will work with the autolinker and you don't need to manually run any command to link the package outside of running pod install in the ios directory per the react-native-community/cli docs.

Note: for any RN version (including autolinking) you still need to perform the "extra steps" described below.

Or something to that effect.

EDIT: I've observed this behaviour for both the current published version and the most recent unpublished version (yarn add react-native-config@"luggit/react-native-config#89a602bf8be3808838403a97afaf915caeec76c2")

@bericp1 I started using RN 0.60.5 and for simple $env:ENVFILE=".env.mycustomenv"; react-native run-android works fine! But adding productFlavors in that mix fucks me up.
Running $env:ENVFILE=".env.mycustomenv"; react-native run-android --variant=myvariantDebug

I still get:
> Configure project :app Reading env from: .env.mycustomenv

but Config.MY_ENV_VAR always returns undefined
Any ideas?

@aecc ifg I add import com.lugg.ReactNativeConfig.ReactNativeConfigPackage;

and then add new ReactNativeConfigPackage()

I get error: package com.lugg.ReactNativeConfig does not exist

Am I missing an install step?

i tried all but nothing resolve my issue , still get undefine

ly

can you give some example that where we run that command because export give error , or may be some other things

I change my applicationId and it works! Don't know exactly why.
I used com.playgrounds.app, it works with com.playgrounds, which was the default id.
Thanks for your time 馃檹

so you need to change aplicationId to default id..??
I tried to change all my version code applicationId so I can define it as another app, but it always returns undefined even though successfully build, can you help..??

I was updating my app from React Native 0.57 to 0.61.3 ran into a lot of issues and unlinked all packages, since newer React Native introduced auto-linking.

  • That didn't work so I needed link it back: react-native link react-native-config.
  • That still wasn't enough so I needed to add apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle" on the 2nd line of android/app/build.gradle, because I didn't have it there before the update.

It works for me:

Adding -keep class com.mypackage.BuildConfig { *; } to android/app/proguard-rules.pro

I got this working through some experimentation. For me, I'm using yarn workspaces so my node_modules folder is up a directory, and that's why it was failing. I just replaced the suggested line with a direct import of the needed file.

In your app/build.gradle, replace:

apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"

Replace with:

apply from: "../../node_modules/react-native-config/android/dotenv.gradle"

Or in my case and using yarn workspaces, replace with:

apply from: "../../../node_modules/react-native-config/android/dotenv.gradle"

Did you do the Android setup?

https://github.com/luggit/react-native-config#extra-step-for-android

Works!

@LuizFBrisighello Hi, did you resolve your undefined issue with Product Flavor? I am getting undefined too.

me too! what's the solution?

@dipeshkoirala21 Sorry for the late answer and its not a very good news, at the time I figured out but now it was over a year ago.. I really dont remeber.

Im still on RN 0.60.5, using react-native-config 0.11.7
I think following extra-step-for-android in the link down bellow fixes it

Did you do the Android setup?

https://github.com/luggit/react-native-config#extra-step-for-android

Did you do the Android setup?

https://github.com/luggit/react-native-config#extra-step-for-android

it works for us

Was this page helpful?
0 / 5 - 0 ratings