I have a multiplatform compose module that targets...
# compose-desktop
m
I have a multiplatform compose module that targets Android and Desktop (which is then a dependency for the Android app and Desktop app). The Compose functions are in the common source set (e.g. as normal / as per the template). The preview works fine in the IDE (import androidx.compose.desktop.ui.tooling.preview.Preview). Jvm compiles fine. Metadatacommonclasses (eg.. commonMain src) compiles fine. Android compilation fails with: Unresolved reference: Preview The Multiplatform template ( https://github.com/JetBrains/compose-multiplatform-template ) isn't using preview as far as I can see. I can workaround the issue by creating another file in the desktopMain source and putting the preview there. Anyone seen this or used previews more successfully in multiplatform including an Android target?
a
The desktop Preview only works for the desktop and the Android only for Android (in Android Studio)
m
Thanks for the clarification Alexander, much appreciated. If the desktop preview is only for the desktop, then it seems like it would be better for the @Preview annotation to be available only for the desktop target. It does seem like something that could use some polish when possible. The primary use case (and what I really like) Compose multiplatform is the ability to build a UI once in a common module and use it on Desktop, Android, etc (and still be able to seamlessly integrate with native/Java APIs). So if that is the primary way to do things (as per the provided templates), then it would be good to have a way to preview things that doesn't require extra files.
a
It is only available for the desktop target
It’s
androidx.compose.desktop.ui.tooling.preview.Preview
For Android it’s
androidx.compose.ui.tooling.preview.Preview
That’s why you’re getting “unresolved reference”
m
Screenshot from 2023-09-06 10-03-00.png
I was able to add it to the commonMain, the preview works in the IDE (Android Studio in this case with the Compose Multiplatform plugin)
a
Interesting. But still, you’re getting the “desktop” preview.
m
And if I run gradlew metadatacommonmainclasses (which should fail if there is something wrong with the commonMain dependencies) it compiles fine
a
i.e. how it would look on the desktop
m
The preview itself is fine...
If the preview is only for desktop, then it seems like what I have shouldn't compile at all
And should be shown as an error by the IDE
a
I see your point.
👍 1
m
In case its useful: this is what I have in build.gradle.kts for the module:
Copy code
plugins {
    kotlin("multiplatform")
    id("com.android.library")
    alias(libs.plugins.jetbrains.compose)
}

kotlin {
    androidTarget {

    }

    jvm("desktop")

    sourceSets {
        val commonMain by getting {
            dependencies {
                api(project(":core"))
                api(project(":lib-database"))
                implementation(compose.runtime)
                implementation(compose.foundation)
                implementation(compose.material)
                implementation(compose.materialIconsExtended)
                @OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
                implementation(compose.components.resources)
                implementation(libs.kodein.di)
                implementation(libs.moko.resources)
                implementation(libs.moko.resources.compose)
            }
        }

        val androidMain by getting {
            dependencies {
                api(libs.activity.compose)
                api(libs.androidx.appcompat)
                api(libs.androidx.core.ktx)
            }
        }

        val desktopMain by getting {
            dependencies {
                api(compose.desktop.common)
                implementation(compose.desktop.currentOs)
            }
        }
    }

}

android {
    compileSdk = 33
    namespace = "com.ustadmobile.libuicompose"

    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    sourceSets["main"].res.srcDirs("src/androidMain/res")
    sourceSets["main"].resources.srcDirs("src/commonMain/resources")

    defaultConfig {
        minSdk = 21
        targetSdk = 33
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlin {
        jvmToolchain(17)
    }
}
a
Hmm, I’m getting this in `commonMain`:
m
Strange... our project is open source... I can give you the revision to check out if you wish
a
Is it possible one of the dependencies in your
commonMain
pulls in the desktop libs?
m
I don't think so... there is only one other module that targets JVM and uses compose ( app-desktop ), and it has lib-ui-compose as a dependency
lib-ui-compose is the module where I have the preview annotation
a
moko-resources-compose seems to be pulling that in
Maybe because of this?
Copy code
named("jvmMain") {
            dependencies {
                api(compose.desktop.common)
                implementation(compose.desktop.currentOs)
            }
        }
Not sure, don’t have enough gradle knowledge.
m
What would indicate that moko-resources-compose is pulling it in? Gradle dependencies output?
a
I looked at that, but didn’t actually find it there. But commenting out
Copy code
implementation(libs.moko.resources.compose)
causes the annotation to not be found in
commonMain
m
Weird... I'll double check that later and maybe submit an issue to moko-resources if that is where it's at
The logical question that would follow is: why would moko-resources commonMain compile OK?
If the preview annotation should be accessible only on JVM...
Thanks again for checking it, very much appreciated
a
Weird, adding
Copy code
implementation("dev.icerock.moko:resources:0.23.0")
                implementation("dev.icerock.moko:resources-compose:0.23.0")
to a simple project doesn’t cause the Preview annotation to be available in
commonMain
Maybe
moko-resources-plugin
does some magic to cause that
m
But the moko-resources-plugin is not applied on the lib-ui-compose module
although... the moko-resources-plugin is applied to a dependency of lib-ui-compose...
might be this on core (dependency of lib-ui-compose) :
Copy code
sourceSets {

    commonMain {
        dependencies {
            api libs.moko.resources
Though changing api to implementation doesn't seem to fix it