Skip to content

Conversation

@hai046
Copy link
Contributor

@hai046 hai046 commented Jun 2, 2022

Add a function: 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

1、Define json file

flutter build apk  --dart-define=CUSTOM_ENVIRONMENT_FILE=config.json 

for example: config.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("CUSTOM_ENVIRONMENT_FILE_RAW", defaultValue: "{}");

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

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>

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.

hai046 added 2 commits June 2, 2022 18:53
Add a function: the developer configures a properties file, which will read all the contents of the configuration into the corresponding variables and parameters
Add a function: the developer configures a properties file, which will read all the contents of the configuration into the corresponding variables and parameters
@flutter-dashboard flutter-dashboard bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Jun 2, 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.

@google-cla
Copy link

google-cla bot commented Jun 2, 2022

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@cedvdb
Copy link
Contributor

cedvdb commented Jun 2, 2022

why json ? It seem unnecessarily verbose.

@hai046
Copy link
Contributor Author

hai046 commented Jun 2, 2022

why json ? It seem unnecessarily verbose.

  • Type definitions are obvious by use bool.fromEnvironment、int.fromEnvironment、stirng.fromEnvironment
  • friendly formatting and format check

@christopherfujino
Copy link
Contributor

This seems like a reasonable new feature. However, I think you should start by filing an issue as a new feature request, and then we should discuss the approach from there (for example, I think this should be a new flag, not a magic string parsed from --dart-define).

Thank you for contributing and hope to see you on the feature request!

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.

3 participants