Hello. I have an issue with CMP Preview on multi m...
# multiplatform
a
Hello. I have an issue with CMP Preview on multi module app. On the module the preview isn't working. Does anyone know how to solve this? Thanks you 🙂
j
Last I checked you can't have previews in commonMain. I think that's still the case, but happy to be proven wrong 😄 If it is still the case, you'll need to move your previews to a non-common target, like androidMain
a
currently it's work in my commonMain composeApp, but not in the other modules of my CMP code. I have a composeApp module, a feature module, a core module...
h
Hi @Alexandra Gnimadi! You have to check your gradle, probably you are missing something. I am using Android Studio to develop and preview (I find no reason to not use it) and I write compose on a KMP module, and to preview it I have this dependency on the commonMain sourceSet
Copy code
api(compose.components.uiToolingPreview)
And this one at the root level of the build.gradle.kts file.
Copy code
dependencies {
    debugImplementation(compose.uiTooling)
}
I'm not sure the relation between those too dependencies, but I needed both in order to see the previews By the way, I use the following plugins, which you should already be using if you can write and test your code, leaving them here just in case
Copy code
plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.androidLibrary)

    alias(libs.plugins.composeMultiplatform)
    alias(libs.plugins.composeCompiler)
    alias(libs.plugins.composeHotReload)
}
Please let me know if that fixes your issues! Note: I'm using "api" but you can use "implementation", it has nothing to do with the previews within the module
a
Hello @Hrodrick thanks for your response I'm using almost the same dependencies like you, the only difference is that I'm using the new androidKotlinMultiplatformLibrary which are recommended for now:
Copy code
plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.androidKotlinMultiplatformLibrary)
    alias(libs.plugins.androidLint)
    alias(libs.plugins.composeMultiplatform)
    alias(libs.plugins.composeCompiler)
    alias(libs.plugins.kotlinSerialization)
}
Also, I think that it's the real difference. But with this I found something to activate preview. I added this three dependencies in the androidMain of each build.gradle of my libraries and it permit me to see the preview now in the commonMain of my kmp library
Copy code
androidMain {
            dependencies {
                // Add Android-specific dependencies here. Note that this source set depends on
                // commonMain by default and will correctly pull the Android artifacts of any KMP
                // dependencies declared in commonMain.
                implementation(compose.uiTooling)
                implementation(libs.androidx.emoji2.text)
                implementation(libs.androidx.customview.poolingcontainer)
            }
        }
h
Awesome! I'm glad you could solve the issue!
👍 1
j
I’m also facing this issue and cannot get it to work, tried the above mention solution but still no luck, getting this error message:
Copy code
The following classes could not be instantiated:
- androidx.compose.ui.tooling.ComposeViewAdapter (Open Class, Show Exception, Clear Cache)
Using
androidKotlinMultiplatformLibrary
in my modules
Copy code
commonMain.dependencies {
            implementation(compose.components.uiToolingPreview)
}

androidMain.dependencies {
            implementation(compose.uiTooling)
}
Running CMP 1.10.0-alpha02 I’ve also tried adding previews in
androidMain
sourceSets as I’ve heard people getting that to work, but still no luck… Anyone have a clue what else I’m missing?