-
Notifications
You must be signed in to change notification settings - Fork 29.2k
Description
Use case
- If
--dart-define
has many definitions, the startup command can be very long and error-prone - It is especially difficult to maintain if there are multiple packages to switch
- Also I want these definitions to be available directly in android and iOS
We counted, our project package includes countries: Brazil, Singapore, Europe, we have a total of 36 configuration items in each region
In my app, different regions and languages use different package names for internationalization. Different package names google-ad, google-service.json, facebook, and firebase have different configurations. It is very cumbersome to switch between definitions. Centralized management and configuration are required to avoid business trips
So I hope :
- Use project variables directly in android build.gradle
- Use environment variables directly in iOS
Proposal
I have a PR ,hope you can give some advice, thanks ! (Because I was not familiar with the process in the early stage, I raised PR first, sorry again)
Add a new flag to support config constants pool by json file
1、Define json file
flutter build apk --define-config-json-file=cn.com.test.pkg.json
You can use flutter run --help
get usage
--dart-define=<foo=bar> Additional key-value pairs that will be available as constants from the String.fromEnvironment, bool.fromEnvironment, int.fromEnvironment, and double.fromEnvironment constructors.
Multiple defines can be passed by repeating "--dart-define" multiple times.
--define-config-json-file The path of a json format file where flutter define a global constant pool. Use `const String.fromEnvironment("DEFINE_CONFIG_JSON_RAW_VALUE")` can get all config json file raw
value. Json entry will be available as constants from the String.fromEnvironment, bool.fromEnvironment, int.fromEnvironment, and double.fromEnvironment constructors;the key is json
filed key,get value is json value.
for example: cn.com.test.pkg.json
content
{
"facebookAppId": "xxx",
"versionName": "1.0.0",
"label": "xxx xxx",
"versionCode": 1,
"android_applicationId": "com.xxx.br",
"googleAndroidApiKey": "xxxx",
"googleAndroidAppId": "1:xxx:android:xxx",
"googleAndroidMessagingSenderId": "xxx",
"schemeHost": "first.xxx.com",
"scheme": "xxxxx",
"privacyProtocol": "https://xxxx.com/privacyProtocol.html",
"teamsOfService": "https://xxx.com/teamsOfService.html",
"androidChannel": "1OSH1PP64X",
"iosChannel": "1J6C22IH91",
"iosUploadCSymS": "false",
"universalLinkDomain": "br.xxx.com"
}
2、Use environment in flutter
You can get all json raw content:
var configRaw =
const String.fromEnvironment("DEFINE_CONFIG_JSON_RAW_VALUE", defaultValue: "{}");
var configRaw = utf8.decode(base64Decode(configRaw));
Or ,you can get field by
var facebookAppId =
const String.fromEnvironment("facebookAppId", defaultValue: "");
3、Android project
file: build.gradle
// This then uses the project variable
buildTypes {
release {
manifestPlaceholders = [
faceBook_appId : facebookAppId
]
resValue "string", "facebook_app_id", facebookAppId
…………
// switch profile
task switchToGoogleServices(type: Copy) {
description = "Switches google-services config,from google-services.${android_applicationId}.json to google-services.json"
println(description)
from "../../precompileConfigs/google-services"
include "google-services.${android_applicationId}.json"
into "./"
rename "google-services.${android_applicationId}.json", 'google-services.json'
}
4、iOS project
file: ios/Flutter/Generated.xcconfig
facebookAppId=xxx
versionCode=1
android_applicationId=com.xxx.br
ios_bundle_identifier=com.xxx.xxxx.nomodify
……
file: ios/Flutter/flutter_export_environment.sh
export "facebookAppId=xxx"
export "versionCode=1"
export "android_applicationId=com.xxx.br"
export "ios_bundle_identifier=com.xxx.xxxx.nomodify"
……
You can use in Info.plist
<key>CFBundleIdentifier</key>
<string>$(ios_bundle_identifier)</string>