Not planned
Description
Problem:
When I run the ./gradlew assembleStagingRelease command, the build process uses the default .env
file instead of the .env.staging
file.
Question:
Is there a way to make the ./gradlew assembleStagingRelease
command use the .env.staging
file for building with the react-native-dotenv
package?
Additional Context:
I am using a third-party CI/CD tool that does not support setting environment variables in the build command, so I need a solution that works directly with Gradle.
Current Setup:
- babel.config.js:
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
[
'module:react-native-dotenv',
{
moduleName: '@env',
path: '.env',
blocklist: null,
allowlist: null,
safe: false,
allowUndefined: true,
verbose: false,
},
],
],
}
- android/app/build.gradle:
android {
...
flavorDimensions "env"
productFlavors {
dev {
dimension "env"
}
staging {
dimension "env"
versionNameSuffix "-staging"
}
preprod {
dimension "env"
versionNameSuffix "-preprod"
}
prod {
dimension "env"
versionNameSuffix "-prod"
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
signingConfig signingConfigs.debug
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
applicationVariants.all { variant ->
variant.outputs.all { output ->
def versionName = variant.versionName
def flavor = variant.flavorName
def buildType = variant.buildType.name
def newApkName = "app-v${versionName}-${flavor}-${buildType}.apk"
def newAabName = "app-v${versionName}-${flavor}-${buildType}.aab"
if (output.outputFile.name.endsWith('.apk')) {
outputFileName = newApkName
} else if (output.outputFile.name.endsWith('.aab')) {
outputFileName = newAabName
}
}
}
}
Activity
github-actions commentedon Jan 31, 2025
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/515
phubner commentedon Jan 31, 2025
I think per the documentation you can do
APP_ENV=staging ./gradlew assembleStagingRelease
. Note I haven't run this, but I believe this is the intent.aokdev06 commentedon Jan 31, 2025
Hi @phubner, thanks for your response.
Normally, I can successfully build on my local device using the following command:
"android:staging-release": "APP_ENV=staging npx react-native build-android --mode=release --tasks assembleRelease"
However, the CI/CD tool I’m using supports productFlavors methods for Android. When I specify StagingRelease as the release method, it runs
./gradlew assembleStagingRelease
. Because of this, I can’t prepend the command withAPP_ENV
.Still, I appreciate your help.
phubner commentedon Jan 31, 2025
I see, sorry for the confusion @aokdev06. I am looking for a clean solution to this as well.