SwiftUI Previews and Static Library #5318
-
I am trying to use the 'micro features' architecture and to use as many static libraries as I can to minimise the dynamic linking. This is working perfectly fine so far for "logic-only" modules or ones that use UIKit. However, I am hitting issues with creating a module that uses SwiftUI due to SwiftUI Previews. Previews aren't supported in static libraries or static frameworks. I have tried working around this by creating a 'dynamic framework' target that includes all of the sources and resources. However, I have issues trying to use resources that are connected to the static library using a resource bundle. I tried to workaround this by connecting the resources to the dynamic framework module used for previews and then having a typealias to use the correct resource synthesizer when building the static library or when building the dynamic framework. This allowed the targets to build and for the preview to at least load, however, it simple displays a black screen that has a red X and the text 'Preview crashed'. Making the SwiftUI view not use any resources and just hard-coded strings allows the preview to load via the dynamic framework scheme, showing that accessing resources is the issue. Does anyone have a working example or know how to use static libraries in conjunction with SwiftUI and SwiftUI Previews? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @lordcodes We ran into a similar issue 😞 From our investigation it seems the build process for Previews is excluding Resource bundle targets all together resulting in them being missing and any access to them subsequently failing. One approach we're experimenting with is for development generating a workspace that is slightly different than for regular builds, one where all targets are dynamic (while keeping them static for regular builds). In Tuist, you can leverage Generation Time Configuration to aid this, for example TUIST_PREVIEWS=1 tuist generate And in the manifests: func productType() -> ProductType {
if Environment.previews.getBoolean(default: false) {
return .framework
}
return .staticFramework
} Another option may be available when Xcode 15 is released, as mergeable libraries promise to achieve a similar purpose, but requires a bit more exploration especially on how to integrate with Tuist. Hope this helps! |
Beta Was this translation helpful? Give feedback.
Hi @lordcodes
We ran into a similar issue 😞
From our investigation it seems the build process for Previews is excluding Resource bundle targets all together resulting in them being missing and any access to them subsequently failing.
One approach we're experimenting with is for development generating a workspace that is slightly different than for regular builds, one where all targets are dynamic (while keeping them static for regular builds).
In Tuist, you can leverage Generation Time Configuration to aid this, for example
And in the manifests: