I seem to be having trouble with Preview in compose, the layout panel doesn't appear when I annotate a compose method with @preview. I assume I'm missing a dependency, but I've copied and pasted the code from here https://developer.android.com/jetpack/compose/setup. Any suggestions? (tried the usual clear cache, reopen project etc) :)
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.0.0-alpha10'
kotlinCompilerVersion '1.4.21'
}
}
dependencies {
implementation 'androidx.compose.ui:ui:1.0.0-alpha10'
// Tooling support (Previews, etc.)
implementation 'androidx.compose.ui:ui-tooling:1.0.0-alpha10'
// Foundation (Border, Background, Box, Image, Scroll, shapes, animations, etc.)
implementation 'androidx.compose.foundation:foundation:1.0.0-alpha10'
// Material Design
implementation 'androidx.compose.material:material:1.0.0-alpha10'
// Material design icons
implementation 'androidx.compose.material:material-icons-core:1.0.0-alpha10'
implementation 'androidx.compose.material:material-icons-extended:1.0.0-alpha10'
// Integration with observables
implementation 'androidx.compose.runtime:runtime-livedata:1.0.0-alpha10'
implementation 'androidx.compose.runtime:runtime-rxjava2:1.0.0-alpha10'
// UI Tests
androidTestImplementation 'androidx.compose.ui:ui-test-junit4:1.0.0-alpha10'
implementation 'com.google.android.material:material:1.2.1'
}
Here is my attempt at using preview (in AS it says Function "DefaultPreview" is never used)
import androidx.compose.ui.tooling.preview.Preview
.....
@Preview
@Composable
fun DefaultPreview() {
Text(text = "Hello!")
}
Click the Deploy to Device icon next to the @Preview annotation or at the top of the preview, and Android Studio will deploy that @Preview to your connected device or emulator.
Jetpack Compose is designed to work with the established View-based UI approach. If you're building a new app, the best option might be to implement your entire UI with Compose.
If you want to use the Navigation component with Compose, you have two options: Define a navigation graph with the Navigation component for fragments. Define a navigation graph with a NavHost in Compose using Compose destinations. This is possible only if all of the screens in the navigation graph are composables.
This library is based on an annotation processor that helps to organize, discover, search, and visualize Jetpack Compose UI elements. It provides a UI with minimal settings that allow you to quickly find your components, colors, and typography.
buildFeatures {
compose true
}
Write the above piece of code inside the Android block in the build.gradle file. Your problem will then be solved.
I'm going to leave this up in case others run into the same issue as I did (it is user error, but I also think the documentation could be clearer).
There are two versions of Android Canary, beta and arctic fox (alpha). Make sure you are using arctic fox if you want to use the latest version of the compose libraries. I've found the compose library 'androidx.compose.ui:ui-tooling:1.0.0-alpha08'
(and higher) doesn't work very well with the beta version of canary.
For me it was some kind of mixture
dependencies { classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.20' }
android { composeOptions { kotlinCompilerExtensionVersion'1.1.1' } buildFeatures { compose true } } dependencies { implementation("androidx.compose.ui:ui:1.1.1") // Tooling support (Previews, etc.) debugImplementation "androidx.compose.ui:ui-tooling:1.1.1" implementation "androidx.compose.ui:ui-tooling-preview:1.1.1" // Foundation (Border, Background, Box, Image, Scroll, shapes, animations, etc.) implementation("androidx.compose.foundation:foundation:1.1.1") // Material Design implementation("androidx.compose.material:material:1.1.1") // Material design icons implementation("androidx.compose.material:material-icons-core:1.1.1") implementation("androidx.compose.material:material-icons-extended:1.1.1") // Integration with observables implementation("androidx.compose.runtime:runtime-livedata:1.1.1") implementation("androidx.compose.runtime:runtime-rxjava2:1.1.1") }
Very important: check for correct @Preview import - should be:
import androidx.compose.ui.tooling.preview.Preview
Example:
@Composable fun SimpleComposable() { Text("Hello World") } @Preview @Composable fun ComposablePreview() { SimpleComposable() }
@Preview function should be without params.
For me, I missed one more dependency in my module's gradle
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
https://developer.android.com/jetpack/compose/tooling
I think you can find something you want in configuration
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With