https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
s

Sachinda Liyanaarachchi

09/01/2022, 5:09 PM
How can I have a business layer model class that only supports js
h

hfhbd

09/01/2022, 5:11 PM
What do you mean? Just define it in your js only module/sourceset?
s

Sachinda Liyanaarachchi

09/01/2022, 5:17 PM
im trying to add fritz2 to my multiplatform project. I have
implementation("dev.fritz2:core:$fritz2Version")
in
commonMain
but when I try to build iOSApp it’s not recognizing this because this is a js library I believe. But I need this for a class in the
shared
model folder
h

hfhbd

09/01/2022, 5:20 PM
Yes it is js only. Kotlin Multiplatform does not recompile dependency and make them available on other platform. What is your use case?
s

Sachinda Liyanaarachchi

09/01/2022, 5:26 PM
Im just testing out the fritz2 for webapp and I want to have android and ios in the same project. My problem is, according to the fritz2 documentation, there is a model class that has this in the commonMain:
Copy code
@Lenses
data class Framework(val name: String) {
    companion object
}
but this is giving me an error when I run the iosApp…
h

hfhbd

09/01/2022, 5:33 PM
yeah, fritz2 is a multiplatform library, but only js and jvm according to their build.gradle file. So it is not possible to use this with ios unless fritz publishes an ios variant too.
s

Sachinda Liyanaarachchi

09/01/2022, 5:39 PM
oh…so many hours spent trying to figure this out 🥲 thanks anyways!
c

Casey Brooks

09/01/2022, 5:44 PM
In general, it’s a good practice to have your shared
commonMain
code related only to your own business logic, and not to any platform- or ui-specific code. Classes that are used to configure or generate code for specific platforms are best kept in that platform’s own code, and you transform the Shared model into your Fritz2 model, for example. This isolates both the shared code and your Fritz code from each other in case one or the other changes in the future, making it much easier to update and maintain them both (at the cost of some additional boilerplate code). A bit of a workaround would be to configure an additional sourceSet that is shared by the JVM and JS targets, but not iOS. A new
fritzMain
sourceSet should be able to contain the Fritz2 dependency and share it between JVM and JS, but it’s not shared to iOS so it won’t cause an issue running iOS. Something like this should work (I’ve done this kind of thing succesfully with Compose before):
Copy code
kotlin {
    sourceSets {
        val commonMain by getting {
            dependencies { }
        }
        val fritzMain by creating {
            dependsOn(commonMain)
            dependencies {
                implementation("dev.fritz2:core:$fritz2Version")
            }
        }
        val jvmMain by getting {
            dependsOn(fritzMain)
            dependencies { }
        }
        val jsMain by getting {
            dependsOn(fritzMain)
            dependencies { }
        }
        val iosMain by getting {
            dependsOn(commonMain)
            dependencies { }
        }
    }
}
s

Sachinda Liyanaarachchi

09/01/2022, 5:47 PM
This is very helpful! I’ll give this a try. Thank you!
5 Views