java-james / flutter_dotenv

Loads environment variables from `.env`.
https://pub.dartlang.org/packages/flutter_dotenv
MIT License
220 stars 47 forks source link

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

Open dirablue opened 1 year ago

dirablue commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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/