Hi all. is this the right space to talk about the ...
# multiplatform
d
Hi all. is this the right space to talk about the Kotlin Multiplatform Wizard? I'm trying to get a new project started to generate a cross-platform (iOS/Android/desktop) app while following the tutorials. I created the app using the wizard, but there's no JUnit support in project created.
👍 1
j
JUnit is JVM-only, so it will only work for your Android and desktop/JVM source sets. Common tests should use Kotlin Test, which uses JUnit by default in Android/JVM source sets. Check out this tutorial in the docs.
d
@Jeff Lockhart I did that, adding the following to the composeApp/build.gradle.kts file:
Copy code
kotlin {
        // ...
        commonTest.dependencies {
            implementation(libs.kotlin.test)
        }
}
However, I don't see things like "@Test" being available to annotate a unit test. What am I doing wrong?
j
Are you able to share the project you're having issues with?
d
Yes, I am: https://github.com/comixed/variant I've only just started coding it, so haven't added any features beyond the minimal application. I did manage to add a test for one piece of common code.
j
I checked out your code and don't see any problem with the tests. The PlatformKtTest in commonTest runs successfully. The one issue I had was I got the error:
java.lang.IllegalStateException: Module entity with name: variant should be available
initially during Gradle sync. I fixed this by changing
Copy code
rootProject.name = "Variant"
to lowercase
Copy code
rootProject.name = "variant"
in settings.gradle.kts.
d
@Jeff Lockhart kk - thank you for looking at the code. I guess it's a part of leaping into this as my first Kotlin project. Is there any site you'd recommend as a good source for examples and references for building a cross-platform app in Kotlin with KMP?
For example, the first feature I want to add is managing a list of remote servers to connect to. So I'd need to learn the Kotlin/KMP way of creating the datamodel, persisting it using some cross-platform support, etc. Can you point me to something to help get up to speed?
j
There are some good samples linked in the docs here. Personally, I use my KMP Couchbase Lite database library, Kotbase for JSON document storage. I have a couple very basic sample apps, as well as extensive documentation on usage.
Using Kotbase, there are several ways you can structure your data models, depending on the type of data and your preference. For example, accessing the JSON directly using the fragment API:
Copy code
val database = Database("db")

val mutableDoc = MutableDocument("remote-servers")
mutableDoc["servers"].array = MutableArray()
    .setData(listOf("<https://foo.com>", "<https://bar.com>"))
database.save(mutableDoc)

val doc = database.getDocument("remote-servers")
    ?: MutableDocument("remote-servers") // not found
val firstServer = doc["servers"][0].string
Or you can use kotlinx-serialization to map the JSON data to a model object. The docs have examples doing this from a query result using the KTX extensions library. Or similar to the above code accessing by document ID:
Copy code
@Serializable
class RemoteServers(
    servers: List<String> = emptyList()
)

val database = Database("db")

val remoteServers = RemoteServers(listOf("<https://foo.com>", "<https://bar.com>"))
val json = Json.encodeToString(remoteServers)
val mutableDoc = MutableDocument("remote-servers", json)
database.save(mutableDoc)

val doc = database.getDocument("remote-servers")
val remoteServersFromDb = Json.decodeFromString<RemoteServers>(doc?.toJSON() ?: "{}")
👍 1