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

Android release build is picking debug environment instead of prod #56

Closed
Sterlingryan opened this issue Aug 11, 2020 · 9 comments
Closed
Labels
waiting author response When a ticket has been addressed but you're waiting for the author to confirm the fix. wontfix This will not be worked on

Comments

@Sterlingryan
Copy link

I'm having an issue where the Android release build is picking up the variables inside .env instead of variables inside .env.production. iOS works fine.

"react-native": "0.62.2",
"react-native-dotenv": "^2.3.0",
"babel-plugin-dotenv-import": "^2.1.0",

.bablerc:

{
    "plugins": [
        "add-react-displayname",
        "./node_modules/@heap/react-native-heap/instrumentor/src/index.js",
        ["dotenv-import", {
            "moduleName": "@env",
            "path": ".env",
            "blacklist": null,
            "whitelist": null,
            "safe": false,
            "allowUndefined": true
        }],
    ]
}

babel.config.js

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
};
@goatandsheep
Copy link
Owner

goatandsheep commented Aug 11, 2020

You can either wait / help #1 or set path to .env.production. Let me know if that works for you!

@goatandsheep goatandsheep added the waiting author response When a ticket has been addressed but you're waiting for the author to confirm the fix. label Aug 11, 2020
@goatandsheep
Copy link
Owner

you may also use the env option in babel

@FanchenBao
Copy link

I am having the same issue. Running react-native run-android --variant release does not pick up .env.production. It still loads .env. I am able to work around this problem by changing the path to .env.production, but that doesn't feel like a robust fix.

The same issue cannot be repeated in iOS, because for whatever reason, I need to manually bundle in iOS. And the react-native bundle commands is picking up .env.production if NODE_ENV=production is specified. However, specifying NODE_ENV=production does not help building release version for android.

@goatandsheep
Copy link
Owner

Some people are saying to follow the Android signing guide https://reactnative.dev/docs/signed-apk-android#adding-signing-config-to-your-apps-gradle-config

@goatandsheep goatandsheep reopened this Jan 20, 2021
@FanchenBao
Copy link

Thanks for the response. I have signing set up already, but still not working. I keep getting this error message:

SyntaxError: /.../src/config/appConfig.js: "BACKEND_API_ENDPOINT" is not defined in .env
>  7 | import {BACKEND_API_ENDPOINT, ENV} from '@env';
   5 |  */
     |         ^^^^^^^^^^^^^^^^^^^^

I think it is because .env.production is not loaded, so the system looks in .env for BACKEND_API_ENDPOINT. But BACKEND_API_ENDPOINT is not defined in .env; it is only defined in .env.production.

I have also tried to define BACKEND_API_ENDPOINT in .env and give it a dummy value, hoping that the system will overwrite the dummy with the real one in .env.production. Unfortunately, the system picks the dummy value directly.

@goatandsheep
Copy link
Owner

Do you think you can try adding the variable to .env just to troubleshoot? Maybe it's not loading properly at all!

@FanchenBao
Copy link

I did a bit more digging with the path set to .env.

  1. I added BACKEND_API_ENDPOINT with a dummy value to .env and the dummy value is picked up. when running npx react-native run-android --variant release.
  2. I then returned .env to its original state (i.e. without the added BACKEND_API_ENDPOINT). Surprisingly, when I use this command BABEL_ENV=production npx react-native run-android --variant release, .env.production is picked up.
  3. However, running NODE_ENV=production npx react-native run-android --variant release still fails to pick up .env.production. It picks up .env and complains that BACKEND_API_ENDPOINT does not exist.
  4. Running npx react-native run-android successfully picks up .env.development.

In conclusion, I can set path to .env and have react-native-dotenv pick the right environment for development, and production as long as I set BABEL_ENV=production.

Some correction on my previous post: I do NOT need to set NOTE_ENV=production to work in iOS. react-native-dotenv can pick up .env.production in command react-native bundle --dev false without the need of setting NODE_ENV.

@goatandsheep
Copy link
Owner

@FanchenBao thank you so much for your incredible detailed response! I'm going to have to look into this a bit more

@stale
Copy link

stale bot commented Mar 21, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label Mar 21, 2021
@stale stale bot closed this as completed Mar 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting author response When a ticket has been addressed but you're waiting for the author to confirm the fix. wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants