goatandsheep / react-native-dotenv

Load react native environment variables using import statements for multiple env files.
https://www.npmjs.com/package/react-native-dotenv
MIT License
813 stars 48 forks source link

Android bundle can't read .env file #302

Open hadnet opened 2 years ago

hadnet commented 2 years ago

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!](https://issuehunt.io/r/goatandsheep/react-native-dotenv/issues/302) #### [Or submit a pull request to get the deposits!](https://issuehunt.io/r/goatandsheep/react-native-dotenv/issues/302) ### Tips - Checkout the [Issuehunt explorer](https://issuehunt.io/r/goatandsheep/react-native-dotenv/) to discover more funded issues. - Need some help from other developers? [Add your repositories](https://issuehunt.io/r/new) on IssueHunt to raise funds.
github-actions[bot] commented 2 years ago

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[bot] commented 2 years ago

An anonymous user has funded $2.00 to this issue.


hadnet commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

hadnet commented 2 years ago

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

hadnet commented 2 years ago

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 commented 2 years ago

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

goatandsheep commented 2 years ago

Yes app_env is not necessary for most applications

hadnet commented 2 years ago

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 commented 2 years ago

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

hadnet commented 2 years ago

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 commented 2 years ago

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

stale[bot] commented 2 years ago

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.

Sejmou commented 1 year ago

@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 commented 1 year ago

@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 commented 1 year ago

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

github-actions[bot] commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@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[bot] commented 1 year ago

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.

moedeveloper commented 1 year ago

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!

kriit24 commented 11 months ago

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

goatandsheep commented 9 months ago

@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 commented 9 months ago

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

goatandsheep commented 9 months ago

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

goatandsheep commented 9 months ago

@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: