-
Notifications
You must be signed in to change notification settings - Fork 52
how to exclude specific .env files for release build #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Answer from Chat-GPTTo exclude the .env file only for release builds in a Flutter project, you can modify your build.gradle file located in the android/app directory of your project. Here are the steps to do so:
android {
// ...
buildTypes {
release {
resValue("string", "dotenv_filename", "")
}
}
// ...
} This code sets a resource value named dotenv_filename to an empty string for the release build type. Save the file and close the editor. In your Dart code, modify the load method call to load the .env file only if the dotenv_filename resource value is not empty: import 'package:flutter_dotenv/flutter_dotenv.dart';
Future<void> main() async {
// Load the environment variables from the .env file
if (dotenv.env['dotenv_filename'] != '') {
await dotenv.load();
}
// Use the environment variables in your code
final apiKey = dotenv.env['MY_API_KEY'];
final apiUrl = dotenv.env['API_BASE_URL'];
// ...
} This code checks if the dotenv_filename resource value is not empty before loading the .env file. With these modifications, the .env file will be excluded only for the release build type, and will be loaded for all other build types (e.g. debug, profile). |
Don't forget to add assets for the env files in pubspec.yaml
Create "environment.dart" file any whare in project
In main.dart file
While running the project and taking build time you need to use |
@Vedsaga @govarthananve the reason I wanted to exlude env files is for the security. |
I found the best solution. using obfuscated env source code isntead of .env file with https://pub.dev/packages/envied. |
Uh oh!
There was an error while loading. Please reload this page.
Hi
I would like to know how to exclude .env files for only release build.
ex)
when I use local build, all of envs are needed for testging.
but only .env.prd is needed in the build file for release build ( for prd )
Is it possible to control these files?
The text was updated successfully, but these errors were encountered: