Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 650
Sparsh Dutta Avatar asked Jul 23 '21 12:07

Sparsh Dutta


People also ask

Is Jetpack Compose available on Android Studio?

For the best experience developing with Jetpack Compose, download and install Android Studio. It includes many smart editor features, such as new project templates and the ability to immediately preview your Compose UI and animations.

Can you use Java with jetpack compose?

Can I use it with Java? Jetpack Compose is Kotlin exclusive. It uses features such as coroutines, and the handling of @Composable annotations is done by a Kotlin compiler. There is no way to get access to these from Java.

What is Androidx compose?

Build better apps faster with. Jetpack Compose. Jetpack Compose is Android's modern toolkit for building native UI. It simplifies and accelerates UI development on Android. Quickly bring your app to life with less code, powerful tools, and intuitive Kotlin APIs.

How do I use @preview in Android Studio?

@Preview accepts parameters to customize the way Android Studio will render it. You can add these parameters manually in your code, or click on the gutter icon next to @Preview to display the configuration picker, to let you select and change those configuration parameters. Android Studio offers some features to extend composable previews.

What is @preview and how do I use it?

@Preview accepts parameters to customize the way Android Studio will render it. You can add these parameters manually in your code, or click on the gutter icon next to @Preview to display the configuration picker, to let you select and change those configuration parameters.

What's new in androidx compose?

androidx.compose.ui:ui-*:1.2.0-alpha06 is released. Version 1.2.0-alpha06 contains these commits. Added RequestFocus semantics action to request focus on the focusable target. ( I17b71) Updated parsing of vector drawables to support auto mirroring to flip the content of a VectorPainter if the current layout direction is RTL. ( I79cd9, b/185760237)

What's new in composeviews?

ComposeViews placed in view hierarchies that are children of another composition now host child compositions of their ancestors ( I92883) Updated compose's imageFromResource API to reuse the resource drawable cache when loading ImageBitmap objects. ( If3627, b/178751994) androidx.compose.ui:ui-*:1.0.0-alpha11 is released.


Video Answer


3 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.

like image 89
Anton Lapshin Avatar answered Oct 17 '22 00:10

Anton Lapshin


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.

like image 27
David Pisoni Avatar answered Oct 17 '22 02:10

David Pisoni


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)
            )
        }
    }
}
like image 23
Sebastian Rieger Avatar answered Oct 17 '22 02:10

Sebastian Rieger