"RuntimeException: Could not launch activity...Unable to resolve activity for Intent" when running Jetpack Compose UI tests with createComposeRule
Asked Answered
S

3

19

Running tests with createComposeRule and hitting a stack trace like (irrelevant parts omitted):

java.lang.RuntimeException: Could not launch activity
at androidx.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:495)
...
Caused by: java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=my.app.package.name.here/android.app.Activity }
...
Saturniid answered 20/2, 2020 at 23:45 Comment(0)
M
43

The OP question is about the use of createComposeRule() which doesn't require a custom activity (it uses ComposeActivity under the hood).

In this case you need to include this below in your gradle file:

debugImplementation("androidx.compose.ui:ui-test-manifest:1.0.0-beta05")

If you take a look at the contents of that package, it's simply an AndroidManifest.xml with an <activity/> entry for androidx.activity.ComponentActivity.

Misdeal answered 27/4, 2021 at 22:15 Comment(3)
I've added the debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version". to solve the problemFaultfinder
I lost 3 days figuring this out. I thought adding the manifest to androidTestImplementation would have the same effect, but looks like you can't do that. So what this basically does it gives you a custom activity (androidx.activity.ComponentActivity) from which you can run your tests, but you have to include it via debugImplementation.Playpen
FWIW, I had to add it as implementation() after moving some androidTest code into a moduleElsworth
H
6

You need to add

<activity android:name="androidx.activity.ComponentActivity" />

to your manifest.

Helle answered 2/5, 2021 at 9:3 Comment(2)
I'm running an Android test using createComposeRule() and it works when "androidx.compose.ui:ui-test-manifest:$compose_version" is added, as above. I haven't added ComponentActivity to the manifest.Megganmeggi
This is the answer. It is also only necessary to add it to your debug manifest.Reportorial
S
2

You need declare an Activity with name android.app.Activity in your AndroidManifest.xml for the Compose UI tests to use to host the content. Add the following within your <application> tag:

<activity android:name="android.app.Activity" android:theme="@style/your_app_theme_here"/>

substituting your_app_theme_here with a theme that exists in your app.

Saturniid answered 20/2, 2020 at 23:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.