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.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay