Swift Packages: exclude developments resources/assets from package used in archived app.

When using Swift Packages in Xcode, is there a way to exclude files conditionally (in DEBUG or similar)? I know I can exclude files but I want them while in development.

I have a package that contains developments resource/assets (used to seed a Core Data database for the simulator + for Previews, with images and files), but I don't want to include these files in the package used by the real app when archiving.

Can we achieve that?

Replies

No there is not currently a great way to do this.

You could try adding two new packages:

  1. A package containing your development code and assets only
  2. A package with dependencies on your new development assets package and your original package

For your debug build, build this new root package from #2. And for the release build, build just the original package.

You can even add your preview providers to package #1, pin the preview, and then continue editing in the original package to get all the benefits of previews, but still putting preview providers and such in the other package.

And if you only have development code (and not assets), you could always just surround that code with #if DEBUG

Thanks! Sounds to answer my question. I'll make sure to add the #if DEBUG to my preview providers in packages that don't use any assets (and in the app too). I didn't know this could be used in packages too (where is the compiler flag defined for the package, like it is for a project with Active Compilation Conditions?).

For the packages that need assets, I'm not sure I correctly understand:

For your debug build, build this new root package from #2. And for the release build, build just the original package.

Can you tell me how to use either the package from #2 or the original package in the app? In the app target, I have to import the packages needed. Do I have to create a new app target so I can import either package from #2 or the original package?