jonbhanson / flutter_native_splash

Automatically generates native code for adding splash screens in Android and iOS. Customize with specific platform, background color and splash image.
https://pub.dev/packages/flutter_native_splash
MIT License
1.36k stars 215 forks source link

Flavors still bundling all storyboard files on release #701

Closed cabaucom376 closed 5 months ago

cabaucom376 commented 6 months ago

I have 3 flavors setup in my iOS Flutter project and I have properly got the flavored splash screens to work appropriately image

I noticed when I ran a build size analysis (flutter build ios --analyze-size -t lib/main_prod.dart --flavor production) that its still bundling all storyboard files:

Xcode build done.                                           150.3s
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  Runner.app                                                               69 MB
  Runner.app/
    _CodeSignature/
      CodeResources                                                        20 KB
    AppIcon60x60@2x.png                                                    32 KB
    Runner                                                                109 KB
    Base.lproj/
      LaunchScreenProduction.storyboardc                                    4 KB
      Main.storyboardc                                                      3 KB
      LaunchScreenStaging.storyboardc                                       4 KB
      LaunchScreenDevelopment.storyboardc                                   4 KB
...

Any guidance on how to strip these?

jonbhanson commented 6 months ago

@vlazdra can you look at this?

vlazdra commented 6 months ago

Hey @jonbhanson and @cabaucom376! 👋

As far as I'm aware, it's not possible to define which file is included in which schema build. It's something that needs to be handled manually by the developer.

I asked ChatGPT if it can help, and it came up with this solution. By manually adding/removing the file from the bundle you are including/excluding the flavour storyboard that has nothing to do in that bundle.

Will test this out on my project as well. Please let me know if this helps.


Using Build Phases to Manage Storyboards in Xcode

Step 1: Edit the Build Phases

  1. Open Your Project:

    • Open your project in Xcode and select your project in the Project Navigator.
  2. Select the Target:

    • Choose the target for which you want to configure the build phases.
  3. Go to Build Phases:

    • Click on the Build Phases tab in the project settings.

Step 2: Modify the Copy Bundle Resources Phase

  1. Locate Copy Bundle Resources:

    • In the Build Phases tab, find the Copy Bundle Resources phase. This phase is responsible for copying resource files like storyboards, images, and other assets into the app bundle.
  2. Remove Unwanted Storyboards:

    • Select the storyboard files that you do not want to include in this target.
    • Click the - button to remove them from the Copy Bundle Resources list.

Step 3: Add a Run Script Phase (Optional but Recommended)

If you want to automate the inclusion/exclusion of specific storyboards based on the build configuration (e.g., Debug or Release), you can add a custom Run Script Phase.

  1. Add a New Run Script Phase:

    • Click the + button at the top of the Build Phases tab and select New Run Script Phase.
  2. Insert the Script:

    • Drag the new Run Script Phase to just before the Copy Bundle Resources phase to ensure it runs before resources are copied.
    • In the script editor, enter a script that removes the unnecessary storyboard files based on the build configuration.

Here is an example script that removes specific storyboard files for different build configurations:

# Define the storyboard names to be excluded for each configuration
DEBUG_STORYBOARD="Main.storyboardc"
RELEASE_STORYBOARD="AltMain.storyboardc"

# Path to the resource directory
RESOURCE_DIR="${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}"

# Remove storyboards based on the build configuration
if [ "$CONFIGURATION" == "Debug" ]; then
    echo "Removing $RELEASE_STORYBOARD for Debug build"
    rm -rf "$RESOURCE_DIR/$RELEASE_STORYBOARD"
elif [ "$CONFIGURATION" == "Release" ]; then
    echo "Removing $DEBUG_STORYBOARD for Release build"
    rm -rf "$RESOURCE_DIR/$DEBUG_STORYBOARD"
fi

Step 4: Adjust Build Settings

  1. Set Info.plist for Each Configuration:
    • Ensure that the Info.plist file for each build configuration points to the correct storyboard.
    • You can do this by setting the UIMainStoryboardFile key in the Info.plist for each build configuration.

Example Scenario

Imagine you have two storyboards: Main.storyboard and AltMain.storyboard. You want Main.storyboard to be used for Debug builds and AltMain.storyboard for Release builds.

  1. In the Copy Bundle Resources phase:

    • Remove AltMain.storyboard from the list for the Debug target.
    • Remove Main.storyboard from the list for the Release target.
  2. Add the Run Script Phase:

    • Add a script that removes the storyboard not needed for the current build configuration.
  3. Set Info.plist:

    • Make sure the Info.plist for the Debug configuration points to Main.storyboard.
    • Make sure the Info.plist for the Release configuration points to AltMain.storyboard.

By carefully configuring the build phases and using a script to manage resource inclusion, you can streamline your build process and ensure that only the necessary storyboards are included in your final app builds.


jonbhanson commented 5 months ago

@cabaucom376 based on your thumbs up, I assumed this helped you and close the ticket.