lugg / react-native-config

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

Missin .env file, when building from android studio #675

Open mak12 opened 1 year ago

mak12 commented 1 year ago

I am using mac m1

run this command

ENVFILE=.env.qa react-native run-android --variant=qa_debug

It works fine i am able to get variables from config.

But when I try to rum same variant from android studio it fails with error

Reading env from: .env
**************************
*** Missing .env file ****
**************************

I have tried almost all the solution available, here are my settings

project.ext.envConfigFiles = [ prod_debug: ".envProd", prod_release: ".envProd", qa_debug: ".env.qa", qa_release: ".env.qa", ]

defaultConfig {
         versionCode 1
         versionName "1.0"
         resValue "string", "build_config_package", "com.myPackageName" 

And my falvours

productFlavors {
        qa_ {
            applicationId "com.myPackage.qa"
            dimension "version"
            versionCode 1
            versionName "1.0"
        }
        prod_ {
            applicationId "com.myPackage"
            dimension "version"
            versionCode 1
            versionName "1.0"

        }
    }

Problem is when building with Android studio, and because of pipelines i have to keep my andorid studio setup good and working because builds are made from native android gradle code instead of package.json.

BoilingOil commented 1 year ago

Here's how to set the default env file, add these in app/build.gradle:

For example, I have three different builds with three different files, and my default is dev.

project.ext.defaultEnvFile = "../__scripts__/.env.dev"

project.ext.envConfigFiles = [
    dev: "../__scripts__/.env.dev",
    qa: "../__scripts__/.env.qa",
    stable: "../__scripts__/.env.stable",
]

It checks for this, and if it doesn't find it, it sets a default of ".env"

mak12 commented 1 year ago

@BoilingOil my problem is not about default .env file, its about when i rum build from cli its works fine but when i run from android studio i am not able to receive constants

BoilingOil commented 1 year ago

https://github.com/luggit/react-native-config/pull/599 Is about iOS, and you're building Android. That issue has nothing to do with your issue.

If you don't have a file called .env file in your project, then it is precisely your problem.

Observe: you set the default environment on command line, but $ENVFILE is not automagically set when running builds from Android Studio.

looking at this file in the codebase: https://github.com/luggit/react-native-config/blob/master/android/dotenv.gradle

Start at line 24, but line 26 is particularly interesting. It says:

    def envFile = project.hasProperty("defaultEnvFile") ? project.defaultEnvFile : ".env"

Which means by default it will look for a file named ".env" at the root of your project.

So, it's either use a file named .env that the library is looking for by default instead of whatever you renamed them to, or tell the library you renamed the file by setting the defaultEnvFile to the new file name and location like in my example.

Then you'll be able to build from command line and Android Studio and get the same results.

If you want to see configs in release builds, make sure this line is in your proguard-rules.pro file:

-keep class com.ea.gp.brooklyn.BuildConfig { *; }

Good luck to you...

tungduonghgg123 commented 1 year ago

answer from @BoilingOil works for me. Just setting the defaultEnvFile on your gradle.properties should do the job defaultEnvFile=.env.dev