Flavor Getter
Flavor Getter is a Flutter plugin that helps developers retrieve the current flavor (or variant) their application is running in directly from their Flutter code. This plugin currently supports iOS and Android platforms.
Installation
To use the Flavor Getter plugin in your Flutter project, follow these steps:
- Add the following dependency to your project's
pubspec.yaml
file:
dependencies:
flavor_getter: ^0.0.3
Replace ^0.0.3
with the desired version of the Flavor Getter plugin.
- Run the following command to fetch the dependencies:
flutter pub get
- Import the Flavor Getter package in your Dart code:
import 'package:flavor_getter/flavor_getter.dart';
Getting Started
To use Flavor Getter in your Flutter project, you need to perform some setup steps depending on the platform you are targeting.
Platform Support
Platform | Support |
---|---|
iOS | ✅ |
Android | ✅ |
Disclaimer
Please note that this readme assumes that you have already set up the flavors (or variants) for your iOS and Android projects. The instructions provided here focus on integrating the Flavor Getter plugin into your existing flavor setup. If you haven't set up flavors yet, please refer to the respective platform documentation to configure flavors for your project.
iOS Setup
- Open your Flutter project in Xcode by navigating to
ios/Runner.xcworkspace
. - In Xcode, select your application target (Runner) and go to the Build Settings tab.
- Under the User-Defined section, add a new build setting called
FLAVOR
and set it to the desired flavor value for each scheme. - Open the
Info.plist
file located in theios/Runner
directory. - Add a new entry to the Information Property List with the key
FLAVOR
and the value$(FLAVOR)
. This will ensure that the flavor value is accessible in the Info.plist file. - Save the
Info.plist
file. - Build and run your application using the desired scheme and flavor.
Android Setup
- Open your Flutter project in Android Studio.
- Locate the
android/app/build.gradle
file. - Inside the
android
block, ensure that thedefaultConfig
section contains the following line:
android {
namespace "com.example.myapp" // Replace with your namespace
// ...
defaultConfig {
// ...
applicationId "com.example.myapp" // Replace with your applicationId; make sure it's similar to namespace
// ...
}
}
The applicationId should match the package name defined in your AndroidManifest.xml
file.
- To add flavors, add the following lines to the android block:
flavorDimensions "default"
productFlavors {
dev {
applicationIdSuffix ".dev" // com.example.myapp.dev
}
prod {
applicationIdSuffix ".prod" // com.example.myapp.prod
}
}
Customize the flavor names (dev and prod) and the applicationIdSuffixes (.dev and .prod) to match your desired flavors.
- When Proguard is enabled, which is the default for Android release builds, the
BuildConfig
Java class is renamed in the minification process and prevents the library from referencing it. To avoid this, add a rule inandroid/app/proguard-rules.pro
:
-keep class com.example.yourPackageName.BuildConfig { *; }
The package name should match the android:label
value in your app/src/main/AndroidManifest.xml
file.
Usage
To retrieve the current flavor in your Flutter code, you can use the getFlavor()
function.
import 'package:flavor_getter/flavor_getter.dart';
void main() async {
String currentFlavor = await FlavorGetter().getFlavor();
print('Running in flavor: $currentFlavor');
}
Example
For a complete example of using Flavor Getter, please refer to the example directory of this plugin.
Issues and Contributions
Please report any issues or bugs you encounter by opening an issue on the GitHub repository. Contributions are also welcome via pull requests.