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.
I have this issue since upgrading my gradle from 3.1.4 and RN from 0.54 to 3.2.1 and RN 0.57.5
https://github.com/luggit/react-native-config#advanced-android-setup fixed it for me. Forgot that I needed that after I created an applicationIdSuffix
@dmoss18 - I don't understand wha tyou mean. Why would I need applicationIdSuffix? I never used this before?
Upgrading gradle is the issue for me. The way the react-native-config script works doesn't support getting build flavor using gradle 3.2. I haven't found a solution for this, yet.
Most probably same for me.
I'm looking at this code:
def getCurrentFlavor() {
Gradle gradle = getGradle()
// match optional modules followed by the task
// (?:.*:)* is a non-capturing group to skip any :foo:bar: if they exist
// *[a-z]+([A-Za-z]+) will capture the flavor part of the task name onward (e.g., assembleRelease --> Release)
def pattern = Pattern.compile("(?:.*:)*[a-z]+([A-Z][A-Za-z]+)")
def flavor = ""
println("gradle: ${gradle.getStartParameter().getTaskNames()}")
gradle.getStartParameter().getTaskNames().any { name ->
Matcher matcher = pattern.matcher(name)
if (matcher.find()) {
flavor = matcher.group(1).toLowerCase()
return true
}
}
return flavor
}
println("gradle: ${gradle.getStartParameter().getTaskNames()}") - return empty array the first time.
If I add a .env file it goes past the first step, but after that it seems that it doesn't properly pick the
project.ext.envConfigFiles = [
debug: ".env.dev",
staging: ".env.dev",
release: ".env.production",
]
configuration.
We have the following build types
buildTypes {
debugEU {
debuggable true
jniDebuggable false
renderscriptDebuggable true
renderscriptOptimLevel 3
minifyEnabled false
zipAlignEnabled true
signingConfig signingConfigs.release
matchingFallbacks = ['debug']
}
release {
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
signingConfig signingConfigs.release
}
releaseEU {
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
signingConfig signingConfigs.release
matchingFallbacks = ['release']
}
staging {
debuggable true
jniDebuggable false
renderscriptDebuggable true
renderscriptOptimLevel 3
minifyEnabled false
zipAlignEnabled true
signingConfig signingConfigs.release
matchingFallbacks = ['release']
}
}
I can see them as Build Variants in android studio, but when I select staging - I see that it's reading the .env.production config instead of the .env.dev config.
Also seeing this on gradle v4.7.
The following seems to be ignored:
project.ext.envConfigFiles = [
/* ... */
]
And when I run e.g. react-native run-android --variant=devDebug I see the following error:
Reading env from: .env
**************************
*** Missing .env file ****
**************************
Is this perhaps something to do with the deprecation of --flavor in favour of --variant?
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
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
return Arrays.asList(
new MainReactPackage()
new ReactNativeConfigPackage()
);
}
Here's the PR for reference: https://github.com/luggit/react-native-config/pull/303/files
I faced this when gradle task run except for assemble (in my case running sourceSets for debug purpose). This problem is caused by what tasks are run since gradle.getStartParameter().getTaskNames() get current task name in the process. So this would extract string buildValiantSuffix rather than flavor. This library is fragile now because this config depends on only what tasks called and doesn't support except for flavor completely matched camel case task name and original task name string must be all lowercase because the flag on pattern determined by startsWith.
As I thought it, I want to handle additional two case, ~It should be fixed by flavor.endsWith instead of startsWith. This should be worked in the case original task name has camel case even if flavor added task.~
I found there are cases of intermediate injected buildVariantConfigName so I avoid it includes.
In another case, task name isn't added buildValiantSuffix but this task may call dotenv.gradle in the process like sourceSets. So, we need to avoid this, and maybe it is appropriate to enable switch defaultEnvFile even when project.ext.envConfigFiles exists.
So I'll pull request today if new version release coming. If I wrong something, please feel easy to tell me.
you need to add defaultEnvFile property
I needed to add defaultEnvFile too, when running ENVFILE=xxx ./gradlew assembleRelease the env is NOT read correctly. So I had to create an intermediate script that copies my env to a temporary location read by defaultEnvFile. Lame. lame. lame.
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
Most helpful comment
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.: