20

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sparsh Dutta
  • 2,450
  • 4
  • 27
  • 54

5 Answers5

12

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.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
8

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.

David Pisoni
  • 3,317
  • 2
  • 25
  • 35
  • 1
    The update works for me. Thanks. I got this error after upgrading from Kotlin `1.5.10` and Compose `1.0.0` to Kotlin `1.5.21` and Compose `1.0.1`. – Sergio Aug 15 '21 at 05:09
  • 1
    Same here (1.0.0 -> 1.0.1 & 1.5.10 -> 1.5.21), but removing preview configs, invalidating caches and restarting did not fix the issue. – zifot Aug 18 '21 at 13:58
  • But can confirm that [Anton's answer](https://stackoverflow.com/a/68602802/359832) did. – zifot Aug 18 '21 at 14:16
  • Removing the "Compose Preview" in configurations did the trick for me. no need to invalidate cache. (using Compose 1.0.1) – Angel Koh Aug 23 '21 at 09:10
  • I found that running _Just Restart_ from the _Invalidate Caches / Restart_ menu was enough. No need to invalidate caches. – Rab Ross Sep 01 '21 at 08:58
3

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)
            )
        }
    }
}
Sebastian Rieger
  • 685
  • 5
  • 20
0

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"
Sparsh Dutta
  • 2,450
  • 4
  • 27
  • 54
0

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.

Rawlin Crasto
  • 195
  • 2
  • 8