react-native-config: Missing .env file in Android Studio

This has been working for some time, but suddenly it has stopped and i’m not sure what’s going on. We have several build variants:

/**
 * Environment configuration
 */
project.ext.envConfigFiles = [
        debug: ".env.dev",
        staging: ".env.production",
        release: ".env.production",
]

apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"

if we do react-native run-android

the project builds fine - it fetched the .env.dev file and everything is fine.

However if I start Android Studio - it immediately complains that a .env file is missing. Which is correct - we don’t have this file. Since sync is not able to run because of that error, we can’t do anything.

If I do add a .env file, then sync runs fine. And after that I see the Build Variants and I can change them. Then I see that the correct dotenv file is being used - either .env.dev or .env.production

I dug around the dotenv.gradle file and I see that when we try to sync our project we end up here

https://github.com/luggit/react-native-config/blob/master/android/dotenv.gradle#L33

but the flavor is empty. So our configuration doesn’t match the flavor and we end up with the default .env file

if I add project.ext.envConfigFiles = [ “”:“.env.dev”, debug: “.env.dev”, staging: “.env.production”, release: “.env.production”, ]

then android studio picks up the .env.dev file as default, but am I supposed to do this? Or is there something else going on? The docs don’t mention anything about that.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 10
  • Comments: 16 (1 by maintainers)

Most upvoted comments

Hmm so for me it turned out to be misconfiguration of project.ext.envConfigFiles - the keys needed to be a combination of the flavor and buildType but in lowercase e.g.:

project.ext.envConfigFiles = [
        devdebug  : ".env.development",
        devrelease  : ".env.development",
        proddebug: ".env.production",
        prodrelease: ".env.production"
]

// ...

    flavorDimensions "environment"
    productFlavors {
        dev {
            dimension "environment"
        }
        prod {
            dimension "environment"
        }
    }
    buildTypes {
        debug {
           /* ... */
        }
        release {
            /* ... */
        }
    }

ok, but we don’t use productFlavors. We have just buildTypes

https://github.com/luggit/react-native-config#advanced-android-setup fixed it for me. Forgot that I needed that after I created an applicationIdSuffix

Instead of adding the defaultEnvFile which mentioned above, I had this error simply because of using a wrong path to my env file. Note that the path should be relative to your project folder, not to the build.gradle .

Check the source code for this error: https://github.com/luggit/react-native-config/blob/63e0e0c7c0e980a5e304f6d8dc08f17d5c19527d/android/dotenv.gradle#L44

The problem for me was that the installation was off, there’s PR with manual Android linking instructions, the last bit we had to add was this:

**MainApplication.java**

 import com.lugg.ReactNativeConfig.ReactNativeConfigPackage;

@Override
protected List<ReactPackage> getPackages() {
     return Arrays.asList(
            new MainReactPackage()
         new ReactNativeConfigPackage()
    );
}

Here's the PR for reference: https://github.com/luggit/react-native-config/pull/303/files