Jetpack Compose stability in a KMM project I’m cu...
# compose
s
Jetpack Compose stability in a KMM project I’m currently writing a KMM application where I use Jetpack Compose on the Android side. Of course most of the business logic, the ViewModels and data classes are placed in the
shared
module under
commonMain
. Now I have the problem that the Compose compiler determines all of my (immutable) data classes as unstable because they are placed in a different module. I cannot add the Compose runtime dependency to
commonMain
because this is not a multiplatform dependency and the project just won’t compile when doing so. Of course I could add wrapper classes like described in the linked article and use them only for Android, but this would mean I would have to start using generic types in my ViewModels extensively to ensure that I still share most of the business logic. Isn’t there another way of telling the Compose compiler which classes are actually immutable/stable, maybe via command line arguments or a configuration similar to ProGuard/R8? I’m using version
1.3.0
of Compose compiler.
j
Use the jetbrains compose runtime dependency in commonMain which will give you access to the annotations
s
Thanks Jake, I tried that like I wrote above 😉 but then I get the following error when syncing the project in AS:
Copy code
:shared:iosArm64Main: Could not resolve org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4.
Required by:
    project :shared > androidx.compose.runtime:runtime:1.3.0-beta01
Ok, I fixed this by specifying the dependency as
Copy code
implementation("androidx.compose.runtime:runtime:1.3.0-beta01") {
    exclude(group = "org.jetbrains.kotlinx")
}
I had to figure out the right syntax because actually I’m using Gradle version catalogs but this seems to be not well supported in a multiplatform project.
Copy code
implementation(libs.androidx.compose.runtime) {
    exclude(group = "org.jetbrains.kotlinx")
}
wasn’t working because
implementation
doesn’t provide this overload as well as
Copy code
implementation(variantOf(libs.androidx.compose.runtime) { exclude(group = "org.jetbrains.kotlinx") })
which I’ve seen in a few examples wasn’t working either because
variantOf
isn’t known 🤷🏼
s
You are using the
androidx.compose
one - he meant
org.jetbrains.compose
s
Oops, sorry 🙈 Thanks for the clarification!
I’m sorry, me again. Adding
Copy code
val commonMain by getting {
    dependencies {
        ... other dependencies here
        implementation(libs.jetbrains.compose.runtime)
    }
}
where
libs.jetbrains.compose.runtime
points to
org.jetbrains.compose.runtime:runtime:1.1.1
now gives me:
Copy code
:shared:iosArm64Main: Could not resolve org.jetbrains.compose.runtime:runtime:1.1.1.
m
I have same problem but am getting:
Copy code
Unable to find method ''void org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions.setUseIR(boolean)''
'void org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions.setUseIR(boolean)'
I gave up on it for the time being 😄
s
Interestingly when using the alpha version
org.jetbrains.compose.runtime:runtime:1.2.0-alpha01-dev620
this only gives me dependency errors for iOS Simulator. When commenting everything out in
build.gradle.kts
related to
iosSimulator
, the project does sync. But this cannot be the solution… 😐
m
But this cannot be the solution…
yes… but thanks, I will try this just to see compose compiler metrics
s
I found a newer alpha version of the dependency which is not on Maven Central:
org.jetbrains.compose.runtime:runtime:1.2.0-alpha01-dev774
. You need to add the repository first, for example in root `build.gradle.kts`:
Copy code
allprojects {
    repositories {
        google()
        mavenCentral()
        // Add this
        maven("<https://maven.pkg.jetbrains.space/public/p/compose/dev/>")
    }
}
This version works with iOS Simulator, too.
m
I defined expected declarations for the annotations and then used type aliases on Android and created new annotations on iOS.
s
That’s also a viable solution 👍🏼
301 Views