react-native-dotenv icon indicating copy to clipboard operation
react-native-dotenv copied to clipboard

Android bundle can't read .env file

Open hadnet opened this issue 3 years ago • 33 comments

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

hadnet avatar Mar 25 '22 17:03 hadnet

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

github-actions[bot] avatar Mar 25 '22 17:03 github-actions[bot]

An anonymous user has funded $2.00 to this issue.


issuehunt-oss[bot] avatar Mar 25 '22 17:03 issuehunt-oss[bot]

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.

hadnet avatar Mar 25 '22 17:03 hadnet

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

goatandsheep avatar Mar 25 '22 18:03 goatandsheep

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"
        }
    }

hadnet avatar Mar 25 '22 20:03 hadnet

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

goatandsheep avatar Mar 25 '22 20:03 goatandsheep

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

hadnet avatar Mar 25 '22 21:03 hadnet

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?

hadnet avatar Mar 25 '22 21:03 hadnet

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

goatandsheep avatar Mar 25 '22 22:03 goatandsheep

Yes app_env is not necessary for most applications

goatandsheep avatar Mar 25 '22 22:03 goatandsheep

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?

hadnet avatar Mar 25 '22 22:03 hadnet

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

goatandsheep avatar Mar 25 '22 22:03 goatandsheep

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.

hadnet avatar Mar 25 '22 22:03 hadnet

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

goatandsheep avatar Mar 27 '22 05:03 goatandsheep

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[bot] avatar Jun 04 '22 01:06 stale[bot]

@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

Sejmou avatar May 10 '23 16:05 Sejmou

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

hadnet avatar May 10 '23 17:05 hadnet

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

goatandsheep avatar May 11 '23 17:05 goatandsheep

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

github-actions[bot] avatar May 11 '23 17:05 github-actions[bot]

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.

hadnet avatar May 11 '23 20:05 hadnet

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

Sejmou avatar May 12 '23 00:05 Sejmou

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.

tamlyn avatar May 23 '23 09:05 tamlyn

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.

hadnet avatar May 23 '23 17:05 hadnet

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

goatandsheep avatar May 23 '23 19:05 goatandsheep

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

michaelessiet avatar Jun 29 '23 01:06 michaelessiet

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

goatandsheep avatar Jun 29 '23 02:06 goatandsheep

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[bot] avatar Sep 17 '23 01:09 stale[bot]

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!

moedeveloper avatar Oct 04 '23 15:10 moedeveloper

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

kriit24 avatar Dec 17 '23 04:12 kriit24

@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 avatar Feb 10 '24 20:02 goatandsheep