首页 > 解决方案 > Change bundle id and name depending on dart-define argument

问题描述

The general goal is to have two copies of the application installed at the same time, but with different names.

One for the production environment, the other for the test environment.

The environment is NOT determined by the build type (release/debug/profile), but by the value of the -dart-define=myEnv="production/test" variable. And all builds making with --release mode in the ci/cd system and go on a Testflight.

If I understand correctly, this requires changing the App ID.

I have two info.plist files:

ios/Runner/Info.plist:

<key>CFBundleIdentifier</key>
<string>com.testcomp.testapp</string>

ios/Runner/GoogleService-Info.plist

<key>BUNDLE_ID</key>
<string>com.testcomp.testapp</string>

and also the application identifier is specified in the file: ios/Runner.xcodeproj/project.pbxproj as:

PRODUCT_BUNDLE_IDENTIFIER = com.testcomp.testapp;

How can I change the Bundle id and Bundle name of the application based on the --dart-define variable?

What I have tried:

Although this script is based on the release/debug parameter, I still tried to add this script to the Build phases:

if [ "${CONFIGURATION}" = "Debug" ]; then
/usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier com.testcomp.testapp.test" "$PROJECT_DIR/Runner/Info.plist"
/usr/libexec/PlistBuddy -c "Set :CFBundleName testapp-beta" "$PROJECT_DIR/Runner/Info.plist"
/usr/libexec/PlistBuddy -c "Set :CFBundleDisplayName testapp-beta" "$PROJECT_DIR/Runner/Info.plist"
/usr/libexec/PlistBuddy -c "Set :BUNDLE_ID com.testcomp.testapp.test" "$PROJECT_DIR/Runner/GoogleService-Info.plist"
echo "Changed bundle id and name for Debug"
else
/usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier com.testcomp.testapp" "$PROJECT_DIR/Runner/Info.plist"
/usr/libexec/PlistBuddy -c "Set :CFBundleName testapp" "$PROJECT_DIR/Runner/Info.plist"
/usr/libexec/PlistBuddy -c "Set :CFBundleDisplayName testapp" "$PROJECT_DIR/Runner/Info.plist"
/usr/libexec/PlistBuddy -c "Set :BUNDLE_ID com.testcomp.testapp" "$PROJECT_DIR/Runner/GoogleService-Info.plist"
echo "Changed bundle id and name for PRODUCTION"
fi

but the behaviour was strange:

the first attempt to build after a complete cleanup occurs without error. But the installed application has the old ID and name. The second attempt to build installs the second app with a changed ID and name and a second shortcut appears on the device, but then the Xcode crashes with the following message: enter image description here

I think that this is due to the fact that the script is editing info.plist files, but flutter store the identifier directly into the ios/Runner.xcodeproj/project.pbxproj file, and I could not change it with the script.

I also moved this script to the very top of the Build phases, and also moved it to the Product->Scheme->Edit Scheme...->Build->Pre-actions. This did not resolve the error.

But again, I need to change Bundle ID and name not depending on the build mode (--release or debug), but from the value of a dart-define variable.

Flutter version: 1.22

标签: iosflutter

解决方案


推荐阅读