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 bundle can't read .env file #302

Open
hadnet opened this issue Mar 25, 2022 · 33 comments
Open

Android bundle can't read .env file #302

hadnet opened this issue Mar 25, 2022 · 33 comments
Assignees
Labels
💵 Funded on Issuehunt This issue has been funded on Issuehunt

Comments

@hadnet
Copy link

hadnet commented Mar 25, 2022

Issuehunt badges

I'm using react-native-dotenv for my Android app. When building a debug or even a release apk, my app reads the env variables correctly. All works fine. The problem is when building the bundle .aab (that one submit in Play Store). When I download my app from Play and install it my app can't read env variables! So why it is working good with debug/release apk and not with bundle .aab? Why this happens and how to fix it?

My babel.config.js

    plugins: [
      [
        'module:react-native-dotenv',
        {
          moduleName: '@env',
          path: '.env',
        },
      ],

I use an .env file and an .env.development file

and import my env vars like this

import {MY_ENV} from '@env'

I'm using the react-native-dotenv 3.1.1, RN 0.63.3 (bare workflow) and EXPO EAS for building the bundle.


IssueHunt Summary

Backers (Total: $2.00)

  • $2.00 have been anonymously funded.

Become a backer now!

Or submit a pull request to get the deposits!

Tips

@github-actions
Copy link

Hey, thank you for opening this issue! 🙂 To boost priority on this issue and support open source please tip the team at https://issuehunt.io/r/goatandsheep/react-native-dotenv/issues/302

@issuehunt-oss
Copy link

issuehunt-oss bot commented Mar 25, 2022

An anonymous user has funded $2.00 to this issue.


@issuehunt-oss issuehunt-oss bot added the 💵 Funded on Issuehunt This issue has been funded on Issuehunt label Mar 25, 2022
@hadnet
Copy link
Author

hadnet commented Mar 25, 2022

Hey, thank you for opening this issue! 🙂 To boost priority on this issue and support open source please tip the team at https://issuehunt.io/r/goatandsheep/react-native-dotenv/issues/302

Done. Hope you can help me with this.

@goatandsheep
Copy link
Owner

Have you tried making sure your build.gradle has the productFlavors and stuff setup? https://medium.com/@ywongcode/building-multiple-versions-of-a-react-native-app-4361252ddde5

@hadnet
Copy link
Author

hadnet commented Mar 25, 2022

Have you tried making sure your build.gradle has the productFlavors and stuff setup? https://medium.com/@ywongcode/building-multiple-versions-of-a-react-native-app-4361252ddde5

Thanks for quick response. No I didn’t know that, I’m new using this. I read the article but how to setup in order to work on bundle? So do I need to add flavorDimensions "default" but how to setup productFlavors for the bundle (.aab). Mine is like below

    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            // Caution! In production, you need to generate your own keystore file.
            // see https://reactnative.dev/docs/signed-apk-android.
            signingConfig signingConfigs.debug
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }

@goatandsheep
Copy link
Owner

debug is going to be develop and release is going to be production. this is unless you use APP_ENV

@hadnet
Copy link
Author

hadnet commented Mar 25, 2022

I’m not using the APP_ENV prop. So adding flavorDimensions "default" and using the productFlavors {} with that configuration above should be working?

@hadnet
Copy link
Author

hadnet commented Mar 25, 2022

And do I need to use an .env.production file or using the .env.development and .env like I’m using currently is just fine?

@goatandsheep
Copy link
Owner

It should follow dotenv flow priority to some extent https://www.npmjs.com/package/dotenv-flow#variables-overwritingpriority

@goatandsheep
Copy link
Owner

Yes app_env is not necessary for most applications

@hadnet
Copy link
Author

hadnet commented Mar 25, 2022

It should follow dotenv flow priority to some extent https://www.npmjs.com/package/dotenv-flow#variables-overwritingpriority

Thanks again, man. So makes no difference using .env.production, it’s just for merging and priority. So do think that this problem with bundle could be related because I’m using Expo EAS for bundling? Have you ever use eas along with your package?

@goatandsheep
Copy link
Owner

goatandsheep commented Mar 25, 2022

I'm not sure? Try taking a look at this example https://github.com/goatandsheep/chatkitty-example-react-native

@hadnet
Copy link
Author

hadnet commented Mar 25, 2022

I'm not sure? Try taking a look at this example https://github.com/goatandsheep/chatkitty-example-react-native

Mmm, my app is a bare workflow, not managed, have you tried with eas+bare workflow+react-native-dotenv? I’m seeing some people with same issue but no proper answer, so I’m not the only one.

@goatandsheep
Copy link
Owner

I don't know...is it possible to provide a simple example repo so I can figure it out with you?

@stale
Copy link

stale bot commented Jun 4, 2022

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 Jun 4, 2022
@stale stale bot closed this as completed Jun 11, 2022
@Sejmou
Copy link

Sejmou commented May 10, 2023

@hadnet Have you figured out what caused the issue? I think I have the same problem: environment variables are undefined when I install the .apk extracted from an .aab file

@hadnet
Copy link
Author

hadnet commented May 10, 2023

@hadnet Have you figured out what caused the issue? I think I have the same problem: environment variables are undefined when I install the .apk extracted from an .aab file

Sadly, nope! I really wish the library author had tested it with .aab, but unfortunately they didn't or just don't care. But if you manage to, I would really appreciate it if you let me know!

@goatandsheep
Copy link
Owner

Hi @hadnet how does .aab work? Do you have an example repo?

@goatandsheep goatandsheep reopened this May 11, 2023
@stale stale bot removed the wontfix This will not be worked on label May 11, 2023
@github-actions
Copy link

Hey, thank you for opening this issue! 🙂 To boost priority on this issue and support open source please tip the team at https://issuehunt.io/r/goatandsheep/react-native-dotenv/issues/302

@hadnet
Copy link
Author

hadnet commented May 11, 2023

Hi @hadnet how does .aab work? Do you have an example repo?

Sorry, nope. I don't have time now, but you, as the author, could create a simple project, generate the .aab file, and confirm that all envs remain undefined and try to fix this issue.

@Sejmou
Copy link

Sejmou commented May 12, 2023

@goatandsheep For my project, I followed the process described here to create an Android app Bundle (.aab file) locally and extract the .apk from it

I use this to build and install the Android version of my app for personal use without having to publish it to the Play Store

While it's not exactly the same as publishing the app to the Play Store and obtaining the .aab from there, I think that my issue might be similar - environment variables cannot be loaded (I logged them in my app's JavaScript bundle and observed the log output with logcat -> undefined is returned)

Interestingly, environment variables do work fine when sideloading the iOS build of my app onto my iPad

I would be really thankful for any help with rhis problem!

PS: My project uses expo and also makes use of additional native Android/iOS modules. Those need to built with expo prebuild. Not sure if that is related to the problem in any way though.

@tamlyn
Copy link

tamlyn commented May 23, 2023

Sorry, nope. I don't have time now, but you, as the author, could create a simple project, generate the .aab file, and confirm that all envs remain undefined and try to fix this issue.

I appreciate you might be difficult day but talking like this is not cool. Open source maintainers do not owe us anything. I'm sure you didn't mean to cause offence, but we all need to remember that open source is built mostly on passion and maintaining a project takes up a huge amount of time.

Anyway, I thought I had this problem so I made a minimal repro starting from a blank RN project. I published the AAB to to Google Play and installed it and... it worked. Then I realised my problem was elsewhere (it wasn't picking up my .env.development file).

So I can confirm that, at least in some cases, the AAB does contain the env vars.

@hadnet
Copy link
Author

hadnet commented May 23, 2023

If you pay attention, I contributed $2. I'm from a third-world country, and dollars are not something small here. Just so you know, I lost my job because of this issue. So, what you're saying doesn't make any sense. I also help and contribute to the RN community in any way I can. I’m just saying that you need to be more careful when you publish final packages, test all cases like this or at least make a note. Just that.

@goatandsheep
Copy link
Owner

I appreciate any and all contributions @hadnet . Thank you! I have also updated my sample app https://github.com/goatandsheep/react-native-dotenv-expo-test

I am pushing for additional Expo updates but will also update this repo this month

@michaelessiet
Copy link

Getting the same issue here too. For some reason android doesn't apply environment variables. The project I'm a part of will be launching soon and the workaround we found was just to hardcode the values. Everything will have to be changed later on but please a fix would be wonderful

@goatandsheep
Copy link
Owner

@michaelessiet sorry to hear. Can you provide any example code? I haven't been able to create any example projects that didn't work

@stale
Copy link

stale bot commented Sep 17, 2023

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 Sep 17, 2023
@moedeveloper
Copy link

any news on this issue? because I have the same issue in my project :O ? I Can see that the expo build is picking up the values from env but when aab is compiled on android store , the aab file is not picking the values :O ? that wierd!

@stale stale bot removed the wontfix This will not be worked on label Oct 4, 2023
@kriit24
Copy link

kriit24 commented Dec 17, 2023

I have issue "Unable to resolve "@env" from "App.js"" looks like this project is no more maintained

@goatandsheep
Copy link
Owner

@kriit24 no this project is being maintained. I haven't received any code examples of this not working. Is the library installed as devDependency or production dependency?

@goatandsheep
Copy link
Owner

@moedeveloper I'll check again locally to see if the android bundle uses the environment values

@goatandsheep
Copy link
Owner

This is an aab file I generated using the library. Do you see the result?
release.zip

@goatandsheep
Copy link
Owner

@hadnet this template of yours shows react-native-dotenv installed as a production dependency. Please make sure that it is installed as such
https://github.com/hadnet/superb-rn-ts-template/blob/94efa068c38d086f711576c254efa70cd003b6f4/template/package.json

For everyone else, for your builds:

  • clear cache: either api.cache(false) in babel.config.js or --reset-cache
  • If you're using APP_ENV you cannot use development or production
  • Make sure your babel is configured properly with the plugin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💵 Funded on Issuehunt This issue has been funded on Issuehunt
Projects
None yet
Development

No branches or pull requests

7 participants