DEV Community

Cover image for SwiftUI Previews don't Work for Kotlin Multiplatform Mobile (KMM) Projects.
Kiolk
Kiolk

Posted on

2 1

SwiftUI Previews don't Work for Kotlin Multiplatform Mobile (KMM) Projects.

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.
Error message about preview building
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
}
Enter fullscreen mode Exit fullscreen mode

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. Correct displaying SwiftUI preview

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.

AWS Security LIVE! Stream

Go beyond the firewall

Security starts with people. Discover solutions to real-world challenges from AWS and AWS Partners on AWS Security LIVE!

Learn More

Top comments (0)

Postmark Image

The email service that speaks your language

Whether you code in Ruby, PHP, Python, C#, or Rails, Postmark's robust API libraries make integration a breeze. Plus, bootstrapping your startup? Get 20% off your first three months!

Start free

👋 Kindness is contagious

Explore this insightful post in the vibrant DEV Community. Developers from all walks of life are invited to contribute and elevate our shared know-how.

A simple "thank you" could lift spirits—leave your kudos in the comments!

On DEV, passing on wisdom paves our way and unites us. Enjoyed this piece? A brief note of thanks to the writer goes a long way.

Okay