Skip to content

Instantly share code, notes, and snippets.

@gdelataillade
Created January 5, 2023 15:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdelataillade/68834caacdd6727f1418e46788f70b53 to your computer and use it in GitHub Desktop.
Save gdelataillade/68834caacdd6727f1418e46788f70b53 to your computer and use it in GitHub Desktop.
How to import your assets to Xcode without adding weight to your Flutter app

In some cases, you will need to import your assets to Xcode so native code can read it.

How to import your assets to Xcode

You will have to follow these steps:

  • In Xcode, open the ios/Runner.xcworkspace of your Flutter project and navigate to the Runner folder.
  • Drag and drop the asset file into the Runner folder. This will copy the asset file into the main bundle and create a reference to it in your project's project.pbxproj file.
  • If asked, make sure you select the options "Copy items if needed" and "Create groups". In "Add to targets", select your app Runner.

Here are two screenshots of the steps to help you:

Drag and drop asset in Runner Add reference to Xcode project

But now I have two copies of my asset...

That's right, you have one version of your asset probably in your assets folder, and now another one in your ios/Runner folder. That's not really a good thing since this could add significant weight to your app. Keep reading to see the solution.

The solution

The workaround I found uses symbolic links. A symbolic link creates a file in your directory and acts as a shortcut to a file or folder. This way, there is only one asset stored in the project, and the other become just a light-weight link.

First, you can delete your asset in the ios/Runner folder with this command. This will keep the reference to the file though.

rm ios/Runner/my_asset.mp3

Then, create the symbolic link like this:

ln -s assets/audio/my_asset.mp3 ios/Runner/my_asset.mp3

That's it !

If you run the command ls -l in you Runner folder, you will see that the symbolic link is around 36 bytes only:

Screenshot 2023-01-05 at 15 54 15

@awadhesh1
Copy link

Greate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment