30

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!")
}
user12507977
  • 361
  • 1
  • 3
  • 7
  • hello have u found workaround for this i tried many things but none worked – Suj Jan 18 '22 at 17:13
  • For me it was updating to "androidx.compose.ui:ui-tooling-preview:1.2.0-alpha02" even though I had buildFeatures set. – Rafael Feb 03 '22 at 15:33
  • Can you please try adding this below dependency. I had been facing this for long but this solved it magically, though not sure why it is needed. implementation("androidx.activity:activity-compose:1.3.1") – prateek Mar 03 '22 at 05:16

14 Answers14

37
    buildFeatures {
        compose true
    }

Write the above piece of code inside the Android block in the build.gradle file. Your problem will then be solved.

Neeraj Sharma
  • 371
  • 3
  • 4
13

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

Tink
  • 586
  • 7
  • 10
8

For me it was some kind of mixture

  1. Be sure you have latest Android Studio version
  2. Within project/build.gradle be sure you have
dependencies {
  classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.20' 
}
  1. Within app/build.gradle be sure you have
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")
}
  1. File -> Invalidate Ccaches and restart
  2. Run project once

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.

Liviu
  • 768
  • 8
  • 9
6

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.

user12507977
  • 361
  • 1
  • 3
  • 7
5

I think you can find something you want in configuration enter image description here

MJ Studio
  • 3,947
  • 1
  • 26
  • 37
2

Updating to the latest version of AS solved the error for me.

Try the latest version from here https://developer.android.com/studio/preview

Sonu Sourav
  • 2,926
  • 2
  • 12
  • 25
  • I updated the version to the latest 1.0.5 but it is not working. downgrading to 1.0.1 that works for me. Use old compose version 1.0.1 until they fix. – Vasudev Vyas Jan 05 '22 at 07:45
2

From the Android Developers website about Jetpack Compose Tooling:

To enable Android Studio-specific features for Jetpack Compose, you need to add these dependencies in your application build.gradle file:

debugImplementation "androidx.compose.ui:ui-tooling:1.4.2"
implementation "androidx.compose.ui:ui-tooling-preview:1.4.2" 
Alex Mandelias
  • 436
  • 5
  • 10
1

The Jetpack Compose (rc01, rc02) has an issue with @Preview. You can solve it by adding the following code in your build.gradle file:

android {
   ...
}

configurations.all {
    resolutionStrategy {
        force("androidx.compose.ui:ui-tooling:1.0.0-beta09")
        force("androidx.compose.ui:ui-tooling-data:1.0.0-beta09")
        force("androidx.compose.ui:ui-tooling-preview:1.0.0-beta09")
    }
}

dependencies {
   ...
}

Example: https://github.com/AlexZhukovich/ComposePlayground/blob/main/app/build.gradle

1

I had to add

debugImplementation 'androidx.customview:customview-poolingcontainer:1.0.0-rc01'
debugImplementation 'androidx.customview:customview:1.1.0'

Source

bytesculptor
  • 411
  • 1
  • 4
  • 21
1

This is what worked for me add these dependencies if missed any

In the new AS compose runtime is missing and probably moved to something else and added UI tooling from the top when these were added studio started working with preview.

implementation("androidx.compose.runtime:runtime:1.2.0")
implementation("androidx.compose.ui:ui-tooling:1.2.0")

all together

implementation("androidx.activity:activity-compose:1.5.1")
implementation("androidx.compose.ui:ui:1.2.0")
implementation("androidx.compose.runtime:runtime:1.2.0")
implementation("androidx.compose.ui:ui-tooling-preview:1.2.0")
implementation("androidx.compose.ui:ui-tooling:1.2.0")
vikas kumar
  • 10,447
  • 2
  • 46
  • 52
0

For me I just didn't have the following in my gradle file:

composeOptions {
    kotlinCompilerExtensionVersion '1.0.3'
}

and

buildFeatures {
   compose true
}

ryankuck
  • 320
  • 3
  • 15
0

in project's build.gradle specify:

dependencies {
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30"
        }
teafoot
  • 1
  • 1
0

I had the same issue, and also had some compiling problems. My conclusion is that you need a good "variable versions" schema, for example is important use the same version for some dependencies, for example the Kotlin version.

I did something like this project: Jetpack Compose Navigation

where they using a global definition of version variables like this:

buildscript {
    ext {
        // Sdk and tools
        compileSdkVersion = 33
        minSdkVersion = 21
        targetSdkVersion = 32

        // App dependencies
        appCompatVersion = '1.5.1'
        activityComposeVersion = '1.6.0'
        composeCompilerVersion = "1.3.0"
        composeVersion = '1.2.1'
        coreTestingVersion = '2.1.0'
        espressoVersion = '3.4.0'
        gradleVersion = '7.2.2'
        kotlinVersion = '1.7.10'
        ktlintVersion = '0.45.2'
        ktxVersion = '1.9.0'
        materialVersion = '1.6.1'
        navigationComposeVersion = '2.5.2'
    }
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$gradleVersion"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        classpath 'com.google.gms:google-services:4.3.14'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.2'
    }
}

And then use it in your app build.gradle too:

dependencies {

    implementation project(path: ':libtoolscommon')

    implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion"
    implementation "androidx.core:core-ktx:$rootProject.ktxVersion"
    implementation "com.google.android.material:material:$rootProject.materialVersion"
    implementation 'androidx.preference:preference-ktx:1.2.0'

    // Compose
    implementation "androidx.compose.runtime:runtime:$rootProject.composeVersion"
    implementation "androidx.compose.ui:ui:$rootProject.composeVersion"
    implementation "androidx.compose.foundation:foundation:$rootProject.composeVersion"
    implementation "androidx.compose.material:material:$rootProject.composeVersion"
    implementation "androidx.compose.material:material-icons-extended:$rootProject.composeVersion"
    implementation "androidx.activity:activity-compose:$rootProject.activityComposeVersion"
    implementation "androidx.navigation:navigation-compose:$rootProject.navigationComposeVersion"
    debugImplementation "androidx.compose.ui:ui-tooling:$rootProject.composeVersion"

    // Testing dependencies
    androidTestImplementation "androidx.arch.core:core-testing:$rootProject.coreTestingVersion"
    androidTestImplementation "androidx.test.espresso:espresso-contrib:$rootProject.espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$rootProject.espressoVersion"

    // Compose testing dependencies
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$rootProject.composeVersion"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$rootProject.composeVersion"

    implementation "androidx.compose.material3:material3:1.0.0-beta03"
    implementation "androidx.compose.material3:material3-window-size-class:1.0.0-beta03"
    implementation 'androidx.window:window:1.1.0-alpha03'

   ---
}

And also try to have the last version of this libraries.

If you have a whole project in Kotlin, it should be like this:

    implementation(libs.androidx.core.ktx)
    implementation(libs.kotlin.stdlib)
    implementation(libs.kotlinx.coroutines.android)

    implementation(libs.androidx.compose.ui.tooling.preview)
    debugImplementation(libs.androidx.compose.ui.tooling)
    implementation(libs.androidx.compose.materialWindow)
    implementation(libs.androidx.compose.material.iconsExtended)

    implementation(libs.androidx.lifecycle.runtime)
    implementation(libs.androidx.lifecycle.viewModelCompose)
    implementation(libs.androidx.navigation.compose)

    implementation(libs.androidx.activity.compose)
    implementation(libs.androidx.window)
Hpsaturn
  • 2,702
  • 31
  • 38
  • Don't do that. Just use the compose BoM and just add the version once. Without any variable: https://developer.android.com/jetpack/compose/bom/bom?hl=es-419 – Rubén Viguera Apr 03 '23 at 05:54
  • At the moment I did that because the official example from Google did that. But I agree with you. That is the way. – Hpsaturn Aug 31 '23 at 11:18
-1

Expect it to show in the 'Split' or 'Design' mode after a build. (Not right over code like it seems in the tutorial.)

clwhisk
  • 1,805
  • 1
  • 18
  • 17