darkmoon_uk
09/07/2021, 6:12 AM@Preview
s from your top-level Application modules, for either Android or Desktop. You can try to do it from library projects or submodules, and some stuff might work if you import extra dependencies; but it's unsupported and gets ugly (ref: this thread).
• Secondly; import the compose.preview
and compose.uitooling
dependencies for Android and Desktop as you would expect to; but for the Android top-level project you must apply a Gradle Dependency Substitution to 'deeply' replace all the JetBrains preview dependencies with the Google ones, for it to work with Android Studio properly. By 'deeply', I mean the replacement will affect all transitive dependencies as well. Just directly depending on the Google one won't work; you'll get duplicate class errors.
I will fetch you a code snippet for the dependency substitution...xxfast
09/07/2021, 6:18 AMcompose.preview
?darkmoon_uk
09/07/2021, 6:25 AMVersions.Compose.JetBrainsMultiplatform
= e.g. 1.0.0-alpha4-build331
Versions.Compose.GoogleAndroid
= 1.0.1
darkmoon_uk
09/07/2021, 6:26 AMbuild.gradle.kts
to workaround the 'broken' JetBrains Artifact and resolve the Google one instead.xxfast
09/07/2021, 6:27 AMdarkmoon_uk
09/07/2021, 6:39 AM@Preview
in the top-level module is painful in the common-case that you want to build a component library in it's own module... you would want the `@Preview`s to live alongside the components, but this is not directly possible right now 👎
As a compromise; I still define @Composable
functions intended as previews in the component library module, just without their @Preview
annotation, then expose them with the @Preview
from the top level module e.g:
Component Library Module (the preview implementation)
@Composable
fun MyButton_Preview() {
MyButton(...)
}
Top-level Application Module (just proxy the preview, adding annotation)
@Preview
@Composable
fun MyButton() = MyButton_Preview()
darkmoon_uk
09/07/2021, 6:39 AMxxfast
09/07/2021, 6:42 AM:login
feature module that targets ios, jvm, android, js while :login:ui
is a sub-module that only targets jvm and android. I wish i can just define my composable previews in the commonMain
of :login:ui
and keep the components and their previews togetherdarkmoon_uk
09/07/2021, 6:42 AMdarkmoon_uk
09/07/2021, 6:42 AMdarkmoon_uk
09/07/2021, 12:01 PMxxfast
09/07/2021, 12:25 PM:login:ui
kotlin {
android {
compilations.all {
kotlinOptions {
jvmTarget = "1.8"
}
}
// Part of workaround to transitively enforce Google Preview/Tooling while JetBrains' is broken
configurations.all {
resolutionStrategy {
dependencySubstitution {
substitute(module("org.jetbrains.compose.ui:ui-tooling-preview:${Jetbrains.Versions.composeMultiplatform}"))
.using(module("androidx.compose.ui:ui-tooling:${AndroidX.Versions.compose}"))
substitute(module("org.jetbrains.compose.ui:ui-tooling-preview:${Jetbrains.Versions.composeMultiplatform}"))
.using(module("androidx.compose.ui:ui-tooling-preview:${AndroidX.Versions.compose}"))
}
}
}
}
..
}
xxfast
09/07/2021, 12:26 PMobject AndroidX {
object Versions {
...
const val compose = "1.0.0"
}
}
object Jetbrains {
object Versions {
const val composeMultiplatform = "1.0.0-alpha3"
const val kotlin = "1.5.21"
..
}
}
xxfast
09/07/2021, 12:28 PM1.0.0-alpha4-build331
- perhaps the plugin version should also match version of the dependencydarkmoon_uk
09/07/2021, 12:34 PMdarkmoon_uk
09/07/2021, 12:35 PM@Preview
annotation from your feature submodule.xxfast
09/07/2021, 12:38 PMxxfast
09/07/2021, 12:39 PMdarkmoon_uk
09/07/2021, 1:19 PMComposeViewAdapter
message is actually the error state expected when the dependency substitution isn't applied.darkmoon_uk
09/07/2021, 1:21 PMbuild.gradle.kts
... not inside kotlin {...}
or android {...}
etc.xxfast
09/07/2021, 1:21 PMxxfast
09/07/2021, 1:26 PMxxfast
09/07/2021, 1:27 PMxxfast
09/07/2021, 1:29 PMcommonMain
source sets where these composable are definedxxfast
09/07/2021, 1:34 PMColton Idle
09/07/2021, 2:50 PMIgor Demin
09/07/2021, 3:55 PM@Igor Demin is this something every project should be doing? Re: deep dependency substitutionOnly as a workaround. Either Android Studio doesn't work with the artifacts published by maven coordinates other than
androidx.compose.ui:ui-tooling-preview
Or our org.jetbrains.compose.ui:ui-tooling-preview-android
is broken.
We currently investigate a possibility of not publishing our own android artifacts and reusing the already published androidx.compose
artifacts. After that this issue will no longer be relevant.
Is there a ticket/issue raised regarding this on compose-jb?I haven't found any
darkmoon_uk
09/08/2021, 1:02 AM