Skip to content

Conversation

@hai046
Copy link
Contributor

@hai046 hai046 commented Jul 5, 2022

Add a new flag to support config constants pool by json file

The developer configures a json file, which will read all the contents of the configuration into the corresponding variables and parameters,then you can use it in android and iOS projects with custom variables

Scenes to be used:

  • 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
  • You can use project variables directly in android build.gradle
  • You can use environment variables directly in iOS

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",
  "ios_bundle_identifier": "com.xxx.xxxx.nomodify",
  "googleAndroidApiKey": "xxxx",
  "googleAndroidAppId": "1:xxx:android:xxx",
  "googleAndroidMessagingSenderId": "xxx",
  "googleAndroidProjectId": "first-xxx",
  "googleAndroidStorageBucket": "first-xxx.xxx.com",
  "googleAndroidClientId": "xxx-xxxx.apps.googleusercontent.com",
  "googleIosClientId": "com.xxx.apps.xxx-xxx",
  "googleIosBundleId": "com.xxxx.br",
  "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

    buildTypes {
        release {
            manifestPlaceholders = [
                    faceBook_appId       : facebookAppId
            ]

            resValue "string", "facebook_app_id", facebookAppId

…………

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>

Thanks for the correction to the last PR

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@flutter-dashboard flutter-dashboard bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Jul 5, 2022
@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@christopherfujino
Copy link
Contributor

I'm not sure if you missed it in my last message (#105213 (comment)), but I actually think that you should start by first filing an issue for a new feature request (you can re-use a lot of your description from this PR), where we can discuss the approach BEFORE you do any coding. Sorry about the confusion!

Copy link

@titkohanch titkohanch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

root

@makinghappen
Copy link

unfortunately this PR has been partially reverted, making --dart-define-from-file useless for automating builds..

@hai046 hai046 changed the title Add a new flag to support config constants pool by json file Add a new flag to support config constants pool by json file [replace #108098] Jan 9, 2024
@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact "@test-exemption-reviewer" in the #hackers channel in Chat (don't just cc them here, they won't see it! Use Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@hai046 hai046 changed the title Add a new flag to support config constants pool by json file [replace #108098] Add a new flag to support config constants pool by json file [Move to pr #108098] Jan 9, 2024
@hai046 hai046 changed the title Add a new flag to support config constants pool by json file [Move to pr #108098] Add a new flag to support config constants pool by json file Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants