FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

how to exclude specific .env files for release build · Issue #76 · java-james/flutter_dotenv · GitHub

Repository navigation

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

Description

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?

Activity

  1. Vedsaga commented on 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).

  2. govarthananve commented on 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

  3. dirablue commented on Jul 5, 2023

    Author

    @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.

  4. dirablue commented on Jul 5, 2023

    Author

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL