lugg / react-native-config

Bring some 12 factor love to your mobile apps!
MIT License
4.8k stars 659 forks source link

Empty config even though `.env` is filled up #143

Open novito opened 7 years ago

novito commented 7 years ago

I have created a react native app using create-react-native-app and I have this in my main app.js

import Config from 'react-native-config';

In my constructor I have:

console.log(Config.API_KEY)

And in my .env file I have:

API_KEY = test

That Config.API_KEY appears to be undefined.

Is this package supposed to work outside of Android and IOS (that is, straight on app.js)?

legshampoo commented 5 years ago

I had this problem trying to use build variants for Android.

Solved it by doing this in MainApplication.java import com.lugg.ReactNativeConfig.ReactNativeConfigPackage; and further down new ReactNativeConfigPackage()

and then following the 'advanced android setup' step in the readme, but modifying it to be defaultConfig { resValue "string", "build_config_package", "${applicationId}" } where the ${applicationId} was the deciding factor in my case, because I was adding an 'applicationIdSuffix' to each build version

The0racle commented 5 years ago

If you're using multiple flavors on Android, with different applicationId (not applicationIdSuffix), do what @loginov-rocks and @bericp1 said as it should fix.

conor909 commented 5 years ago

@legshampoo I always get this: ERROR: Project with path ':react-native-config' could not be found in project ':app'.

Is there a linking step for Android? Works fine for iOS

Chepkeitany commented 5 years ago

Still getting the same issue. I get an empty object for Config. Works perfectly in the native code though only the Javascript part that's problematic.

jeveloper commented 5 years ago

@Chepkeitany HI there , i had noticed that the lastest release builds (no longer apk but aab due to new google requirements) with no other changes is turning Config into empty object. Builds are not failing.

Have you or others seen this? RN 0.59 node 12 macos

single .env file

Chepkeitany commented 5 years ago

@jeveloper That's a similar experience. Although I didn't take note when Config was being returned as an empty object. Now that I think about it, I think it was around that time when we switched to aab on the playstore.

conor909 commented 5 years ago

I got it working for Android eventually, here's my setup. (iOS just worked without any manual steps for me)

"react-native": "0.59.0" "react-native-config": "0.11.5"

I'm also using yarn workspaces with the .env file outside the react-native project root, so to start android I need to give the path to the start command in my package.json:

scripts: {
   "start-android-dev": "ENVFILE=../../.env react-native run-android",
}

> android/settings.gradle

rootProject.name = 'MY-PROJECT-NAME'
...
include ':react-native-config'
project(':react-native-config').projectDir = new File(rootProject.projectDir, '/MY-PATH-TO/node_modules/react-native-config/android')
...
include ':app'

> android/app/build.gradle

apply plugin: "com.android.application"
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
...
...
project.ext.envConfigFiles = [
  debug : ".env",
  release : ".env",
  stagingrelease : ".env.staging",
  productionrelease : ".env.production"
]
...
...
dependencies {
    ...
    implementation project(':react-native-config')
    ...
}

> android/app/main/src/java/MY-APP-NAME/MainApplication.java

...
import com.lugg.ReactNativeConfig.ReactNativeConfigPackage;
import com.MY-APP-NAME.BuildConfig;
...
...
    @Override
    public boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
       ...
        new ReactNativeConfigPackage()
       ...
      );
    }
helderberto commented 4 years ago

Maybe in issue #400 I made a response that could help you. See here.

chenop commented 4 years ago

Note that "PACKAGE_NAME_IN_ANDROID_MANIFEST.XML" means: [Package name in AndroidManifest.xml] not [package name in AndroidManifest].xml

defaultConfig {
    resValue "string", "build_config_package", "com.packageName" // No .xml extension!
   ...
}

Yes I felt for it...

TNChalise commented 3 years ago

Mine was space at the beginning of the .env file.

rubentlc commented 3 years ago

In my case, what fixed the problem was this point: https://github.com/luggit/react-native-config#problems-with-proguard

AizenSousuke commented 2 years ago

is there a fix for this issue at this point of time? doesn't seem to work in js

dylanhillier commented 1 year ago

I had some issues recently on iOS where changes in my .env file were not reflected in my mobile app. What didn't work:

What did work:

It's not entirely clear whether or not the need to close the simulator is a result of some misconfiguration on my end. I'm using react-native-config@1.5.0.

owen800q commented 1 year ago

@dylanhillier Working for me, I think the doc need to mention every time anything inside .env has been changed, need to rebuild the app.

dylanhillier commented 1 year ago

@owen800q

I use the scripts from within package.json typically to build/run my react-native mobile app.

On Android, if my mobile app is running via a simulator, and I make a change to the .env file, then for this change to be make available to my app, i just need to re-run yarn android. I consider this to be working as expected.

On iOS, if my mobile app is running via a simulator, and i make a change to the .env file, then this change will not be visible to my mobile app by simply running yarn ios. I consider this to be not working as expected. For my .env changes to be available, I have to run the build via xcode.

This is inconsistent and undesirable. It would be nice for it to just work via yarn ios and to be consistent between ios and android.

edit: This appears to be a known and ongoing issue. The following issues are likely due to the same root-cause.

  1. https://github.com/luggit/react-native-config/issues/703
  2. https://github.com/luggit/react-native-config/issues/737
  3. https://github.com/luggit/react-native-config/issues/731
  4. https://github.com/luggit/react-native-config/issues/710
  5. https://github.com/luggit/react-native-config/issues/698
Biplovkumar commented 1 year ago

Same issue in 1.5.1.

ttruongatl commented 9 months ago

In the code snippet from the RNCConfigModule.java, the package name (stored in the variable className, though the name might be a bit misleading) is intended to be retrieved from strings.xml using a specific method. However, if the build_config_package string is absent in strings.xml, the system defaults to using the ApplicationId from build.gradle through the method getReactApplicationContext().getApplicationContext().getPackageName().

This creates an issue when the application Id is different from the packageName defined in MainApplication.java.

To resolve this issue, you should add a new line in the strings.xml file:

<string name="build_config_package">your_package_name</string>

Here, your_package_name should match the package name defined in your MainApplication.java file.