Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A successful build is needed before the preview can be displayed or can not show content in preview in Android studio 4.0 Canary

I have tried to open the preview, but I can not open it.

I have tried opening Android Studio again, but the preview shows only a blank white screen and no content is shown inside it. When I open it again, it shows the following message at the preview side:

A successful build is needed before the preview can be displayed

A Build process has been finished and tried to build multiple times but still showing the same error.

I have used simple text inside it.

    @Composable
    fun Greeting(name: String) {
        Text (text = "Hello $name!")
    }

Here is my full content:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Greeting("Android")
        }
    }

    @Composable
    fun Greeting(name: String) {
        Text (text = "Hello $name!")
    }

    @Preview
    @Composable
    fun PreviewGreeting() {
        Greeting("Android")
    }
}

How can I fix this?

like image 379
Harsh Panchal Avatar asked Nov 20 '19 12:11

Harsh Panchal


People also ask

How do you compose a preview?

It is very simple to create a Preview in Compose, simply annotate a Composable function with “@Preview” annotation and call the Composable function that you want to render on your editor or screen.

What is Android canary?

Canary channel. Canary channel gets all the newest releases (including stable). That means that you can test all the features as soon as they are built. Updates are usually released weekly and are mostly used to show the new latest and greatest features.

How to see Live changes in Android Studio?

To turn it on manually, navigate to File > Settings > Editor > Live Edit (Android Studio > Preferences > Editor > Live Edit on a Mac).

What annotation is used to annotate composable functions?

The function is annotated with the @Composable annotation. All Composable functions must have this annotation; this annotation informs the Compose compiler that this function is intended to convert data into UI. The function takes in data.


1 Answers

I had the same issue. The build error message I got when I clicked on "Run with --info" was:

e: This version (1.0.0-alpha13) of the Compose Compiler requires Kotlin version 1.4.30 but you appear to be using Kotlin version 1.5.0 which is not known to be compatible. Please fix your configuration (or suppressKotlinVersionCompatibilityCheck but don't say I didn't warn you!).

So I went to the project level build.gradle file and added a snippet before the changes (you may change the version according to your error message) -

buildscript {
    ..
    ..
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.0-alpha15"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
    ..
    ..
}

So according to the error message, I changed kotlin-gradle-plugin version from 1.5.0 to 1.4.30 as below:

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0"

to

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30"

After this again, I did 'Sync Now' the Gradle file and did 'Build & Refresh' in the preview tab. Hopefully the compatibility issues were gone and I was able to see the real time preview.

This may not be the best solution as we it requires downgrading Kotlin version, but yeah it worked for me. You can just try it off!!

like image 137
Ayush Khare Avatar answered Sep 25 '22 13:09

Ayush Khare