Skip to content

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

Open
dirablue opened this issue Dec 13, 2022 · 4 comments
Open

how to exclude specific .env files for release build #76

dirablue opened this issue Dec 13, 2022 · 4 comments

Comments

@dirablue
Copy link

dirablue commented Dec 13, 2022

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.

assets:
  - .env.dev
  - .env.stg
  - .env.prd

but only .env.prd is needed in the build file for release build ( for prd )
Is it possible to control these files?

@Vedsaga
Copy link

Vedsaga commented May 2, 2023

Answer from Chat-GPT

@dirablue

To 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:

Open the build.gradle file in an editor.

Add the following code inside the android block:
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).

@govarthananve
Copy link

govarthananve commented May 13, 2023

Don't forget to add assets for the env files in pubspec.yaml

assets:
    - .env
    - .env.dev
    - .env.stg
    - .env.prd

Create "environment.dart" file any whare in project

import 'dart:core';

import 'package:flutter_dotenv/flutter_dotenv.dart';

class Environment {
  static const env = String.fromEnvironment('ENV');
   String get envFileName {
    switch (env) {
      case 'dev': // flutter run --dart-define=ENV=dev
        return '.env.dev';
      case 'stg': // flutter run --dart-define=ENV=stg
        return '.env.stg';
      case 'prd': // flutter run --dart-define=ENV=prd
        return '.env.prd';
      default:
        return '.env';
    }
  }
}

In main.dart file

import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:sos/src/utils/environment.dart';
Future<void> main() async {
  if (kDebugMode) {
    print(Environment.envFileName);
  }
  await dotenv.load(fileName: Environment.envFileName);
  runApp(const MyApp());
}

While running the project and taking build time you need to use --dart-define=ENV=prd
flutter run --dart-define=ENV=prd
That's It It will work as you expect

@dirablue
Copy link
Author

dirablue commented Jul 5, 2023

@Vedsaga
Thank you for telling your solution.
I wanted how to do on only dart / flutter. but maybe it's impossible?

@govarthananve
Thank you for your comment.
But it's not solution for that thing. because all of env files will be saved in assets of flutter.

the reason I wanted to exlude env files is for the security.

@dirablue
Copy link
Author

dirablue commented Jul 5, 2023

I found the best solution.

using obfuscated env source code isntead of .env file with https://pub.dev/packages/envied.
https://codewithandrea.com/articles/flutter-api-keys-dart-define-env-files/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants