https://kotlinlang.org logo
c

Christian Gaisl

05/02/2021, 7:03 PM
Hi, I’m building a multiplatform app and I have run into an issue in iOS regarding SwiftUI preview not working at all. I’ve boiled the issue down to the following minimum working example: I’m using an Android Studio KMM template project with the iOS framework distribution set to the CocoaPods dependency manager. When use a class that has the member-parameter syntax (I don’t know how it’s called? i mean “testString” in the following example code) in a View, the preview crashes. An example for such a class would be:
Copy code
//Does not work with SwiftUI preview
class Greeting1(
    var testString: String
) {
    fun greeting(): String {
        return "Hello, ${Platform().platform}!"
    }
}

//Works with SwiftUI preview
class Greeting2(
    testString: String
) {
    fun greeting(): String {
        return "Hello, ${Platform().platform}!"
    }
}
An instance of this class being part of the view, even if none of its members are actually displayed, crashes the preview. It works fine on the simulator though. This does not happen in the template project when using the Xcode build phases framework approach, the one with the packForXcode task. Does anyone have any idea what could cause this?
x

xxfast

05/03/2021, 3:31 AM
What does the swift ui preview diagnostics say?
c

Christian Gaisl

05/03/2021, 5:47 AM
It simply states that the preview has crashed
j

José González Gómez

05/03/2021, 11:12 AM
I seem to remember this was a known issue, but can't recall where did I read about it
c

Christian Gaisl

05/03/2021, 8:44 PM
I don’t think that it’s the same problem, because my preview diagnostics output is different (and even less helpful):
RemoteHumanReadableError: Failed to update preview.
The preview process appears to have crashed.
Error encountered when sending 'previewInstances' message to agent.
==================================
| RemoteHumanReadableError: The operation couldn't be completed. (BSServiceConnectionErrorDomain error 3.)
|
 
| BSServiceConnectionErrorDomain (3):
| ==BSErrorCodeDescription: OperationFailed
Nevertheless, I tried one of the suggested solutions, which was using the following settings in my “shared” gradle file:
Copy code
kotlin {
    targets.withType<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget> {
        binaries.withType<org.jetbrains.kotlin.gradle.plugin.mpp.Framework> {
            isStatic = false
        }
    }
}
However, I did not get this to compile with this setting enabled, Xcode could not find the shared library at all anymore. I tried setting the framework search paths to $(inherited), but no luck. 😕
v

Vitor Prado

05/03/2021, 9:58 PM
I fixed my problem with swiftui just removing build optimizations.
(this is a setting in your ios project)
just set your optimization level to none.
x

xxfast

05/04/2021, 1:17 AM
@Vitor Prado Where exactly is this setting?
v

Vitor Prado

05/04/2021, 12:37 PM
look on build settings inside your schemes.
open project settings.. go to your target settings.. open build setting tabs and use search to find “optimization level”
c

Christian Gaisl

05/04/2021, 2:55 PM
Did not solve my problem either, I guess I’ll just use the Framework+PackForXcode approach and do pod dependencies within Xcode
3 Views