androidx.compose.ui.tooling.preview.PreviewActivity is not an activity subclass or alias

Viewed 4217

I updated my compose and kotlinCompilerExtensionVersion to 1.0.0-rc02 from beta09, Kotlin version to 1.5.21, Gradle version to 7.1 and Gradle plugin version to 7.1.0-alpha04

Ever since - on trying to run MyScreen - I'm getting this error:

androidx.compose.ui.tooling.preview.PreviewActivity is not an activity subclass or alias

I am not able to resolve this. How do I resolve this?

5 Answers

I am using Compose 1.0, Kotlin 1.5.10 and Android Studio 2020.3.1, and I run into the same problem on most of my previews (not on others).

On the top bar menu, clicking on "Edit configurations" and manually creating a new configuration with the same parameters as those in the invalid configuration with warning (" androidx.compose.ui.tooling.PreviewActivity is not an Activity subclass or alias") did the trick for me.

I'm experiencing this error using Bumblebee (AKA, 2021.1.1) Canary 6, 7, and 8, using compose version 1.0.0, 1.0.1, and 1.1.0-alpha01. I've just raised a bug on the Studio issue tracker:

https://issuetracker.google.com/issues/196248459

To quote myself:

It seems clear this is a "left hand doesn't know what right hand is doing" thing, because PreviewActivity is not in that package, but rather androidx.compose.ui.tooling – it hasn't been in android.compose.ui.tooling.preview since compose 1.0.0-beta09.

UPDATE

I was able to get previews working again by clearing off all the preview run configurations. Here were my steps:

  1. Click on the "run configurations" selector and select Edit Configurations...
  2. Expand the Compose Preview grouping
  3. Select all the preview configurations (you can select the first and shift-select the last)
  4. Click the - button to remove all the collections
  5. Click Ok
  6. Run Invalidate Caches... in the file menu
  7. After AS restarts and your cache repopulates, run a preview. It should work now.

Apparently AS was caching "junk" in the temporary run configurations it was creating every time I launched a preview. The "junk" was valid for earlier versions of AS, but broke later versions. Clearing out this cache got me working again.

I've got the same error with Compose 1.0.0 and Kotlin 1.5.10 (which is recommended).

enter image description here

I fixed these with adding the following dependence debugImplementation "androidx.compose.ui:ui-tooling:$compose_version" in my gradle file.

Don't forget to set the version of the dependence to your current compose version.

enter image description here

After a rebuild and reload of the preview everything works fine for me.

The Preview of the example looks like this:

package com.example.compose.basics.ui

import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Card
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import de.db.dbs.travio.compose.basics.themeing.ComposeBasicsTheme

@Preview
@Composable
fun ScreenContent() {
    ComposeBasicsTheme {
        Card(modifier = Modifier
                .padding(8.dp)
                .shadow(8.dp, RoundedCornerShape(32.dp))
        ) {
            Text(
                    text = "Hello World!",
                    modifier = Modifier.padding(16.dp)
            )
        }
    }
}

The thing that worked for me was downgrading the ui-tooling version to beta09, i.e using this dependency for ui-tooling:

implementation "androidx.compose.ui:ui-tooling:1.0.0-beta09"

Updating the compose_version to 1.0.1 , and kotlin-gradle-plugin to 1.5.21 fixes the issue if the versions used are lower than mentioned here.

Related