if you made a `previewImplementation` configuratio...
# compose-android
b
if you made a
previewImplementation
configuration, how would you make the previews use it? I guess I was hoping there was already a configuration like this built in that the preview used, but im guessing not.
m
Something like this:
Copy code
android {
    ....

    buildTypes {
        create("preview") {
            initWith(getByName("debug"))
            matchingFallbacks += listOf("debug")
        }
        ....
    }
}
dependencies {
    add("previewImplementation", libs.ui.tooling) // instead of debugImplementation(libs.ui.tooling)
}
Copy code
// in libs.versions.toml file
ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
and add the other dependency that you want to use in previews to
previewImplementation
as well
but honestly I think the latter solution I proposed here which is about using a boolean flag to conditionally add the dependency is more suitable here
so you set the value of this flag to true and when you get to build/launch your debug apk, just set it to false
b
i tried the first method, but previews didn't work at all. the second method would probably work, i just don't like having to manually change a boolean every time i build, since i only build debug builds and toggling it back and forth while i'm changing things is pretty tedious and i'm sure to forget which will cause runtime exceptions
m
I'm not sure about your setup, but I tried the first method in a project and I can confirm it works and the previews are showing with no issues in the IDE. maybe you forgot to rebuild the project after adding this new configuration
for the second method, you don't have to manually change the value every time, you can edit your run configuration settings for AS to include this parameter and set to false(Assuming you run the app from the green play button in Android studio).
b
sorry, I stopped getting notifications so I didn't see your reply. I did a full rebuild but the previews in AS still says class not found so I'm not sure if there's an extra step to tell it to use
previewImplementation
instead of
debugImplementation
. For your second remark, I use commandline
./gradlew
to build/install, but manually changing a configuration setting wouldn't be much different than editing a file (not-automatic).
i guess i could set the flag with a commandline switch, but i would still prefer to have a separate config if i could get that to work
m
what class which is not found? Can you share the stacktrace or the error message?
b
it was a class from the
compileOnly
dependency, let me see if i can set it up again
oh it's literally only this:
Copy code
java.lang.ClassNotFoundException: <my library class>
   at java.lang.ClassLoader.loadClass(ClassLoader.java:592)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:525)
changing the dependency from
compileOnly
to
implementation
fixes the error, but this just brings us back to the beginning of trying to create a separate build config for the preview plugin
m
add it to
previewImplementation
not
implementation
as I said previously here at the end of f this message https://kotlinlang.slack.com/archives/C04TPPEQKEJ/p1725900495580069?thread_ts=1725472532.293059&amp;cid=C04TPPEQKEJ
Also, try to check if preview variant for current module is the active one in build variants tab in AS
b
Ohhh, that was the problem, I didn't realize I had to set the variant in the AS UI, i think that made it work
that's a good solution for my use, thanks for all the help