Is there any way to get sample images to use in a ...
# compose-android
e
Is there any way to get sample images to use in a preview without including the image in your build (like how
@tools:sample
works in xml)?
g
Other than generating the resource programmatically, there is maybe a possibly hacky solution that I used to include code just for the previews (R8 is generally enough for this, but I could not rely on it). Create a library module with all the resources you want and then depend on it with both
compileOnly
and
androidTestImplementation
.
compileOnly
will let the preview build, while
androidTestImplementation
will ensure the resources will be present in previews launched on the device.
c
Sounds like we should file a feature request. I like it!
g
To be fair, what I proposed could be also done with a simple
debugImplementation
and something like
androidx.compose.ui:ui-tooling
, but with sample resources for previews. I went with that weird and more complex `androidTestImplementation`/`compileOnly` combo simply because I disable the debug variant of all my modules, so that was the only way to exclude the resources from actual builds.
Scratch that,
debugImplementation
would not work, because then the resources would be missing when building the release flavor. So it's either what I'm suggesting or something even more complex done at the tooling level
(sorry, I'm thinking out loud) or something that uses reflection to load resources by name, so that the build works with and without the resources, thus allowing the use of
debugImplementation