Today, I encountered a problem in a KMM project where the SwiftUI preview didn't work. I joined the project after most of the functionality was complete. I needed to add some UI components on the iOS side, but all preview configurations for views failed, and I received an error message in the left panel of Xcode.
I could proceed without the preview display, but it was highly inefficient, especially for someone new to SwiftUI. I began investigating the problem. The key issue was that I needed some classes from a shared module, but they weren't recognized by Xcode. I looked deeper into the Gradle configuration for the shared part. The solution involved changing the static
parameter from true
to false
.
iosTarget.binaries.framework {
baseName = "Shared"
isStatic = true
}
So, what is the role of this, and how does it affect the SwiftUI preview? After googling and conversing with various LLMs, I found out that it determines whether the framework will be a static or a dynamic framework.
- Static linking is used: All code from the framework is linked into the final app binary at compile time. This avoids the need for dynamic loading at runtime.
- Dynamic linking is used: The framework is included as a separate .framework file in the app bundle and is loaded into memory at runtime.
- Dynamic frameworks are loaded at runtime, which aligns well with how SwiftUI previews function. Previews are executed in a separate runtime environment (a simulator process) and rely on dynamic loading of the compiled code to render live updates.
As a result, I set static = false
while developing new functionality and change it to static = true
when building and distributing applications. I hope this information will help you save several hours if you encounter the same issue.
You can find more useful content on my LinkedIn page, on X, in Medium or Mastodon.
Top comments (0)